Parse once and re-use the parsed output

Rather than re-parsing the same output in each function
This commit is contained in:
David Baker 2017-02-02 11:34:39 +00:00
parent fc90ed1c3a
commit 72e5b2235d

View file

@ -24,7 +24,10 @@ import commonmark from 'commonmark';
export default class Markdown {
constructor(input) {
this.input = input;
this.parser = new commonmark.Parser();
const parser = new commonmark.Parser();
this.parsed = parser.parse(this.input);
this.renderer = new commonmark.HtmlRenderer({safe: false});
}
@ -49,8 +52,7 @@ export default class Markdown {
dummy_renderer.softbreak = function(t) { return t; };
dummy_renderer.paragraph = function(t) { return t; };
const dummy_parser = new commonmark.Parser();
dummy_renderer.render(dummy_parser.parse(this.input));
dummy_renderer.render(this.parsed);
return is_plain;
}
@ -73,8 +75,7 @@ export default class Markdown {
}
};
var parsed = this.parser.parse(this.input);
var rendered = this.renderer.render(parsed);
var rendered = this.renderer.render(this.parsed);
this.renderer.paragraph = real_paragraph;
@ -116,8 +117,7 @@ export default class Markdown {
}
};
var parsed = this.parser.parse(this.input);
var rendered = this.renderer.render(parsed);
var rendered = this.renderer.render(this.parsed);
this.renderer.paragraph = real_paragraph;