Merge pull request #1270 from matrix-org/luke/fix-md-unescaped-tokens
MD-escape URLs/alises/user IDs prior to parsing markdown
This commit is contained in:
commit
b06922b6fc
1 changed files with 20 additions and 1 deletions
|
@ -55,6 +55,25 @@ function is_multi_line(node) {
|
||||||
return par.firstChild != par.lastChild;
|
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
|
* Class that wraps commonmark, adding the ability to see whether
|
||||||
* a given message actually uses any markdown syntax or whether
|
* a given message actually uses any markdown syntax or whether
|
||||||
|
@ -62,7 +81,7 @@ function is_multi_line(node) {
|
||||||
*/
|
*/
|
||||||
export default class Markdown {
|
export default class Markdown {
|
||||||
constructor(input) {
|
constructor(input) {
|
||||||
this.input = input;
|
this.input = linkifyMarkdown(input);
|
||||||
|
|
||||||
const parser = new commonmark.Parser();
|
const parser = new commonmark.Parser();
|
||||||
this.parsed = parser.parse(this.input);
|
this.parsed = parser.parse(this.input);
|
||||||
|
|
Loading…
Reference in a new issue