(
src,
{ path, template, data, unified, remarkParse, remarkGfm, remarkRehype, rehypeStringify }
)
| 2 | const { version } = require('../../package.json') |
| 3 | |
| 4 | function transformHTML ( |
| 5 | src, |
| 6 | { path, template, data, unified, remarkParse, remarkGfm, remarkRehype, rehypeStringify } |
| 7 | ) { |
| 8 | const content = unified() |
| 9 | .use(remarkParse) |
| 10 | .use(remarkGfm, { |
| 11 | singleTilde: false, |
| 12 | }) |
| 13 | .use(remarkRehype) |
| 14 | .use(rehypeStringify) |
| 15 | .processSync(src) |
| 16 | .toString() |
| 17 | |
| 18 | // Inject this data into the template, using a mustache-like |
| 19 | // replacement scheme. |
| 20 | const html = template.replace(/{{\s*([\w.]+)\s*}}/g, (token, key) => { |
| 21 | switch (key) { |
| 22 | case 'content': |
| 23 | return `<div id="_content">${content}</div>` |
| 24 | case 'url_path': |
| 25 | return encodeURI(path) |
| 26 | |
| 27 | case 'toc': |
| 28 | return '<div id="_table_of_contents"></div>' |
| 29 | |
| 30 | case 'title': |
| 31 | case 'section': |
| 32 | case 'description': |
| 33 | return data[key] |
| 34 | |
| 35 | case 'config.github_repo': |
| 36 | case 'config.github_branch': |
| 37 | case 'config.github_path': |
| 38 | return data[key.replace(/^config\./, '')] |
| 39 | |
| 40 | case 'version': |
| 41 | return version |
| 42 | |
| 43 | default: |
| 44 | throw new Error(`warning: unknown token '${token}' in ${path}`) |
| 45 | } |
| 46 | }) |
| 47 | |
| 48 | const dom = new jsdom.JSDOM(html) |
| 49 | const document = dom.window.document |
| 50 | |
| 51 | // Rewrite relative URLs in links and image sources to be relative to |
| 52 | // this file; this is for supporting `file://` links. HTML pages need |
| 53 | // suffix appended. |
| 54 | const links = [ |
| 55 | { tag: 'a', attr: 'href', suffix: '.html' }, |
| 56 | { tag: 'img', attr: 'src' }, |
| 57 | ] |
| 58 | |
| 59 | for (const linktype of links) { |
| 60 | for (const tag of document.querySelectorAll(linktype.tag)) { |
| 61 | let url = tag.getAttribute(linktype.attr) |
nothing calls this directly
no test coverage detected