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

Commit b06922b

Browse files
authored
Merge pull request #1270 from matrix-org/luke/fix-md-unescaped-tokens
MD-escape URLs/alises/user IDs prior to parsing markdown
2 parents ee5fc12 + ee18ddb commit b06922b

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Markdown.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,33 @@ function is_multi_line(node) {
5555
return par.firstChild != par.lastChild;
5656
}
5757

58+
import linkifyMatrix from './linkify-matrix';
59+
import * as linkify from 'linkifyjs';
60+
linkifyMatrix(linkify);
61+
62+
// Thieved from draft-js-export-markdown
63+
function escapeMarkdown(s) {
64+
return s.replace(/[*_`]/g, '\\$&');
65+
}
66+
67+
// Replace URLs, room aliases and user IDs with md-escaped URLs
68+
function linkifyMarkdown(s) {
69+
const links = linkify.find(s);
70+
links.forEach((l) => {
71+
// This may replace several instances of `l.value` at once, but that's OK
72+
s = s.replace(l.value, escapeMarkdown(l.value));
73+
});
74+
return s;
75+
}
76+
5877
/**
5978
* Class that wraps commonmark, adding the ability to see whether
6079
* a given message actually uses any markdown syntax or whether
6180
* it's plain text.
6281
*/
6382
export default class Markdown {
6483
constructor(input) {
65-
this.input = input;
84+
this.input = linkifyMarkdown(input);
6685

6786
const parser = new commonmark.Parser();
6887
this.parsed = parser.parse(this.input);

0 commit comments

Comments
 (0)