2020-11-22 09:39:25 +00:00
|
|
|
// Build stability table to documentation.html/json/md by generated all.json
|
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
import raw from 'rehype-raw';
|
|
|
|
import htmlStringify from 'rehype-stringify';
|
|
|
|
import gfm from 'remark-gfm';
|
|
|
|
import markdown from 'remark-parse';
|
|
|
|
import remark2rehype from 'remark-rehype';
|
2021-09-08 19:49:55 +00:00
|
|
|
import { unified } from 'unified';
|
2021-06-08 10:44:33 +00:00
|
|
|
import { visit } from 'unist-util-visit';
|
2020-11-22 09:39:25 +00:00
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
const source = new URL('../../out/doc/api/', import.meta.url);
|
|
|
|
const data = JSON.parse(fs.readFileSync(new URL('./all.json', source), 'utf8'));
|
2021-04-02 19:27:19 +00:00
|
|
|
const markBegin = '<!-- STABILITY_OVERVIEW_SLOT_BEGIN -->';
|
|
|
|
const markEnd = '<!-- STABILITY_OVERVIEW_SLOT_END -->';
|
|
|
|
const mark = `${markBegin}(.*)${markEnd}`;
|
2020-11-22 09:39:25 +00:00
|
|
|
|
|
|
|
const output = {
|
2021-06-08 10:44:33 +00:00
|
|
|
json: new URL('./stability.json', source),
|
|
|
|
docHTML: new URL('./documentation.html', source),
|
|
|
|
docJSON: new URL('./documentation.json', source),
|
|
|
|
docMarkdown: new URL('./documentation.md', source),
|
2020-11-22 09:39:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function collectStability(data) {
|
|
|
|
const stability = [];
|
|
|
|
|
|
|
|
for (const mod of data.modules) {
|
|
|
|
if (mod.displayName && mod.stability >= 0) {
|
|
|
|
const link = mod.source.replace('doc/api/', '').replace('.md', '.html');
|
2021-01-27 03:07:04 +00:00
|
|
|
|
2022-11-07 01:25:44 +00:00
|
|
|
let { stabilityText } = mod;
|
|
|
|
if (stabilityText.includes('. ')) {
|
|
|
|
stabilityText = stabilityText.slice(0, stabilityText.indexOf('.'));
|
|
|
|
}
|
|
|
|
|
2020-11-22 09:39:25 +00:00
|
|
|
stability.push({
|
|
|
|
api: mod.name,
|
2022-11-06 09:32:28 +00:00
|
|
|
displayName: mod.textRaw,
|
2020-11-22 09:39:25 +00:00
|
|
|
link: link,
|
|
|
|
stability: mod.stability,
|
2022-11-07 01:25:44 +00:00
|
|
|
stabilityText: `(${mod.stability}) ${stabilityText}`,
|
2020-11-22 09:39:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 09:32:28 +00:00
|
|
|
stability.sort((a, b) => a.displayName.localeCompare(b.displayName));
|
2020-11-22 09:39:25 +00:00
|
|
|
return stability;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMarkdownTable(data) {
|
|
|
|
const md = ['| API | Stability |', '| --- | --------- |'];
|
|
|
|
|
|
|
|
for (const mod of data) {
|
2022-11-06 09:32:28 +00:00
|
|
|
md.push(`| [${mod.displayName}](${mod.link}) | ${mod.stabilityText} |`);
|
2020-11-22 09:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return md.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
function createHTML(md) {
|
|
|
|
const file = unified()
|
|
|
|
.use(markdown)
|
|
|
|
.use(gfm)
|
|
|
|
.use(remark2rehype, { allowDangerousHtml: true })
|
|
|
|
.use(raw)
|
|
|
|
.use(htmlStringify)
|
|
|
|
.use(processStability)
|
|
|
|
.processSync(md);
|
|
|
|
|
2021-09-08 19:49:55 +00:00
|
|
|
return file.value.trim();
|
2020-11-22 09:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function processStability() {
|
|
|
|
return (tree) => {
|
|
|
|
visit(tree, { type: 'element', tagName: 'tr' }, (node) => {
|
|
|
|
const [api, stability] = node.children;
|
|
|
|
if (api.tagName !== 'td') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
api.properties.class = 'module_stability';
|
|
|
|
|
|
|
|
const level = stability.children[0].value[1];
|
|
|
|
stability.properties.class = `api_stability api_stability_${level}`;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateStabilityMark(file, value, mark) {
|
|
|
|
const fd = fs.openSync(file, 'r+');
|
2021-04-02 19:27:19 +00:00
|
|
|
const content = fs.readFileSync(fd, { encoding: 'utf8' });
|
2020-11-22 09:39:25 +00:00
|
|
|
|
2021-04-02 19:27:19 +00:00
|
|
|
const replaced = content.replace(mark, value);
|
|
|
|
if (replaced !== content) {
|
|
|
|
fs.writeSync(fd, replaced, 0, 'utf8');
|
|
|
|
}
|
2020-11-22 09:39:25 +00:00
|
|
|
fs.closeSync(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
const stability = collectStability(data);
|
|
|
|
|
|
|
|
// add markdown
|
|
|
|
const markdownTable = createMarkdownTable(stability);
|
2021-04-02 19:27:19 +00:00
|
|
|
updateStabilityMark(output.docMarkdown,
|
|
|
|
`${markBegin}\n${markdownTable}\n${markEnd}`,
|
|
|
|
new RegExp(mark, 's'));
|
2020-11-22 09:39:25 +00:00
|
|
|
|
|
|
|
// add html table
|
|
|
|
const html = createHTML(markdownTable);
|
2021-04-02 19:27:19 +00:00
|
|
|
updateStabilityMark(output.docHTML, `${markBegin}${html}${markEnd}`,
|
|
|
|
new RegExp(mark, 's'));
|
2020-11-22 09:39:25 +00:00
|
|
|
|
|
|
|
// add json output
|
2021-04-02 19:27:19 +00:00
|
|
|
updateStabilityMark(output.docJSON,
|
|
|
|
JSON.stringify(`${markBegin}${html}${markEnd}`),
|
|
|
|
new RegExp(JSON.stringify(mark), 's'));
|