Skip to content

Commit

Permalink
Merge pull request #7852 from RocketChat/add-marked
Browse files Browse the repository at this point in the history
[NEW] Add markdown parser "marked"
  • Loading branch information
rodrigok authored Aug 24, 2017
2 parents b80f5fc + 9aad03a commit 4953bb6
Show file tree
Hide file tree
Showing 20 changed files with 520 additions and 201 deletions.
2 changes: 1 addition & 1 deletion .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ rocketchat:logger@0.0.1
rocketchat:login-token@1.0.0
rocketchat:mailer@0.0.1
rocketchat:mapview@0.0.1
rocketchat:markdown@0.0.1
rocketchat:markdown@0.0.2
rocketchat:mentions@0.0.1
rocketchat:mentions-flextab@0.0.1
rocketchat:message-attachments@0.0.1
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@
"wolfy87-eventemitter": "^5.2.2"
}
}

3 changes: 1 addition & 2 deletions packages/rocketchat-autotranslate/server/autotranslate.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ class AutoTranslate {
let count = message.tokens.length;

message.html = message.msg;
RocketChat.MarkdownCode.handle_codeblocks(message);
RocketChat.MarkdownCode.handle_inlinecode(message);
message = RocketChat.Markdown.parse(message);
message.msg = message.html;

for (const tokenIndex in message.tokens) {
Expand Down
8 changes: 8 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,17 @@
"MapView_GMapsAPIKey_Description": "This can be obtained from the Google Developers Console for free.",
"Mark_as_read": "Mark as read",
"Mark_as_unread": "Mark as unread",
"Markdown_Parser": "Markdown Parser",
"Original": "Original",
"Markdown_Headers": "Allow Markdown headers in messages",
"Markdown_SupportSchemesForLink": "Markdown Support Schemes for Link",
"Markdown_SupportSchemesForLink_Description": "Comma-separated list of allowed schemes",
"Markdown_Marked_GFM": "Enable Marked GFM",
"Markdown_Marked_Tables": "Enable Marked Tables",
"Markdown_Marked_Breaks": "Enable Marked Breaks",
"Markdown_Marked_Pedantic": "Enable Marked Pedantic",
"Markdown_Marked_SmartLists": "Enable Marked Smart Lists",
"Markdown_Marked_Smartypants": "Enable Marked Smartypants",
"Max_length_is": "Max length is %s",
"Members_List": "Members List",
"Mentions": "Mentions",
Expand Down
12 changes: 10 additions & 2 deletions packages/rocketchat-i18n/i18n/ja.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,17 @@
"Managing_assets": "資産を管理します",
"Managing_integrations": "統合管理",
"Mark_as_read": "既読にする",
"Markdown_Headers": "Markdown ヘッダー",
"Markdown_SupportSchemesForLink": "Markdown リンクでサポートするスキーマ",
"Markdown_Parser": "Markdown パーサー",
"Original": "オリジナル",
"Markdown_Headers": "ヘッダーを有効にする",
"Markdown_SupportSchemesForLink": "リンクでサポートするスキーマリスト",
"Markdown_SupportSchemesForLink_Description": "許可するスキーマをカンマ区切りで記述してください。",
"Markdown_Marked_GFM": "Marked GFM を有効にする",
"Markdown_Marked_Tables": "Marked Tables を有効にする",
"Markdown_Marked_Breaks": "Marked Breaks を有効にする",
"Markdown_Marked_Pedantic": "Marked Pedantic を有効にする",
"Markdown_Marked_SmartLists": "Marked Smart Lists を有効にする",
"Markdown_Marked_Smartypants": "Marked Smartypants を有効にする",
"Members_List": "メンバーリスト",
"Mentions": "メンション",
"Mentions_default": "メンション (デフォルト)",
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-markdown/.npm/package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions packages/rocketchat-markdown/.npm/package/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.

You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.
14 changes: 14 additions & 0 deletions packages/rocketchat-markdown/.npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 21 additions & 81 deletions packages/rocketchat-markdown/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,32 @@
* Markdown is a named function that will parse markdown syntax
* @param {Object} message - The message object
*/
import { Meteor } from 'meteor/meteor';
import { Blaze } from 'meteor/blaze';
import { RocketChat } from 'meteor/rocketchat:lib';

import { marked } from './parser/marked/marked.js';
import { original } from './parser/original/original.js';

const parsers = {
original,
marked
};

class MarkdownClass {
parse(text) {
return this.parseNotEscaped(_.escapeHTML(text));
const message = {
html: _.escapeHTML(text)
};
return this.parseNotEscaped(message).html;
}

parseNotEscaped(msg, message) {
if (message && message.tokens == null) {
message.tokens = [];
parseNotEscaped(message) {
const parser = RocketChat.settings.get('Markdown_Parser');
if (typeof parsers[parser] === 'function') {
return parsers[parser](message);
}

const schemes = RocketChat.settings.get('Markdown_SupportSchemesForLink').split(',').join('|');

if (RocketChat.settings.get('Markdown_Headers')) {
// Support # Text for h1
msg = msg.replace(/^# (([\S\w\d-_\/\*\.,\\][ \u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]?)+)/gm, '<h1>$1</h1>');

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

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

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

// Support *text* to make bold
msg = msg.replace(/(^|&gt;|[ >_~`])\*{1,2}([^\*\r\n]+)\*{1,2}([<_~`]|\B|\b|$)/gm, '$1<span class="copyonly">*</span><strong>$2</strong><span class="copyonly">*</span>$3');

// Support _text_ to make italics
msg = msg.replace(/(^|&gt;|[ >*~`])\_([^\_\r\n]+)\_([<*~`]|\B|\b|$)/gm, '$1<span class="copyonly">_</span><em>$2</em><span class="copyonly">_</span>$3');

// Support ~text~ to strike through text
msg = msg.replace(/(^|&gt;|[ >_*`])\~{1,2}([^~\r\n]+)\~{1,2}([<_*`]|\B|\b|$)/gm, '$1<span class="copyonly">~</span><strike>$2</strike><span class="copyonly">~</span>$3');

// Support for block quote
// >>>
// Text
// <<<
msg = msg.replace(/(?:&gt;){3}\n+([\s\S]*?)\n+(?:&lt;){3}/g, '<blockquote class="background-transparent-darker-before"><span class="copyonly">&gt;&gt;&gt;</span>$1<span class="copyonly">&lt;&lt;&lt;</span></blockquote>');

// Support >Text for quote
msg = msg.replace(/^&gt;(.*)$/gm, '<blockquote class="background-transparent-darker-before"><span class="copyonly">&gt;</span>$1</blockquote>');

// Remove white-space around blockquote (prevent <br>). Because blockquote is block element.
msg = msg.replace(/\s*<blockquote class="background-transparent-darker-before">/gm, '<blockquote class="background-transparent-darker-before">');
msg = msg.replace(/<\/blockquote>\s*/gm, '</blockquote>');

// Remove new-line between blockquotes.
msg = msg.replace(/<\/blockquote>\n<blockquote/gm, '</blockquote><blockquote');

// Support ![alt text](http://image url)
msg = msg.replace(new RegExp(`!\\[([^\\]]+)\\]\\(((?:${ schemes }):\\/\\/[^\\)]+)\\)`, 'gm'), function(match, title, url) {
const target = url.indexOf(Meteor.absoluteUrl()) === 0 ? '' : '_blank';
const html = `<a href="${ _.escapeHTML(url) }" title="${ _.escapeHTML(title) }" target="${ _.escapeHTML(target) }" rel="noopener noreferrer"><div class="inline-image" style="background-image: url(${ _.escapeHTML(url) });"></div></a>`;

if (message && message.tokens) {
const token = `=!=${ Random.id() }=!=`;

message.tokens.push({
token,
text: html
});

return token;
}

return html;
});

// Support [Text](http://link)
msg = msg.replace(new RegExp(`\\[([^\\]]+)\\]\\(((?:${ schemes }):\\/\\/[^\\)]+)\\)`, 'gm'), function(match, title, url) {
const target = url.indexOf(Meteor.absoluteUrl()) === 0 ? '' : '_blank';
return `<a href="${ _.escapeHTML(url) }" target="${ _.escapeHTML(target) }" rel="noopener noreferrer">${ _.escapeHTML(title) }</a>`;
});

// Support <http://link|Text>
msg = msg.replace(new RegExp(`(?:<|&lt;)((?:${ schemes }):\\/\\/[^\\|]+)\\|(.+?)(?=>|&gt;)(?:>|&gt;)`, 'gm'), (match, url, title) => {
const target = url.indexOf(Meteor.absoluteUrl()) === 0 ? '' : '_blank';
return `<a href="${ _.escapeHTML(url) }" target="${ _.escapeHTML(target) }" rel="noopener noreferrer">${ _.escapeHTML(title) }</a>`;
});

if (typeof window !== 'undefined' && window !== null ? window.rocketDebug : undefined) { console.log('Markdown', msg); }

return msg;
return parsers['original'](message);
}
}

Expand All @@ -97,7 +37,7 @@ RocketChat.Markdown = Markdown;
// renderMessage already did html escape
const MarkdownMessage = (message) => {
if (_.trim(message != null ? message.html : undefined)) {
message.html = Markdown.parseNotEscaped(message.html, message);
message = Markdown.parseNotEscaped(message);
}

return message;
Expand Down
99 changes: 0 additions & 99 deletions packages/rocketchat-markdown/markdowncode.js

This file was deleted.

10 changes: 6 additions & 4 deletions packages/rocketchat-markdown/package.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
Package.describe({
name: 'rocketchat:markdown',
version: '0.0.1',
version: '0.0.2',
summary: 'Message pre-processor that will process selected markdown notations',
git: ''
});

Npm.depends({
'marked': '0.3.6'
});

Package.onUse(function(api) {
api.use([
'ecmascript',
'underscore',
'templating',
'underscorestring:underscore.string',
'rocketchat:lib'
]);

api.addFiles('settings.js', 'server');
api.addFiles('markdown.js');
api.addFiles('markdowncode.js');
api.mainModule('markdown.js');
});
Loading

0 comments on commit 4953bb6

Please sign in to comment.