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).
pull/21833/head
David Baker 2017-02-02 11:45:21 +00:00
parent 72e5b2235d
commit 63e47d8677
1 changed files with 9 additions and 17 deletions

View File

@ -27,8 +27,6 @@ export default class Markdown {
const parser = new commonmark.Parser(); const parser = new commonmark.Parser();
this.parsed = parser.parse(this.input); this.parsed = parser.parse(this.input);
this.renderer = new commonmark.HtmlRenderer({safe: false});
} }
isPlainText() { isPlainText() {
@ -58,9 +56,10 @@ export default class Markdown {
} }
toHTML() { toHTML() {
const real_paragraph = this.renderer.paragraph; const renderer = new commonmark.HtmlRenderer({safe: false});
const real_paragraph = renderer.paragraph;
this.renderer.paragraph = function(node, entering) { renderer.paragraph = function(node, entering) {
// If there is only one top level node, just return the // If there is only one top level node, just return the
// bare text: it's a single line of text and so should be // bare text: it's a single line of text and so should be
// 'inline', rather than unnecessarily wrapped in its own // 'inline', rather than unnecessarily wrapped in its own
@ -75,11 +74,7 @@ export default class Markdown {
} }
}; };
var rendered = this.renderer.render(this.parsed); return renderer.render(this.parsed);
this.renderer.paragraph = real_paragraph;
return rendered;
} }
/* /*
@ -89,17 +84,18 @@ export default class Markdown {
* (to fix https://github.com/vector-im/riot-web/issues/2870) * (to fix https://github.com/vector-im/riot-web/issues/2870)
*/ */
toPlaintext() { toPlaintext() {
const real_paragraph = this.renderer.paragraph; const renderer = new commonmark.HtmlRenderer({safe: false});
const real_paragraph = renderer.paragraph;
// The default `out` function only sends the input through an XML // The default `out` function only sends the input through an XML
// escaping function, which causes messages to be entity encoded, // escaping function, which causes messages to be entity encoded,
// which we don't want in this case. // which we don't want in this case.
this.renderer.out = function(s) { renderer.out = function(s) {
// The `lit` function adds a string literal to the output buffer. // The `lit` function adds a string literal to the output buffer.
this.lit(s); this.lit(s);
}; };
this.renderer.paragraph = function(node, entering) { renderer.paragraph = function(node, entering) {
// If there is only one top level node, just return the // If there is only one top level node, just return the
// bare text: it's a single line of text and so should be // bare text: it's a single line of text and so should be
// 'inline', rather than unnecessarily wrapped in its own // 'inline', rather than unnecessarily wrapped in its own
@ -117,10 +113,6 @@ export default class Markdown {
} }
}; };
var rendered = this.renderer.render(this.parsed); return renderer.render(this.parsed);
this.renderer.paragraph = real_paragraph;
return rendered;
} }
} }