diff --git a/app/javascript/dashboard/assets/scss/widgets/_conversation-view.scss b/app/javascript/dashboard/assets/scss/widgets/_conversation-view.scss index b67932e6d..6c892f8fb 100644 --- a/app/javascript/dashboard/assets/scss/widgets/_conversation-view.scss +++ b/app/javascript/dashboard/assets/scss/widgets/_conversation-view.scss @@ -422,11 +422,27 @@ } } - pre code { - background: var(--color-background); - } - p:last-child { margin-bottom: 0; } } + +.bubble { + pre { + background: var(--b-50); + border-radius: var(--border-radius-normal); + border: 1px solid var(--color-border); + display: block; + margin: var(--space-small) 0; + padding: var(--space-slab); + } + + pre code { + display: block; + padding: 0; + } + + code { + background: var(--b-50); + } +} diff --git a/app/javascript/shared/helpers/MessageFormatter.js b/app/javascript/shared/helpers/MessageFormatter.js index c4d0bb62e..59e1173ac 100644 --- a/app/javascript/shared/helpers/MessageFormatter.js +++ b/app/javascript/shared/helpers/MessageFormatter.js @@ -1,57 +1,49 @@ -import { marked } from 'marked'; -import DOMPurify from 'dompurify'; -import { escapeHtml, afterSanitizeAttributes } from './HTMLSanitizer'; +import mila from 'markdown-it-link-attributes'; +import mentionPlugin from './markdownIt/link'; +const md = require('markdown-it')({ + html: false, + xhtmlOut: true, + breaks: true, + langPrefix: 'language-', + linkify: true, + typographer: true, + quotes: '\u201c\u201d\u2018\u2019', + maxNesting: 20, +}) + .use(mentionPlugin) + .use(mila, { + attrs: { + class: 'link', + rel: 'noreferrer noopener nofollow', + target: '_blank', + }, + }); const TWITTER_USERNAME_REGEX = /(^|[^@\w])@(\w{1,15})\b/g; -const TWITTER_USERNAME_REPLACEMENT = - '$1@$2'; - +const TWITTER_USERNAME_REPLACEMENT = '$1[@$2](http://twitter.com/$2)'; const TWITTER_HASH_REGEX = /(^|\s)#(\w+)/g; -const TWITTER_HASH_REPLACEMENT = - '$1#$2'; - -const USER_MENTIONS_REGEX = /mention:\/\/(user|team)\/(\d+)\/(.+)/gm; +const TWITTER_HASH_REPLACEMENT = '$1[#$2](https://twitter.com/hashtag/$2)'; class MessageFormatter { constructor(message, isATweet = false, isAPrivateNote = false) { - this.message = DOMPurify.sanitize(escapeHtml(message || '')); + this.message = message || ''; this.isAPrivateNote = isAPrivateNote; this.isATweet = isATweet; - this.marked = marked; - - const renderer = { - heading(text) { - return `${text}`; - }, - link(url, title, text) { - const mentionRegex = new RegExp(USER_MENTIONS_REGEX); - if (url.match(mentionRegex)) { - return `${text}`; - } - return `${text}`; - }, - }; - this.marked.use({ renderer }); } formatMessage() { + let updatedMessage = this.message; if (this.isATweet && !this.isAPrivateNote) { - const withUserName = this.message.replace( + updatedMessage = updatedMessage.replace( TWITTER_USERNAME_REGEX, TWITTER_USERNAME_REPLACEMENT ); - const withHash = withUserName.replace( + updatedMessage = updatedMessage.replace( TWITTER_HASH_REGEX, TWITTER_HASH_REPLACEMENT ); - const markedDownOutput = marked(withHash); - return markedDownOutput; } - DOMPurify.addHook('afterSanitizeAttributes', afterSanitizeAttributes); - return DOMPurify.sanitize( - marked(this.message, { breaks: true, gfm: true }) - ); + return md.render(updatedMessage); } get formattedMessage() { diff --git a/app/javascript/shared/helpers/markdownIt/link.js b/app/javascript/shared/helpers/markdownIt/link.js new file mode 100644 index 000000000..c5300391b --- /dev/null +++ b/app/javascript/shared/helpers/markdownIt/link.js @@ -0,0 +1,69 @@ +// Process [@mention](mention://user/1/Pranav) +const USER_MENTIONS_REGEX = /mention:\/\/(user|team)\/(\d+)\/(.+)/gm; + +const buildMentionTokens = () => (state, silent) => { + var label; + var labelEnd; + var labelStart; + var pos; + var res; + var token; + var href = ''; + var max = state.posMax; + + if (state.src.charCodeAt(state.pos) !== 0x5b /* [ */) { + return false; + } + + labelStart = state.pos + 1; + labelEnd = state.md.helpers.parseLinkLabel(state, state.pos, true); + + // parser failed to find ']', so it's not a valid link + if (labelEnd < 0) { + return false; + } + + label = state.src.slice(labelStart, labelEnd); + pos = labelEnd + 1; + + if (pos < max && state.src.charCodeAt(pos) === 0x28 /* ( */) { + pos += 1; + res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax); + if (res.ok) { + href = state.md.normalizeLink(res.str); + if (state.md.validateLink(href)) { + pos = res.pos; + } else { + href = ''; + } + } + pos += 1; + } + + if (!href.match(new RegExp(USER_MENTIONS_REGEX))) { + return false; + } + + if (!silent) { + state.pos = labelStart; + state.posMax = labelEnd; + + token = state.push('mention', ''); + token.href = href; + token.content = label; + } + + state.pos = pos; + state.posMax = max; + + return true; +}; + +const renderMentions = () => (tokens, idx) => { + return `${tokens[idx].content}`; +}; + +export default function mentionPlugin(md) { + md.renderer.rules.mention = renderMentions(md); + md.inline.ruler.before('link', 'mention', buildMentionTokens(md)); +} diff --git a/app/javascript/shared/helpers/specs/MessageFormatter.spec.js b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js index 1f2209b71..60c875c8a 100644 --- a/app/javascript/shared/helpers/specs/MessageFormatter.spec.js +++ b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js @@ -6,14 +6,14 @@ describe('#MessageFormatter', () => { const message = 'Chatwoot is an opensource tool. [Chatwoot](https://www.chatwoot.com)'; expect(new MessageFormatter(message).formattedMessage).toMatch( - '
Chatwoot is an opensource tool. Chatwoot
' + 'Chatwoot is an opensource tool. Chatwoot
' ); }); it('should format correctly', () => { const message = 'Chatwoot is an opensource tool. https://www.chatwoot.com'; expect(new MessageFormatter(message).formattedMessage).toMatch( - 'Chatwoot is an opensource tool. https://www.chatwoot.com
' + 'Chatwoot is an opensource tool. https://www.chatwoot.com
' ); }); }); @@ -22,7 +22,8 @@ describe('#MessageFormatter', () => { it('should format correctly', () => { const message = '### opensource \n ## tool'; expect(new MessageFormatter(message).formattedMessage).toMatch( - 'opensourcetool' + `@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername
' + '@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername
' ); }); @@ -48,7 +49,7 @@ describe('#MessageFormatter', () => { expect( new MessageFormatter(message, true, false).formattedMessage ).toMatch( - '#chatwootapp is an opensource tool
' + '#chatwootapp is an opensource tool
' ); }); }); @@ -90,7 +91,8 @@ describe('#MessageFormatter', () => { const message = '[xssLink](javascript:alert(document.cookie))\n[normalLink](https://google.com)**I am a bold text paragraph**'; expect(new MessageFormatter(message).formattedMessage).toMatch( - 'xssLink
normalLinkI am a bold text paragraph
[xssLink](javascript:alert(document.cookie))
+normalLinkI am a bold text paragraph