Skip to content

Commit 4cd471e

Browse files
Lxxyxruyadorno
authored andcommitted
tools,doc: list the stability status of each API
Fixes: #23723 PR-URL: #36223 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 0c9e2d0 commit 4cd471e

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

Makefile

+5-1
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,10 @@ 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+
.PHONY: out/doc/api/stability
753+
out/doc/api/stability: out/doc/api/all.json tools/doc/stability.js | out/doc/api
754+
$(call available-node, tools/doc/stability.js)
755+
752756
.PHONY: docopen
753757
docopen: out/doc/api/all.html
754758
@$(PYTHON) -mwebbrowser file://$(abspath $<)

doc/api/documentation.md

+3
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

+4
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ ol.version-picker li:last-child a {
263263
background-color: var(--green2);
264264
}
265265

266+
.module_stability {
267+
vertical-align: middle;
268+
}
269+
266270
.api_metadata {
267271
font-size: .85rem;
268272
margin-bottom: 1rem;

tools/doc/alljson.js

+3
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

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)