From 72e5b2235dbd4758f1c9162c39c34b21986604f7 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 2 Feb 2017 11:34:39 +0000 Subject: [PATCH] Parse once and re-use the parsed output Rather than re-parsing the same output in each function --- src/Markdown.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Markdown.js b/src/Markdown.js index c342e7cceb..126e0583d1 100644 --- a/src/Markdown.js +++ b/src/Markdown.js @@ -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;