node/tools/doc/alljson.mjs
Michaël Zasso a1d0aef60e
tools: update doctool dependencies, migrate to ESM
- Migrated to ESM because some dependencies now require it.
- Did not update `highlight.js` to v11 because it has many breaking
  changes.
- Used non-deprecated `highlight.js` API.

Refs: https://github.com/highlightjs/highlight.js/issues/2277
Fixes: https://github.com/nodejs/node/issues/38938
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/38966
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2021-06-14 19:31:05 -07:00

58 lines
1.6 KiB
JavaScript

// Build all.json by combining the miscs, modules, classes, globals, and methods
// from the generated json files.
import fs from 'fs';
const source = new URL('../../out/doc/api/', import.meta.url);
// Get a list of generated API documents.
const jsonFiles = fs.readdirSync(source, 'utf8')
.filter((name) => name.includes('.json') && name !== 'all.json');
// Read the table of contents.
const toc = fs.readFileSync(new URL('./index.html', source), 'utf8');
// Initialize results. Only these four data values will be collected.
const results = {
miscs: [],
modules: [],
classes: [],
globals: [],
methods: []
};
// Identify files that should be skipped. As files are processed, they
// are added to this list to prevent dupes.
const seen = {
'all.json': true,
'index.json': true
};
// Extract (and concatenate) the selected data from each document.
// Expand hrefs found in json to include source HTML file.
for (const link of toc.match(/<a.*?>/g)) {
const href = /href="(.*?)"/.exec(link)[1];
const json = href.replace('.html', '.json');
if (!jsonFiles.includes(json) || seen[json]) continue;
const data = JSON.parse(
fs.readFileSync(new URL(`./${json}`, source), 'utf8')
.replace(/<a href=\\"#/g, `<a href=\\"${href}#`)
);
for (const property in data) {
if (results.hasOwnProperty(property)) {
data[property].forEach((mod) => {
mod.source = data.source;
});
results[property].push(...data[property]);
}
}
// Mark source as seen.
seen[json] = true;
}
// Write results.
fs.writeFileSync(new URL('./all.json', source),
`${JSON.stringify(results, null, 2)}\n`, 'utf8');