0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/tools/doc/alljson.js
Sam Ruby 0c743b5f77 tools: build all.json by combining generated JSON
Notes:

1) Removed a number of root properties that did not seem relevant:
   source, desc, and introduced_in.  There no longer is a source, and
   the other two are from the first include and do not reflect the
   entire API.

2) As with https://github.com/nodejs/node/issues/20100, the current
   "desc" properties sometimes contained in-page links, other times
   referenced another page, and often did not match the links in the
   original HTML or JSON file. I chose to standardize on external links
   as "desc" values are isolated snippets as opposed to all.html which
   can be viewed as a standalone and self contained document.

3) Eliminated preprocessing for @include entirely, including the test
   case for this function.

4) _toc.md was renamed to index.md.

5) index comments no longer appear in embedded TOCs (left hand side
   column in the generated documentation.

PR-URL: https://github.com/nodejs/node/pull/21637
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2018-07-09 22:57:33 +03:00

57 lines
1.5 KiB
JavaScript

'use strict';
// Build all.json by combining the miscs, modules, classes, globals, and methods
// from the generated json files.
const fs = require('fs');
const source = `${__dirname}/../../out/doc/api`;
// 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(source + '/index.html', '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(source + '/' + json, 'utf8')
.replace(/<a href=\\"#/g, `<a href=\\"${href}#`)
);
for (const property in data) {
if (results.hasOwnProperty(property)) {
results[property].push(...data[property]);
}
}
// Mark source as seen.
seen[json] = true;
}
// Write results.
fs.writeFileSync(source + '/all.json',
`${JSON.stringify(results, null, 2)}\n`, 'utf8');