From 17bdcd9d186b7b5f96281477b8505411292a24f3 Mon Sep 17 00:00:00 2001 From: Zijian Liu Date: Sun, 22 Nov 2020 17:39:25 +0800 Subject: [PATCH] tools,doc: list the stability status of each API Fixes: https://github.com/nodejs/node/issues/23723 PR-URL: https://github.com/nodejs/node/pull/36223 Reviewed-By: Joyee Cheung Reviewed-By: Gireesh Punathil Reviewed-By: Rich Trott Reviewed-By: Myles Borins Reviewed-By: Franziska Hinkelmann Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- Makefile | 6 ++- doc/api/documentation.md | 3 ++ doc/api_assets/style.css | 4 ++ tools/doc/alljson.js | 3 ++ tools/doc/stability.js | 111 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 tools/doc/stability.js diff --git a/Makefile b/Makefile index e6ad96da91474d..aa3eb1567d447e 100644 --- a/Makefile +++ b/Makefile @@ -696,7 +696,7 @@ doc-only: tools/doc/node_modules \ @if [ "$(shell $(node_use_openssl))" != "true" ]; then \ echo "Skipping doc-only (no crypto)"; \ else \ - $(MAKE) out/doc/api/all.html out/doc/api/all.json; \ + $(MAKE) out/doc/api/all.html out/doc/api/all.json out/doc/api/stability; \ fi .PHONY: doc @@ -749,6 +749,10 @@ out/doc/api/all.html: $(apidocs_html) tools/doc/allhtml.js \ out/doc/api/all.json: $(apidocs_json) tools/doc/alljson.js | out/doc/api $(call available-node, tools/doc/alljson.js) +.PHONY: out/doc/api/stability +out/doc/api/stability: out/doc/api/all.json tools/doc/stability.js | out/doc/api + $(call available-node, tools/doc/stability.js) + .PHONY: docopen docopen: out/doc/api/all.html @$(PYTHON) -mwebbrowser file://$(abspath $<) diff --git a/doc/api/documentation.md b/doc/api/documentation.md index 979d85ef0a3005..d39aa30438aefc 100644 --- a/doc/api/documentation.md +++ b/doc/api/documentation.md @@ -43,6 +43,9 @@ Bugs or behavior changes may surprise users when Experimental API modifications occur. To avoid surprises, use of an Experimental feature may need a command-line flag. Experimental features may also emit a [warning][]. +## Stability overview + + ## JSON output '; + +const output = { + json: path.join(source, 'stability.json'), + docHTML: path.join(source, 'documentation.html'), + docJSON: path.join(source, 'documentation.json'), + docMarkdown: path.join(source, 'documentation.md'), +}; + +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'); + // const link = re.exec(toc)[1] + stability.push({ + api: mod.name, + link: link, + stability: mod.stability, + stabilityText: `(${mod.stability}) ${mod.stabilityText}`, + }); + } + } + + stability.sort((a, b) => a.api.localeCompare(b.api)); + return stability; +} + +function createMarkdownTable(data) { + const md = ['| API | Stability |', '| --- | --------- |']; + + for (const mod of data) { + md.push(`| [${mod.api}](${mod.link}) | ${mod.stabilityText} |`); + } + + 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); + + return file.contents.trim(); +} + +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+'); + const content = fs.readFileSync(fd); + + // Find the position of the `mark`. + const index = content.indexOf(mark); + + // Overwrite the mark with `value` parameter. + const offset = fs.writeSync(fd, value, index, 'utf-8'); + + // Re-write the end of the file after `value`. + fs.writeSync(fd, content, index + mark.length, undefined, index + offset); + fs.closeSync(fd); +} + +const stability = collectStability(data); + +// add markdown +const markdownTable = createMarkdownTable(stability); +updateStabilityMark(output.docMarkdown, markdownTable, mark); + +// add html table +const html = createHTML(markdownTable); +updateStabilityMark(output.docHTML, html, mark); + +// add json output +updateStabilityMark(output.docJSON, JSON.stringify(html), JSON.stringify(mark));