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

Commit 72e5b22

Browse files
committed
Parse once and re-use the parsed output
Rather than re-parsing the same output in each function
1 parent fc90ed1 commit 72e5b22

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/Markdown.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import commonmark from 'commonmark';
2424
export default class Markdown {
2525
constructor(input) {
2626
this.input = input;
27-
this.parser = new commonmark.Parser();
27+
28+
const parser = new commonmark.Parser();
29+
this.parsed = parser.parse(this.input);
30+
2831
this.renderer = new commonmark.HtmlRenderer({safe: false});
2932
}
3033

@@ -49,8 +52,7 @@ export default class Markdown {
4952
dummy_renderer.softbreak = function(t) { return t; };
5053
dummy_renderer.paragraph = function(t) { return t; };
5154

52-
const dummy_parser = new commonmark.Parser();
53-
dummy_renderer.render(dummy_parser.parse(this.input));
55+
dummy_renderer.render(this.parsed);
5456

5557
return is_plain;
5658
}
@@ -73,8 +75,7 @@ export default class Markdown {
7375
}
7476
};
7577

76-
var parsed = this.parser.parse(this.input);
77-
var rendered = this.renderer.render(parsed);
78+
var rendered = this.renderer.render(this.parsed);
7879

7980
this.renderer.paragraph = real_paragraph;
8081

@@ -116,8 +117,7 @@ export default class Markdown {
116117
}
117118
};
118119

119-
var parsed = this.parser.parse(this.input);
120-
var rendered = this.renderer.render(parsed);
120+
var rendered = this.renderer.render(this.parsed);
121121

122122
this.renderer.paragraph = real_paragraph;
123123

0 commit comments

Comments
 (0)