• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Build stability table to documentation.html/json/md by generated all.json
2
3import fs from 'fs';
4
5import raw from 'rehype-raw';
6import htmlStringify from 'rehype-stringify';
7import gfm from 'remark-gfm';
8import markdown from 'remark-parse';
9import remark2rehype from 'remark-rehype';
10import unified from 'unified';
11import { visit } from 'unist-util-visit';
12
13const source = new URL('../../out/doc/api/', import.meta.url);
14const data = JSON.parse(fs.readFileSync(new URL('./all.json', source), 'utf8'));
15const markBegin = '<!-- STABILITY_OVERVIEW_SLOT_BEGIN -->';
16const markEnd = '<!-- STABILITY_OVERVIEW_SLOT_END -->';
17const mark = `${markBegin}(.*)${markEnd}`;
18
19const output = {
20  json: new URL('./stability.json', source),
21  docHTML: new URL('./documentation.html', source),
22  docJSON: new URL('./documentation.json', source),
23  docMarkdown: new URL('./documentation.md', source),
24};
25
26function 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
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
46function 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
56function 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
69function 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
85function updateStabilityMark(file, value, mark) {
86  const fd = fs.openSync(file, 'r+');
87  const content = fs.readFileSync(fd, { encoding: 'utf8' });
88
89  const replaced = content.replace(mark, value);
90  if (replaced !== content) {
91    fs.writeSync(fd, replaced, 0, 'utf8');
92  }
93  fs.closeSync(fd);
94}
95
96const stability = collectStability(data);
97
98// add markdown
99const markdownTable = createMarkdownTable(stability);
100updateStabilityMark(output.docMarkdown,
101                    `${markBegin}\n${markdownTable}\n${markEnd}`,
102                    new RegExp(mark, 's'));
103
104// add html table
105const html = createHTML(markdownTable);
106updateStabilityMark(output.docHTML, `${markBegin}${html}${markEnd}`,
107                    new RegExp(mark, 's'));
108
109// add json output
110updateStabilityMark(output.docJSON,
111                    JSON.stringify(`${markBegin}${html}${markEnd}`),
112                    new RegExp(JSON.stringify(mark), 's'));
113