(template = '', context = {}, options = {})
| 12 | |
| 13 | // parse multiple times because some templates contain more templates. :] |
| 14 | async function renderContent(template = '', context = {}, options = {}) { |
| 15 | // If called with a falsy template, it can't ever become something |
| 16 | // when rendered. We can exit early to save some pointless work. |
| 17 | if (!template) return template |
| 18 | try { |
| 19 | // remove any newlines that precede html comments, then remove the comments |
| 20 | if (template) { |
| 21 | template = stripHtmlComments(template.replace(/\n<!--/g, '<!--')) |
| 22 | } |
| 23 | |
| 24 | template = await liquid.parseAndRender(template, context) |
| 25 | |
| 26 | // this workaround loses syntax highlighting but correctly handles tags like <em> and entities like < |
| 27 | template = template.replace( |
| 28 | /``` ?shell\r?\n\s*?(\S[\s\S]*?)\r?\n.*?```/gm, |
| 29 | '<pre><code class="hljs language-shell">$1</code></pre>' |
| 30 | ) |
| 31 | |
| 32 | // clean up empty lines in TOC lists left by unrendered list items (due to productVersions) |
| 33 | // for example, remove the blank line here: |
| 34 | // - <a>foo</a> |
| 35 | // |
| 36 | // - <a>bar</a> |
| 37 | if (template.includes('</a>')) { |
| 38 | template = template.replace(blankLineInList, '$1$2') |
| 39 | } |
| 40 | |
| 41 | // this removes any extra newlines left by (now resolved) liquid |
| 42 | // statements so that extra space doesn't mess with list numbering |
| 43 | template = template.replace(/(\r?\n){3}/g, '\n\n') |
| 44 | |
| 45 | const processor = createProcessor(context) |
| 46 | const vFile = await processor.process(template) |
| 47 | let html = vFile.toString() |
| 48 | |
| 49 | if (options.textOnly) { |
| 50 | html = fastTextOnly(html) |
| 51 | } |
| 52 | |
| 53 | return html.trim() |
| 54 | } catch (error) { |
| 55 | if (options.filename) { |
| 56 | console.error(`renderContent failed on file: ${options.filename}`) |
| 57 | } |
| 58 | throw error |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Given a piece of HTML return it without HTML. E.g. |
| 63 | // `<p>Foo & bar</p>` becomes `Foo & bar` |
no test coverage detected