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

Improve link parser using tokens #7615

Merged
merged 1 commit into from Jul 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/rocketchat-markdown/markdown.js
Expand Up @@ -8,7 +8,11 @@ class MarkdownClass {
return this.parseNotEscaped(_.escapeHTML(text));
}

parseNotEscaped(msg) {
parseNotEscaped(msg, message) {
if (message && message.tokens == null) {
message.tokens = [];
}

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

if (RocketChat.settings.get('Markdown_Headers')) {
Expand Down Expand Up @@ -53,7 +57,20 @@ class MarkdownClass {
// 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';
return `<a href="${ _.escapeHTML(url) }" title="${ _.escapeHTML(title) }" target="${ _.escapeHTML(target) }" ref="noopener noreferrer"><div class="inline-image" style="background-image: url(${ _.escapeHTML(url) });"></div></a>`;
const html = `<a href="${ _.escapeHTML(url) }" title="${ _.escapeHTML(title) }" target="${ _.escapeHTML(target) }" ref="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)
Expand All @@ -80,7 +97,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.html = Markdown.parseNotEscaped(message.html, message);
}

return message;
Expand Down