|
| 1 | +import marked from 'marked'; |
| 2 | + |
| 3 | +import hljs from 'highlight.js/lib/highlight'; |
| 4 | + |
| 5 | +// Installed languages |
| 6 | +import javascript from 'highlight.js/lib/languages/javascript'; |
| 7 | +import css from 'highlight.js/lib/languages/css'; |
| 8 | +import handlebars from 'highlight.js/lib/languages/handlebars'; |
| 9 | +import htmlbars from 'highlight.js/lib/languages/htmlbars'; |
| 10 | +import json from 'highlight.js/lib/languages/json'; |
| 11 | +import xml from 'highlight.js/lib/languages/xml'; |
| 12 | +import diff from 'highlight.js/lib/languages/diff'; |
| 13 | +import shell from 'highlight.js/lib/languages/shell'; |
| 14 | + |
| 15 | +hljs.registerLanguage('javascript', javascript); |
| 16 | +hljs.registerLanguage('css', css); |
| 17 | +hljs.registerLanguage('handlebars', handlebars); |
| 18 | +hljs.registerLanguage('htmlbars', htmlbars); |
| 19 | +hljs.registerLanguage('json', json); |
| 20 | +hljs.registerLanguage('xml', xml); |
| 21 | +hljs.registerLanguage('diff', diff); |
| 22 | +hljs.registerLanguage('shell', shell); |
| 23 | + |
| 24 | +function highlightCode(code, lang) { |
| 25 | + return hljs.getLanguage(lang) ? hljs.highlight(lang, code).value : code |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + This is the function used by AddonDocs to compile Markdown into HTML, for |
| 30 | + example when turning `template.md` files into `template.hbs`. It includes |
| 31 | + some parsing options, as well as syntax highlighting for code blocks. |
| 32 | +
|
| 33 | + You can use it in your own code, so your Markdown-rendered content shares the |
| 34 | + same styling & syntax highlighting as the content AddonDocs already handles. |
| 35 | +
|
| 36 | + For example, you can use it if your Ember App has Markdown data that is |
| 37 | + fetched at runtime from an API: |
| 38 | +
|
| 39 | + ```js |
| 40 | + import Component from '@ember/component'; |
| 41 | + import compileMarkdown from 'ember-cli-addon-docs/utils/compile-markdown'; |
| 42 | + import { htmlSafe } from '@ember/string'; |
| 43 | +
|
| 44 | + export default Component.extend({ |
| 45 | + htmlBody: computed('post.body', function() { |
| 46 | + return htmlSafe(compileMarkdown(this.post.body)); |
| 47 | + }); |
| 48 | + }); |
| 49 | + ``` |
| 50 | +
|
| 51 | + @function |
| 52 | + @param {string} source Markdown string representing the source content |
| 53 | + @param {object} options? Options. Pass `targetHandlebars: true` if turning MD into HBS |
| 54 | +*/ |
| 55 | +export default function compileMarkdown(source, config) { |
| 56 | + let tokens = marked.lexer(source); |
| 57 | + let markedOptions = { |
| 58 | + highlight: highlightCode, |
| 59 | + renderer: new HBSRenderer(config) |
| 60 | + }; |
| 61 | + |
| 62 | + if (config && config.targetHandlebars) { |
| 63 | + tokens = compactParagraphs(tokens); |
| 64 | + } |
| 65 | + |
| 66 | + return `<div class="docs-md">${marked.parser(tokens, markedOptions).trim()}</div>`; |
| 67 | +} |
| 68 | + |
| 69 | +// Whitespace can imply paragraphs in Markdown, which can result |
| 70 | +// in interleaving between <p> tags and block component invocations, |
| 71 | +// so this scans the Marked tokens to turn things like this: |
| 72 | +// <p>{{#my-component}}<p> |
| 73 | +// <p>{{/my-component}}</p> |
| 74 | +// Into this: |
| 75 | +// <p>{{#my-component}} {{/my-component}}</p> |
| 76 | +function compactParagraphs(tokens) { |
| 77 | + let compacted = []; |
| 78 | + |
| 79 | + compacted.links = tokens.links; |
| 80 | + |
| 81 | + let balance = 0; |
| 82 | + for (let token of tokens) { |
| 83 | + if (balance === 0) { |
| 84 | + compacted.push(token); |
| 85 | + } else if (token.text) { |
| 86 | + let last = compacted[compacted.length - 1]; |
| 87 | + last.text = `${last.text} ${token.text}`; |
| 88 | + } |
| 89 | + |
| 90 | + let tokenText = token.text || ''; |
| 91 | + let textWithoutCode = tokenText.replace(/`[\s\S]*?`/g, ''); |
| 92 | + |
| 93 | + balance += count(/{{#/g, textWithoutCode); |
| 94 | + balance += count(/<[A-Z]/g, textWithoutCode); |
| 95 | + balance -= count(/{{\//g, textWithoutCode); |
| 96 | + balance -= count(/<\/[A-Z]/g, textWithoutCode); |
| 97 | + } |
| 98 | + |
| 99 | + return compacted; |
| 100 | +} |
| 101 | + |
| 102 | +function count(regex, string) { |
| 103 | + let total = 0; |
| 104 | + while (regex.exec(string)) total++; |
| 105 | + return total; |
| 106 | +} |
| 107 | + |
| 108 | +class HBSRenderer extends marked.Renderer { |
| 109 | + constructor(config) { |
| 110 | + super(); |
| 111 | + this.config = config || {}; |
| 112 | + } |
| 113 | + |
| 114 | + codespan() { |
| 115 | + return this._processCode(super.codespan.apply(this, arguments)); |
| 116 | + } |
| 117 | + |
| 118 | + code() { |
| 119 | + let code = this._processCode(super.code.apply(this, arguments)); |
| 120 | + |
| 121 | + return code.replace(/^<pre>/, '<pre class="docs-md__code">'); |
| 122 | + } |
| 123 | + |
| 124 | + // Unescape markdown escaping in general, since it can interfere with |
| 125 | + // Handlebars templating |
| 126 | + text() { |
| 127 | + let text = super.text.apply(this, arguments); |
| 128 | + if (this.config.targetHandlebars) { |
| 129 | + text = text |
| 130 | + .replace(/&/g, '&') |
| 131 | + .replace(/</g, '<') |
| 132 | + .replace(/>/g, '>') |
| 133 | + .replace(/"|"/g, '"') |
| 134 | + .replace(/'|'/g, '\''); |
| 135 | + } |
| 136 | + return text; |
| 137 | + } |
| 138 | + |
| 139 | + // Escape curlies in code spans/blocks to avoid treating them as Handlebars |
| 140 | + _processCode(string) { |
| 141 | + if (this.config.targetHandlebars) { |
| 142 | + string = this._escapeCurlies(string); |
| 143 | + } |
| 144 | + |
| 145 | + return string; |
| 146 | + } |
| 147 | + |
| 148 | + _escapeCurlies(string) { |
| 149 | + return string |
| 150 | + .replace(/{{/g, '{{') |
| 151 | + .replace(/}}/g, '}}'); |
| 152 | + } |
| 153 | + |
| 154 | + heading(text, level) { |
| 155 | + let id = text.toLowerCase().replace(/<\/?.*?>/g, '').replace(/[^\w]+/g, '-'); |
| 156 | + let inner = level === 1 ? text : `<a href='#${id}' class='heading-anchor'>${text}</a>`; |
| 157 | + |
| 158 | + return ` |
| 159 | + <h${level} id='${id}' class='docs-md__h${level}'>${inner}</h${level}> |
| 160 | + `; |
| 161 | + } |
| 162 | + |
| 163 | + hr() { |
| 164 | + return `<hr class='docs-md__hr'>`; |
| 165 | + } |
| 166 | + |
| 167 | + blockquote(text) { |
| 168 | + return `<blockquote class='docs-md__blockquote'>${text}</blockquote>`; |
| 169 | + } |
| 170 | + |
| 171 | + link(href, title, text) { |
| 172 | + const titleAttribute = title ? `title="${title}"` : ''; |
| 173 | + return `<a href="${href}" ${titleAttribute} class="docs-md__a">${text}</a>`; |
| 174 | + } |
| 175 | +} |
0 commit comments