|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | +const yaml = require('yaml'); |
| 6 | +const cmark = require('cmark-gfm'); |
| 7 | +const mkdirp = require('mkdirp'); |
| 8 | +const jsdom = require('jsdom'); |
| 9 | +const npm = require('../lib/npm.js') |
| 10 | + |
| 11 | +const config = require('./config.json'); |
| 12 | + |
| 13 | +const docsRoot = __dirname; |
| 14 | +const inputRoot = path.join(docsRoot, 'content'); |
| 15 | +const outputRoot = path.join(docsRoot, 'output'); |
| 16 | + |
| 17 | +const template = fs.readFileSync('template.html').toString(); |
| 18 | + |
| 19 | +walk(inputRoot); |
| 20 | + |
| 21 | +function walk(root, dirRelative) { |
| 22 | + const dirPath = dirRelative ? path.join(root, dirRelative) : root; |
| 23 | + |
| 24 | + fs.readdirSync(dirPath).forEach((childFilename) => { |
| 25 | + const childRelative = dirRelative ? path.join(dirRelative, childFilename) : childFilename; |
| 26 | + const childPath = path.join(root, childRelative); |
| 27 | + |
| 28 | + if (fs.lstatSync(childPath).isDirectory()) { |
| 29 | + walk(root, childRelative); |
| 30 | + } |
| 31 | + else { |
| 32 | + translate(childRelative); |
| 33 | + } |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +function translate(childPath) { |
| 38 | + const inputPath = path.join(inputRoot, childPath); |
| 39 | + |
| 40 | + if (!inputPath.match(/\.md$/)) { |
| 41 | + console.log(`warning: unknown file type ${inputPath}, ignored`); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const outputPath = path.join(outputRoot, childPath.replace(/\.md$/, '.html')); |
| 46 | + |
| 47 | + let md = fs.readFileSync(inputPath).toString(); |
| 48 | + let frontmatter = { }; |
| 49 | + |
| 50 | + // Take the leading frontmatter out of the markdown |
| 51 | + md = md.replace(/^---\n([\s\S]+)\n---\n/, (header, fm) => { |
| 52 | + frontmatter = yaml.parse(fm, 'utf8'); |
| 53 | + return ''; |
| 54 | + }); |
| 55 | + |
| 56 | + // Replace any tokens in the source |
| 57 | + md = md.replace(/@VERSION@/, npm.version); |
| 58 | + |
| 59 | + // Render the markdown into an HTML snippet using a GFM renderer. |
| 60 | + const content = cmark.renderHtmlSync(md, { |
| 61 | + 'smart': true, |
| 62 | + 'githubPreLang': true, |
| 63 | + 'strikethroughDoubleTilde': true, |
| 64 | + 'unsafe': false, |
| 65 | + extensions: { |
| 66 | + 'table': true, |
| 67 | + 'strikethrough': true, |
| 68 | + 'tagfilter': true, |
| 69 | + 'autolink': true |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // Inject this data into the template, using a mustache-like |
| 74 | + // replacement scheme. |
| 75 | + const html = template.replace(/\{\{\s*([\w\.]+)\s*\}\}/g, (token, key) => { |
| 76 | + switch (key) { |
| 77 | + case 'content': |
| 78 | + return content; |
| 79 | + case 'path': |
| 80 | + return childPath; |
| 81 | + case 'url_path': |
| 82 | + return encodeURI(childPath); |
| 83 | + |
| 84 | + case 'title': |
| 85 | + case 'section': |
| 86 | + case 'description': |
| 87 | + return frontmatter[key]; |
| 88 | + |
| 89 | + case 'config.github_repo': |
| 90 | + case 'config.github_branch': |
| 91 | + case 'config.github_path': |
| 92 | + return config[key.replace(/^config\./, '')]; |
| 93 | + |
| 94 | + default: |
| 95 | + console.log(`warning: unknown token '${token}' in ${inputPath}`); |
| 96 | + return ''; |
| 97 | + } |
| 98 | + console.log(key); |
| 99 | + return key; |
| 100 | + }); |
| 101 | + |
| 102 | + const dom = new jsdom.JSDOM(html); |
| 103 | + const document = dom.window.document; |
| 104 | + |
| 105 | + // Rewrite relative URLs in links and image sources to be relative to |
| 106 | + // this file; this is for supporting `file://` links. HTML pages need |
| 107 | + // suffix appended. |
| 108 | + const links = [ |
| 109 | + { tag: 'a', attr: 'href', suffix: '.html' }, |
| 110 | + { tag: 'img', attr: 'src' } |
| 111 | + ]; |
| 112 | + |
| 113 | + for (let linktype of links) { |
| 114 | + for (let tag of document.querySelectorAll(linktype.tag)) { |
| 115 | + let url = tag.getAttribute(linktype.attr); |
| 116 | + |
| 117 | + if (url.startsWith('/')) { |
| 118 | + const childDepth = childPath.split('/').length - 1; |
| 119 | + const prefix = childDepth > 0 ? '../'.repeat(childDepth) : './'; |
| 120 | + |
| 121 | + url = url.replace(/^\//, prefix); |
| 122 | + |
| 123 | + if (linktype.suffix) { |
| 124 | + url += linktype.suffix; |
| 125 | + } |
| 126 | + |
| 127 | + tag.setAttribute(linktype.attr, url); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Give headers a unique id so that they can be linked within the doc |
| 133 | + const headerIds = [ ]; |
| 134 | + for (let header of document.querySelectorAll('h1, h2, h3, h4, h5, h6')) { |
| 135 | + if (header.getAttribute('id')) { |
| 136 | + headerIds.push(header.getAttribute('id')); |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + const headerText = header.textContent.replace(/[A-Z]/g, x => x.toLowerCase()).replace(/ /g, '-').replace(/[^a-z0-9\-]/g, ''); |
| 141 | + let headerId = headerText; |
| 142 | + let headerIncrement = 1; |
| 143 | + |
| 144 | + while (headerIds.includes(headerId)) { |
| 145 | + headerId = headerText + (++headerIncrement); |
| 146 | + } |
| 147 | + |
| 148 | + headerIds.push(headerId); |
| 149 | + header.setAttribute('id', headerId); |
| 150 | + } |
| 151 | + |
| 152 | + const output = dom.serialize(); |
| 153 | + |
| 154 | + mkdirp.sync(path.dirname(outputPath)); |
| 155 | + fs.writeFileSync(outputPath, output); |
| 156 | +} |
| 157 | + |
| 158 | +function debug(str) { |
| 159 | + console.log(str); |
| 160 | +} |
0 commit comments