|
1 | 1 | class Markdown { |
2 | | - constructor(output, options) { |
3 | | - if (!window.marked) { |
4 | | - throw new ReferenceError( |
5 | | - `marked.js is not loaded.` |
6 | | - ) |
7 | | - } |
8 | | - this.output = output |
9 | | - this.options = options |
10 | | - } |
| 2 | + constructor(output, options) { |
| 3 | + if (!window.marked) { |
| 4 | + throw new ReferenceError( |
| 5 | + `marked.js is not loaded.` |
| 6 | + ) |
| 7 | + } |
| 8 | + this.output = output |
| 9 | + this.options = options |
| 10 | + } |
| 11 | + |
| 12 | + process() { |
| 13 | + let renderer = new marked.Renderer() |
| 14 | + renderer.code = function(text) { |
| 15 | + let highlightedCode = window.hljs ? hljs.highlightAuto(text) : { |
| 16 | + value: text |
| 17 | + } |
| 18 | + let language = window.hljs ? highlightedCode.language : '' |
| 19 | + let template = `<pre><code class="ejs-code hljs ${language}">${highlightedCode.value}</code></pre>`; |
| 20 | + return template |
| 21 | + } |
| 22 | + renderer.link = (text, title, link) => { |
| 23 | + if (text.indexOf('</a') === -1) return text |
| 24 | + if (text.match(/>(.+)<\/a/gi)) { |
| 25 | + return `<a href="${RegExp.$1}" rel=${this.options.linkOptions.rel}" target="${this.options.linkOptions.target}" title="${title}">${link}</a>` |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + //Fix for heading that should be actually present in marked.js |
| 30 | + //if gfm is true the `## Heading` is acceptable but `##Heading` is not |
| 31 | + marked.Lexer.rules.gfm.heading = marked.Lexer.rules.normal.heading; |
| 32 | + marked.Lexer.rules.tables.heading = marked.Lexer.rules.normal.heading; |
11 | 33 |
|
12 | | - process() { |
13 | | - let renderer = new marked.Renderer() |
14 | | - renderer.code = function(text) { |
15 | | - let highlightedCode = window.hljs ? hljs.highlightAuto(text) : { |
16 | | - value: text |
17 | | - } |
18 | | - let language = window.hljs ? highlightedCode.language : '' |
19 | | - let template = `<pre><code class="ejs-code hljs ${language}">${highlightedCode.value}</code></pre>`; |
20 | | - return template |
21 | | - } |
22 | | - renderer.link = (text,title,link) => { |
23 | | - if (text.indexOf('</a') === -1) return text |
24 | | - if (text.match(/>(.+)<\/a/gi)){ |
25 | | - return `<a href="${RegExp.$1}" rel=${this.options.linkOptions.rel}" target="${this.options.linkOptions.target}" title="${title}">${link}</a>` |
26 | | - } |
27 | | - } |
28 | 34 | renderer.paragraph = (text) => `<p> ${text} </p>` //for font smiley in end. |
29 | 35 | this.options.markedOptions.renderer = renderer |
30 | 36 | let output = marked(this.output, this.options.markedOptions) |
|
0 commit comments