Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add markdown parser "marked" #7852

Merged
merged 28 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4079139
Add new markdown parser `marked`.
Feb 24, 2017
54bcb9d
Do not use token for inline code
Feb 24, 2017
8c2613b
revert roomName code
Feb 24, 2017
9e16354
fix codacy issue
Feb 24, 2017
e819e57
Merge remote-tracking branch 'upstream/develop' into add-marked
Mar 3, 2017
e67f73d
var to const
Mar 3, 2017
67e3ce6
Merge remote-tracking branch 'upstream/develop' into add-marked
Mar 4, 2017
4467384
Delay the option setting for the rendering phase
Mar 5, 2017
2d20cda
Merge branch develop into add-marked
Mar 6, 2017
acdbadd
Merge branch develop into add-marked
Mar 14, 2017
073a93d
Port comments
Mar 14, 2017
e358eac
remove markdowncode.coffee (move parser dir)
Mar 15, 2017
133b135
Merge upstream/develop into add-marked
Mar 18, 2017
887a3d4
Merge upstream/develop into add-marked
Mar 25, 2017
6d64060
fix codacy issues
Mar 25, 2017
40bab20
Merge upstream/develop into add-marked
Apr 6, 2017
d0b9667
Merge and resolve conflicts upstream/develop into add-marked
Apr 22, 2017
f3fd15a
Merge remote-tracking branch 'upstream/develop' into add-marked
May 8, 2017
05dd0db
Merge branch upstream/develop into add-marked
May 19, 2017
92899fd
resolve conflict
May 26, 2017
d2867c3
Merge remote-tracking branch 'upstream/develop' into add-marked
May 26, 2017
0e88ac2
Port markdown code from develop
May 27, 2017
01931cb
Merge remote-tracking branch 'upstream/develop' into add-marked
May 27, 2017
3e467cb
Resolve conflict
Jun 1, 2017
1d051dd
Resolve conflicts
Aug 22, 2017
6e813d6
Fix code style
Aug 22, 2017
2251e9c
Try force travis build
rodrigok Aug 23, 2017
9aad03a
Filter markdown shortcuts
sampaiodiego Aug 23, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -117,3 +117,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 @@ -1010,9 +1010,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
14 changes: 11 additions & 3 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 Expand Up @@ -1237,4 +1245,4 @@
"Your_mail_was_sent_to_s": "メールは %s へを送信されました",
"Your_password_is_wrong": "パスワードが間違っています!",
"Your_push_was_sent_to_s_devices": "プッシュ通知が %s 台のデバイスへ送信されました"
}
}
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