|
| 1 | +const fs = require('fs/promises') |
| 2 | +const path = require('path') |
| 3 | +const marked = require('marked') |
| 4 | +const fm = require('front-matter') |
| 5 | + |
| 6 | +function tocHTML (toc) { |
| 7 | + let html = '<ul>\n' |
| 8 | + for (const li of toc) { |
| 9 | + html += '<li>\n' |
| 10 | + html += `<div>\n<a href="#${li.slug}">${li.text}</a>\n</div>\n` |
| 11 | + if (li.children && li.children.length > 0) { |
| 12 | + html += tocHTML(li.children) |
| 13 | + } |
| 14 | + html += '</li>\n' |
| 15 | + } |
| 16 | + html += '</ul>\n' |
| 17 | + |
| 18 | + return html |
| 19 | +} |
| 20 | + |
| 21 | +function markdownTOC (markdown) { |
| 22 | + const tokens = marked.lexer(markdown) |
| 23 | + const slugger = new marked.Slugger() |
| 24 | + const toc = [] |
| 25 | + let currentHeading |
| 26 | + let ignoreFirst = true |
| 27 | + for (const token of tokens) { |
| 28 | + if (token.type === 'heading') { |
| 29 | + if (token.depth === 1) { |
| 30 | + if (ignoreFirst) { |
| 31 | + ignoreFirst = false |
| 32 | + continue |
| 33 | + } |
| 34 | + currentHeading = { |
| 35 | + text: token.text, |
| 36 | + slug: slugger.slug(token.text), |
| 37 | + children: [] |
| 38 | + } |
| 39 | + toc.push(currentHeading) |
| 40 | + } else if (token.depth === 2) { |
| 41 | + if (!currentHeading) { |
| 42 | + continue |
| 43 | + } |
| 44 | + currentHeading.children.push({ |
| 45 | + text: token.text, |
| 46 | + slug: slugger.slug(token.text) |
| 47 | + }) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + toc: tocHTML(toc), |
| 54 | + html: marked.parser(tokens) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function createHTML (header, footer, text) { |
| 59 | + const { attributes, body } = fm(text) |
| 60 | + for (const prop in attributes) { |
| 61 | + header = header.replace(new RegExp(`%\\(${prop}\\)s`, 'ig'), attributes[prop]) |
| 62 | + footer = footer.replace(new RegExp(`%\\(${prop}\\)s`, 'ig'), attributes[prop]) |
| 63 | + } |
| 64 | + |
| 65 | + const { toc, html } = markdownTOC(body) |
| 66 | + |
| 67 | + header = header.replace(/%\(toc_html\)s/ig, toc) |
| 68 | + |
| 69 | + return header + html + footer |
| 70 | +} |
| 71 | + |
| 72 | +async function createDocs () { |
| 73 | + const docs = path.join(__dirname, 'docs') |
| 74 | + const dist = path.join(docs, 'dist') |
| 75 | + |
| 76 | + await fs.rmdir(dist, { recursive: true }) |
| 77 | + await fs.mkdir(dist) |
| 78 | + |
| 79 | + const header = await fs.readFile(path.join(docs, 'branding', 'header.html.in'), { encoding: 'utf8' }) |
| 80 | + const footer = await fs.readFile(path.join(docs, 'branding', 'footer.html.in'), { encoding: 'utf8' }) |
| 81 | + const files = await fs.readdir(docs) |
| 82 | + for (const file of files) { |
| 83 | + if (!file.endsWith('.md')) { |
| 84 | + continue |
| 85 | + } |
| 86 | + const text = await fs.readFile(path.join(docs, file), { encoding: 'utf8' }) |
| 87 | + const html = createHTML(header, footer, text) |
| 88 | + |
| 89 | + await fs.writeFile(path.join(dist, file.replace(/md$/, 'html')), html) |
| 90 | + } |
| 91 | + |
| 92 | + const dest = path.join(dist, 'media') |
| 93 | + const src = path.join(docs, 'branding', 'media') |
| 94 | + await fs.mkdir(dest) |
| 95 | + await fs.mkdir(path.join(dest, 'css')) |
| 96 | + await fs.mkdir(path.join(dest, 'img')) |
| 97 | + await fs.copyFile(path.join(src, 'css', 'style.css'), path.join(dest, 'css', 'style.css')) |
| 98 | + await fs.copyFile(path.join(src, 'img', 'logo.svg'), path.join(dest, 'img', 'logo.svg')) |
| 99 | +} |
| 100 | + |
| 101 | +createDocs() |
0 commit comments