|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Build stability table to documentation.html/json/md by generated all.json |
| 4 | + |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const unified = require('unified'); |
| 8 | +const raw = require('rehype-raw'); |
| 9 | +const markdown = require('remark-parse'); |
| 10 | +const htmlStringify = require('rehype-stringify'); |
| 11 | +const gfm = require('remark-gfm'); |
| 12 | +const remark2rehype = require('remark-rehype'); |
| 13 | +const visit = require('unist-util-visit'); |
| 14 | + |
| 15 | +const source = `${__dirname}/../../out/doc/api`; |
| 16 | +const data = require(path.join(source, 'all.json')); |
| 17 | +const mark = '<!-- STABILITY_OVERVIEW_SLOT -->'; |
| 18 | + |
| 19 | +const output = { |
| 20 | + json: path.join(source, 'stability.json'), |
| 21 | + docHTML: path.join(source, 'documentation.html'), |
| 22 | + docJSON: path.join(source, 'documentation.json'), |
| 23 | + docMarkdown: path.join(source, 'documentation.md'), |
| 24 | +}; |
| 25 | + |
| 26 | +function collectStability(data) { |
| 27 | + const stability = []; |
| 28 | + |
| 29 | + for (const mod of data.modules) { |
| 30 | + if (mod.displayName && mod.stability >= 0) { |
| 31 | + const link = mod.source.replace('doc/api/', '').replace('.md', '.html'); |
| 32 | + // const link = re.exec(toc)[1] |
| 33 | + stability.push({ |
| 34 | + api: mod.name, |
| 35 | + link: link, |
| 36 | + stability: mod.stability, |
| 37 | + stabilityText: `(${mod.stability}) ${mod.stabilityText}`, |
| 38 | + }); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + stability.sort((a, b) => a.api.localeCompare(b.api)); |
| 43 | + return stability; |
| 44 | +} |
| 45 | + |
| 46 | +function createMarkdownTable(data) { |
| 47 | + const md = ['| API | Stability |', '| --- | --------- |']; |
| 48 | + |
| 49 | + for (const mod of data) { |
| 50 | + md.push(`| [${mod.api}](${mod.link}) | ${mod.stabilityText} |`); |
| 51 | + } |
| 52 | + |
| 53 | + return md.join('\n'); |
| 54 | +} |
| 55 | + |
| 56 | +function createHTML(md) { |
| 57 | + const file = unified() |
| 58 | + .use(markdown) |
| 59 | + .use(gfm) |
| 60 | + .use(remark2rehype, { allowDangerousHtml: true }) |
| 61 | + .use(raw) |
| 62 | + .use(htmlStringify) |
| 63 | + .use(processStability) |
| 64 | + .processSync(md); |
| 65 | + |
| 66 | + return file.contents.trim(); |
| 67 | +} |
| 68 | + |
| 69 | +function processStability() { |
| 70 | + return (tree) => { |
| 71 | + visit(tree, { type: 'element', tagName: 'tr' }, (node) => { |
| 72 | + const [api, stability] = node.children; |
| 73 | + if (api.tagName !== 'td') { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + api.properties.class = 'module_stability'; |
| 78 | + |
| 79 | + const level = stability.children[0].value[1]; |
| 80 | + stability.properties.class = `api_stability api_stability_${level}`; |
| 81 | + }); |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function updateStabilityMark(file, value, mark) { |
| 86 | + const fd = fs.openSync(file, 'r+'); |
| 87 | + const content = fs.readFileSync(fd); |
| 88 | + |
| 89 | + // Find the position of the `mark`. |
| 90 | + const index = content.indexOf(mark); |
| 91 | + |
| 92 | + // Overwrite the mark with `value` parameter. |
| 93 | + const offset = fs.writeSync(fd, value, index, 'utf-8'); |
| 94 | + |
| 95 | + // Re-write the end of the file after `value`. |
| 96 | + fs.writeSync(fd, content, index + mark.length, undefined, index + offset); |
| 97 | + fs.closeSync(fd); |
| 98 | +} |
| 99 | + |
| 100 | +const stability = collectStability(data); |
| 101 | + |
| 102 | +// add markdown |
| 103 | +const markdownTable = createMarkdownTable(stability); |
| 104 | +updateStabilityMark(output.docMarkdown, markdownTable, mark); |
| 105 | + |
| 106 | +// add html table |
| 107 | +const html = createHTML(markdownTable); |
| 108 | +updateStabilityMark(output.docHTML, html, mark); |
| 109 | + |
| 110 | +// add json output |
| 111 | +updateStabilityMark(output.docJSON, JSON.stringify(html), JSON.stringify(mark)); |
0 commit comments