// Build all.html by combining the generated toc and apicontent from each
// of the generated html files.
import fs from 'fs';
const source = new URL('../../out/doc/api/', import.meta.url);
// Get a list of generated API documents.
const htmlFiles = fs.readdirSync(source, 'utf8')
.filter((name) => name.includes('.html') && name !== 'all.html');
// Read the table of contents.
const toc = fs.readFileSync(new URL('./index.html', source), 'utf8');
// Extract (and concatenate) the toc and apicontent from each document.
let contents = '';
let apicontent = '';
// Identify files that should be skipped. As files are processed, they
// are added to this list to prevent dupes.
const seen = new Set(['all.html', 'index.html']);
for (const link of toc.match(//g)) {
const href = /href="(.*?)"/.exec(link)[1];
if (!htmlFiles.includes(href) || seen.has(href)) continue;
const data = fs.readFileSync(new URL(`./${href}`, source), 'utf8');
// Split the doc.
const match = /(<\/ul>\s*)?<\/\w+>\s*<\w+ id="apicontent">/.exec(data);
// Get module name
const moduleName = href.replace(/\.html$/, '');
contents += data.slice(0, match.index)
.replace(/[\s\S]*?id="toc"[^>]*>\s*<\w+>.*?<\/\w+>\s*(\s*)?/, '')
// Prefix TOC links with current module name
.replace(/ {
return `' + data.slice(match.index + match[0].length)
.replace(/[\s\S]*/, '')
// Prefix all in-page anchor marks with module name
.replace(/ {
if (anchor !== id) throw new Error(`Mark does not match: ${anchor} should match ${id}`);
return ` {
return ` {
return ` {
return `<${tagName} id="all_${moduleName}_${id}"`;
})
// Prefix all links to other docs modules with those module names
.replace(/ {
if (!htmlFiles.includes(href)) return match;
return `', '')
.replace('index.json', 'all.json')
.replace('api-section-index', 'api-section-all')
.replace('data-id="index"', 'data-id="all"')
.replace(/- .*?<\/li>/, '');
// Clean up the title.
all = all.replace(/.*?\| /, '');
// Insert the combined table of contents.
const tocStart = //.exec(all);
all = all.slice(0, tocStart.index + tocStart[0].length) +
'
Table of contents
\n' +
'\n' +
' \n' +
all.slice(tocStart.index + tocStart[0].length);
// Replace apicontent with the concatenated set of apicontents from each source.
const apiStart = /<\w+ id="apicontent">\s*/.exec(all);
const apiEnd = all.lastIndexOf('');
all = all.slice(0, apiStart.index + apiStart[0].length) +
apicontent +
all.slice(apiEnd);
// Write results.
fs.writeFileSync(new URL('./all.html', source), all, 'utf8');
// Validate all hrefs have a target.
const idRe = / id="([^"]+)"/g;
const ids = new Set([...all.matchAll(idRe)].map((match) => match[1]));
const hrefRe = / href="#([^"]+)"/g;
const hrefMatches = all.matchAll(hrefRe);
for (const match of hrefMatches) {
if (!ids.has(match[1])) throw new Error(`link not found: ${match[1]}`);
}