Merge pull request #389 from mredigonda/mentions-parser

Mentions parser
This commit is contained in:
Ivan Diaz 2018-11-17 13:13:48 -03:00 committed by GitHub
commit 7dd88a8f82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import AdminDataActions from 'actions/admin-data-actions';
import i18n from 'lib-app/i18n';
import API from 'lib-app/api-call';
import SessionStore from 'lib-app/session-store';
import MentionsParser from 'lib-app/mentions-parser';
import TicketEvent from 'app-components/ticket-event';
import AreYouSure from 'app-components/are-you-sure';
@ -73,7 +74,7 @@ class TicketViewer extends React.Component {
</div>
{this.props.editable ? this.renderEditableHeaders() : this.renderHeaders()}
<div className="ticket-viewer__content">
<TicketEvent type="COMMENT" author={ticket.author} content={ticket.content} date={ticket.date} file={ticket.file}/>
<TicketEvent type="COMMENT" author={ticket.author} content={this.props.userStaff ? MentionsParser.parse(ticket.content) : ticket.content} date={ticket.date} file={ticket.file}/>
</div>
<div className="ticket-viewer__comments">
{ticket.events && ticket.events.map(this.renderTicketEvent.bind(this))}
@ -206,6 +207,9 @@ class TicketViewer extends React.Component {
}
renderTicketEvent(options, index) {
if (this.props.userStaff) {
options.content = MentionsParser.parse(options.content);
}
return (
<TicketEvent {...options} author={(!_.isEmpty(options.author)) ? options.author : this.props.ticket.author} key={index} />
);

View File

@ -7,6 +7,7 @@ import ArticlesActions from 'actions/articles-actions';
import SessionStore from 'lib-app/session-store';
import i18n from 'lib-app/i18n';
import API from 'lib-app/api-call';
import MentionsParser from 'lib-app/mentions-parser';
import DateTransformer from 'lib-core/date-transformer';
import AreYouSure from 'app-components/are-you-sure';
@ -76,7 +77,7 @@ class AdminPanelViewArticle extends React.Component {
<Header title={article.title}/>
<div className="admin-panel-view-article__article-content">
<div dangerouslySetInnerHTML={{__html: article.content}}/>
<div dangerouslySetInnerHTML={{__html: MentionsParser.parse(article.content)}}/>
</div>
<div className="admin-panel-view-article__last-edited">
{i18n('LAST_EDITED_IN', {date: DateTransformer.transformToString(article.lastEdited)})}

View File

@ -0,0 +1,53 @@
const PARSING_TEXT = 0;
const PARSING_MENTION = 1;
class MentionsParser {
parse(text) {
let parsingLink = false;
let parsingType = PARSING_TEXT;
let parsingSegment = '';
let ans = '';
for(let index = 0; index < text.length; ++index){
let character = text[index];
if(character == '#'){
ans += this.compileSegment(parsingSegment, parsingType);
parsingLink = true;
parsingType = PARSING_MENTION;
parsingSegment = '';
} else if(!this.isAlphanumeric(character) && parsingLink){
ans += this.compileSegment(parsingSegment, parsingType);
parsingLink = false;
parsingType = PARSING_TEXT;
parsingSegment = character;
} else {
parsingSegment += character;
}
}
ans += this.compileSegment(parsingSegment, parsingType);
return ans;
}
isAlphanumeric(string){
return /[a-zA-Z0-9]/.test(string);
}
compileSegment(segment, parsingType){
switch(parsingType){
case PARSING_TEXT:
return segment;
case PARSING_MENTION:
return '<a href=' + root + '/admin/panel/tickets/view-ticket/' + segment + '>#' + segment + '</a>';
default:
return '';
}
}
};
export default new MentionsParser;