From 073a93d2905fe4470dfd66ee120f85f02ca44cb7 Mon Sep 17 00:00:00 2001 From: Maki Nishifuji Date: Tue, 14 Mar 2017 22:53:28 +0900 Subject: [PATCH] Port comments --- .../parser/original/code.js | 6 ++++ .../parser/original/original.js | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/rocketchat-markdown/parser/original/code.js b/packages/rocketchat-markdown/parser/original/code.js index b7445b5c819..4f16d8c320b 100644 --- a/packages/rocketchat-markdown/parser/original/code.js +++ b/packages/rocketchat-markdown/parser/original/code.js @@ -7,6 +7,7 @@ import { _ } from 'meteor/underscore'; import hljs from 'highlight.js'; const inlineCode = (message) => { + // Support `text` message.html = message.html.replace(/(^|>|[ >_*~])\`([^`\r\n]+)\`([<_*~]|\B|\b|$)/gm, (match, p1, p2, p3) => { const token = `=&=${Random.id()}=&=`; message.tokens.push({ @@ -20,8 +21,10 @@ const inlineCode = (message) => { }; const blockCode = (message) => { + // Count occurencies of ``` const count = (message.html.match(/```/g) || []).length; + // Check if we need to add a final ``` if (!count) { return message; } @@ -34,11 +37,13 @@ const blockCode = (message) => { // Separate text in code blocks and non code blocks let msgParts = message.html.replace(/
/gm, '\n').split(/^.*(```(?:[a-zA-Z]+)?(?:(?:.|\n)*?)```)(.*\n?)$/gm); msgParts = msgParts.map((part) => { + // Verify if this part is code const codeMatch = part.match(/^```(\w*[\n\ ]?)([\s\S]*?)```+?$/); if (null == codeMatch) { return part; } + // Process highlight if this part is code const singleLine = codeMatch[0].indexOf('\n') === -1; let code = null, lang = null; if (singleLine) { @@ -67,6 +72,7 @@ const blockCode = (message) => { return token; }); + // Re-mount message message.html = msgParts.join(''); return message; diff --git a/packages/rocketchat-markdown/parser/original/original.js b/packages/rocketchat-markdown/parser/original/original.js index 7aaa1e5f1fa..f6fdba3fc15 100644 --- a/packages/rocketchat-markdown/parser/original/original.js +++ b/packages/rocketchat-markdown/parser/original/original.js @@ -20,34 +20,65 @@ export const original = (message) => { } const schemes = RocketChat.settings.get('Markdown_SupportSchemesForLink').split(',').join('|'); + + // Support ![alt text](http://image url) msg = msg.replace(new RegExp(`!\\[([^\\]]+)\\]\\(((?:${schemes}):\\/\\/[^\\)]+)\\)`, 'gm'), (match, title, url) => { const target = url.indexOf(Meteor.absoluteUrl()) === 0 ? '' : '_blank'; return `
`; }); + + // Support [Text](http://link) msg = msg.replace(new RegExp(`\\[([^\\]]+)\\]\\(((?:${schemes}):\\/\\/[^\\)]+)\\)`, 'gm'), (match, title, url) => { const target = url.indexOf(Meteor.absoluteUrl()) === 0 ? '' : '_blank'; return `${title}`; }); + + // Support msg = msg.replace(new RegExp(`(?:<|<)((?:${schemes}):\\/\\/[^\\|]+)\\|(.+?)(?=>|>)(?:>|>)`, 'gm'), (match, url, title) => { const target = url.indexOf(Meteor.absoluteUrl()) === 0 ? '' : '_blank'; return `${title}`; }); if (RocketChat.settings.get('Markdown_Headers')) { + // Support # Text for h1 msg = msg.replace(/(?:^|\n)# (([\S\w\d-_\/\*\.,\\][ \u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]?)+)\n?/gm, '

$1

'); + + // Support # Text for h2 msg = msg.replace(/(?:^|\n)## (([\S\w\d-_\/\*\.,\\][ \u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]?)+)\n?/gm, '

$1

'); + + // Support # Text for h3 msg = msg.replace(/(?:^|\n)### (([\S\w\d-_\/\*\.,\\][ \u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]?)+)\n?/gm, '

$1

'); + + // Support # Text for h4 msg = msg.replace(/(?:^|\n)#### (([\S\w\d-_\/\*\.,\\][ \u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]?)+)\n?/gm, '

$1

'); } + // Support *text* to make bold msg = msg.replace(/(^|>|[ >_~`])\*{1,2}([^\*\r\n]+)\*{1,2}([<_~`]|\B|\b|$)/gm, '$1*$2*$3'); + + // Support _text_ to make italics msg = msg.replace(/(^|>|[ >*~`])\_([^\_\r\n]+)\_([<*~`]|\B|\b|$)/gm, '$1_$2_$3'); + + // Support ~text~ to strike through text msg = msg.replace(/(^|>|[ >_*`])\~{1,2}([^~\r\n]+)\~{1,2}([<_*`]|\B|\b|$)/gm, '$1~$2~$3'); + + // Support for block quote + // >>> + // Text + // <<< msg = msg.replace(/(?:>){3}\n+([\s\S]*?)\n+(?:<){3}/g, '
>>>$1<<<
'); + + // Support >Text for quote msg = msg.replace(/^>(.*)$/gm, '
>$1
'); + + // Remove white-space around blockquote (prevent
). Because blockquote is block element. msg = msg.replace(/\s*
/gm, '
'); msg = msg.replace(/<\/blockquote>\s*/gm, '
'); + + // Remove new-line between blockquotes. msg = msg.replace(/<\/blockquote>\n
'); if (!_.isString(message)) { @@ -56,6 +87,7 @@ export const original = (message) => { message = msg; } + // Support code blocks and inline code message = code(message); if (window && window.rocketDebug) {