diff options
author | Robin Appelman <robin@icewind.nl> | 2017-08-27 13:33:11 +0300 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-08-27 13:33:11 +0300 |
commit | 3c9feffbaa4e0800b127f7f7dfabc850609296e2 (patch) | |
tree | 1c6895e4bcd6daf00ddc672c204e3524898f67b3 /js | |
parent | 1dfa99d5146f76353e98e4e723f130ffb6794202 (diff) |
load highlight.js on demand
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'js')
-rw-r--r-- | js/Renderer.ts | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/js/Renderer.ts b/js/Renderer.ts index 51a0721..c3e4108 100644 --- a/js/Renderer.ts +++ b/js/Renderer.ts @@ -1,7 +1,6 @@ import * as MarkdownIt from 'markdown-it'; import * as MathPlugin from 'markdown-it-texmath'; import * as KaTeX from 'katex'; -import * as HighlightPlugin from 'markdown-it-highlightjs'; import * as iterator from 'markdown-it-for-inline'; import {CheckboxPlugin} from './CheckboxPlugin'; import * as AnchorPlugin from 'markdown-it-anchor'; @@ -17,11 +16,11 @@ const slugifyHeading = name => 'editor/' + slugify(name).toLowerCase(); export class Renderer { md: MarkdownIt.MarkdownIt; mermaidLoaded: boolean = false; + highlightLoaded: boolean = false; constructor() { this.md = new MarkdownIt(); this.md.use(MathPlugin.use(KaTeX)); - this.md.use(HighlightPlugin); this.md.use(CheckboxPlugin, { checkboxClass: 'checkbox' }); @@ -71,6 +70,10 @@ export class Renderer { return text.match(/(gantt|sequenceDiagram|graph (?:TB|BT|RL|LR|TD))/) !== null; } + requiresHighlight(text: string): boolean { + return text.indexOf('```') !== -1; + } + renderText(text: string, element): void { if (this.requiresMermaid(text) && !this.mermaidLoaded) { this.mermaidLoaded = true; @@ -80,6 +83,13 @@ export class Renderer { this.renderText(text, element); }); } + if (this.requiresHighlight(text) && !this.highlightLoaded) { + this.highlightLoaded = true; + require.ensure(['markdown-it-highlightjs'], () => { + this.md.use(require('markdown-it-highlightjs')); + this.renderText(text, element); + }); + } const html = this.md.render(this.prepareText(text)); element.html(html); } |