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

Commit 63e47d8

Browse files
committed
Make ourselves a new rendered each time
Rather than keeping one in memory, abusing it in different ways each time and then craefully putting it back the way it was (and in one case, failing, because we forgot to put the `out` method back).
1 parent 72e5b22 commit 63e47d8

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/Markdown.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export default class Markdown {
2727

2828
const parser = new commonmark.Parser();
2929
this.parsed = parser.parse(this.input);
30-
31-
this.renderer = new commonmark.HtmlRenderer({safe: false});
3230
}
3331

3432
isPlainText() {
@@ -58,9 +56,10 @@ export default class Markdown {
5856
}
5957

6058
toHTML() {
61-
const real_paragraph = this.renderer.paragraph;
59+
const renderer = new commonmark.HtmlRenderer({safe: false});
60+
const real_paragraph = renderer.paragraph;
6261

63-
this.renderer.paragraph = function(node, entering) {
62+
renderer.paragraph = function(node, entering) {
6463
// If there is only one top level node, just return the
6564
// bare text: it's a single line of text and so should be
6665
// 'inline', rather than unnecessarily wrapped in its own
@@ -75,11 +74,7 @@ export default class Markdown {
7574
}
7675
};
7776

78-
var rendered = this.renderer.render(this.parsed);
79-
80-
this.renderer.paragraph = real_paragraph;
81-
82-
return rendered;
77+
return renderer.render(this.parsed);
8378
}
8479

8580
/*
@@ -89,17 +84,18 @@ export default class Markdown {
8984
* (to fix https://github.com/vector-im/riot-web/issues/2870)
9085
*/
9186
toPlaintext() {
92-
const real_paragraph = this.renderer.paragraph;
87+
const renderer = new commonmark.HtmlRenderer({safe: false});
88+
const real_paragraph = renderer.paragraph;
9389

9490
// The default `out` function only sends the input through an XML
9591
// escaping function, which causes messages to be entity encoded,
9692
// which we don't want in this case.
97-
this.renderer.out = function(s) {
93+
renderer.out = function(s) {
9894
// The `lit` function adds a string literal to the output buffer.
9995
this.lit(s);
10096
};
10197

102-
this.renderer.paragraph = function(node, entering) {
98+
renderer.paragraph = function(node, entering) {
10399
// If there is only one top level node, just return the
104100
// bare text: it's a single line of text and so should be
105101
// 'inline', rather than unnecessarily wrapped in its own
@@ -117,10 +113,6 @@ export default class Markdown {
117113
}
118114
};
119115

120-
var rendered = this.renderer.render(this.parsed);
121-
122-
this.renderer.paragraph = real_paragraph;
123-
124-
return rendered;
116+
return renderer.render(this.parsed);
125117
}
126118
}

0 commit comments

Comments
 (0)