Skip to content

Commit c41e94f

Browse files
committed
doc: list the stability status of each API
Fixes: #23723
1 parent f7dd330 commit c41e94f

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ doc-only: tools/doc/node_modules \
696696
@if [ "$(shell $(node_use_openssl))" != "true" ]; then \
697697
echo "Skipping doc-only (no crypto)"; \
698698
else \
699-
$(MAKE) out/doc/api/all.html out/doc/api/all.json; \
699+
$(MAKE) out/doc/api/all.html out/doc/api/all.json out/doc/api/stability; \
700700
fi
701701

702702
.PHONY: doc
@@ -749,6 +749,9 @@ out/doc/api/all.html: $(apidocs_html) tools/doc/allhtml.js \
749749
out/doc/api/all.json: $(apidocs_json) tools/doc/alljson.js | out/doc/api
750750
$(call available-node, tools/doc/alljson.js)
751751

752+
out/doc/api/stability: out/doc/api/all.json tools/doc/stability.js | out/doc/api
753+
$(call available-node, tools/doc/stability.js)
754+
752755
.PHONY: docopen
753756
docopen: out/doc/api/all.html
754757
@$(PYTHON) -mwebbrowser file://$(abspath $<)

doc/api/documentation.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Bugs or behavior changes may surprise users when Experimental API
4343
modifications occur. To avoid surprises, use of an Experimental feature may need
4444
a command-line flag. Experimental features may also emit a [warning][].
4545

46+
## Stability overview
47+
<!-- STABILITY_OVERVIEW_SLOT -->
48+
4649
## JSON output
4750
<!-- YAML
4851
added: v0.6.12

doc/api_assets/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ ol.version-picker li:last-child a {
211211
background-color: #5a8147;
212212
}
213213

214+
.module_stability {
215+
vertical-align: middle;
216+
}
217+
214218
.api_metadata {
215219
font-size: .85rem;
216220
margin-bottom: 1rem;

tools/doc/alljson.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ for (const link of toc.match(/<a.*?>/g)) {
4343

4444
for (const property in data) {
4545
if (results.hasOwnProperty(property)) {
46+
data[property].forEach((mod) => {
47+
mod.source = data.source;
48+
});
4649
results[property].push(...data[property]);
4750
}
4851
}

tools/doc/stability.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
return stability;
43+
}
44+
45+
function createMarkdownTable(data) {
46+
const md = ['| API | Stability |', '| --- | --------- |'];
47+
48+
for (const mod of data) {
49+
md.push(`| [${mod.api}](${mod.link}) | ${mod.stabilityText} |`);
50+
}
51+
52+
return md.join('\n');
53+
}
54+
55+
function createHTML(md) {
56+
const file = unified()
57+
.use(markdown)
58+
.use(gfm)
59+
.use(remark2rehype, { allowDangerousHtml: true })
60+
.use(raw)
61+
.use(htmlStringify)
62+
.use(processStability)
63+
.processSync(md);
64+
65+
return file.contents.trim();
66+
}
67+
68+
function processStability() {
69+
return (tree) => {
70+
visit(tree, { type: 'element', tagName: 'tr' }, (node) => {
71+
const [api, stability] = node.children;
72+
if (api.tagName !== 'td') {
73+
return;
74+
}
75+
76+
api.properties.class = 'module_stability';
77+
78+
const level = stability.children[0].value[1];
79+
stability.properties.class = `api_stability api_stability_${level}`;
80+
});
81+
};
82+
}
83+
84+
function updateStabilityMark(file, value, mark) {
85+
const content = fs.readFileSync(file, { encoding: 'utf-8' });
86+
const newContent = content.replace(mark, value);
87+
fs.writeFileSync(file, newContent, { encoding: 'utf-8' });
88+
}
89+
90+
const stability = collectStability(data);
91+
92+
// add markdown
93+
const markdownTable = createMarkdownTable(stability);
94+
updateStabilityMark(output.docMarkdown, markdownTable, mark);
95+
96+
// add html table
97+
const html = createHTML(markdownTable);
98+
updateStabilityMark(output.docHTML, html, mark);
99+
100+
// add json output
101+
updateStabilityMark(output.docJSON, JSON.stringify(html), `"${mark}"`);

0 commit comments

Comments
 (0)