Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
MD-escape URLs/alises/user IDs prior to parsing markdown
Browse files Browse the repository at this point in the history
So that MD characters in them do not result in formatting being applied.

Fixes element-hq/element-web#3428
Fixes element-hq/element-web#4674
  • Loading branch information
Luke Barnard committed Aug 4, 2017
1 parent ee5fc12 commit ee18ddb
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,33 @@ function is_multi_line(node) {
return par.firstChild != par.lastChild;
}

import linkifyMatrix from './linkify-matrix';
import * as linkify from 'linkifyjs';
linkifyMatrix(linkify);

// Thieved from draft-js-export-markdown
function escapeMarkdown(s) {
return s.replace(/[*_`]/g, '\\$&');
}

// Replace URLs, room aliases and user IDs with md-escaped URLs
function linkifyMarkdown(s) {
const links = linkify.find(s);
links.forEach((l) => {
// This may replace several instances of `l.value` at once, but that's OK
s = s.replace(l.value, escapeMarkdown(l.value));
});
return s;
}

/**
* Class that wraps commonmark, adding the ability to see whether
* a given message actually uses any markdown syntax or whether
* it's plain text.
*/
export default class Markdown {
constructor(input) {
this.input = input;
this.input = linkifyMarkdown(input);

const parser = new commonmark.Parser();
this.parsed = parser.parse(this.input);
Expand Down

0 comments on commit ee18ddb

Please sign in to comment.