2018-06-27 18:14:54 +00:00
|
|
|
// Build all.html by combining the generated toc and apicontent from each
|
|
|
|
// of the generated html files.
|
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
import fs from 'fs';
|
2022-06-27 16:31:44 +00:00
|
|
|
import buildCSSForFlavoredJS from './buildCSSForFlavoredJS.mjs';
|
2018-06-27 18:14:54 +00:00
|
|
|
|
2021-06-08 10:44:33 +00:00
|
|
|
const source = new URL('../../out/doc/api/', import.meta.url);
|
2018-06-27 18:14:54 +00:00
|
|
|
|
|
|
|
// 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.
|
2021-06-08 10:44:33 +00:00
|
|
|
const toc = fs.readFileSync(new URL('./index.html', source), 'utf8');
|
2018-06-27 18:14:54 +00:00
|
|
|
|
|
|
|
// 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.
|
2022-01-24 11:46:47 +00:00
|
|
|
const seen = new Set(['all.html', 'index.html']);
|
2018-06-27 18:14:54 +00:00
|
|
|
|
|
|
|
for (const link of toc.match(/<a.*?>/g)) {
|
|
|
|
const href = /href="(.*?)"/.exec(link)[1];
|
2022-01-24 11:46:47 +00:00
|
|
|
if (!htmlFiles.includes(href) || seen.has(href)) continue;
|
2021-06-08 10:44:33 +00:00
|
|
|
const data = fs.readFileSync(new URL(`./${href}`, source), 'utf8');
|
2018-06-27 18:14:54 +00:00
|
|
|
|
|
|
|
// Split the doc.
|
2021-01-12 15:29:21 +00:00
|
|
|
const match = /(<\/ul>\s*)?<\/\w+>\s*<\w+ id="apicontent">/.exec(data);
|
2018-06-27 18:14:54 +00:00
|
|
|
|
2021-07-05 03:39:17 +00:00
|
|
|
// Get module name
|
|
|
|
const moduleName = href.replace(/\.html$/, '');
|
|
|
|
|
2018-06-27 18:14:54 +00:00
|
|
|
contents += data.slice(0, match.index)
|
2021-07-05 03:39:17 +00:00
|
|
|
.replace(/[\s\S]*?id="toc"[^>]*>\s*<\w+>.*?<\/\w+>\s*(<ul>\s*)?/, '')
|
|
|
|
// Prefix TOC links with current module name
|
|
|
|
.replace(/<a href="#(?!DEP[0-9]{4})([^"]+)"/g, (match, anchor) => {
|
2021-12-23 01:32:10 +00:00
|
|
|
return `<a href="#all_${moduleName}_${anchor}"`;
|
2021-07-05 03:39:17 +00:00
|
|
|
});
|
2018-06-27 18:14:54 +00:00
|
|
|
|
2021-02-09 21:41:47 +00:00
|
|
|
apicontent += '<section>' + data.slice(match.index + match[0].length)
|
|
|
|
.replace(/<!-- API END -->[\s\S]*/, '</section>')
|
2021-07-05 03:39:17 +00:00
|
|
|
// Prefix all in-page anchor marks with module name
|
|
|
|
.replace(/<a class="mark" href="#([^"]+)" id="([^"]+)"/g, (match, anchor, id) => {
|
|
|
|
if (anchor !== id) throw new Error(`Mark does not match: ${anchor} should match ${id}`);
|
2021-12-23 01:32:10 +00:00
|
|
|
return `<a class="mark" href="#all_${moduleName}_${anchor}" id="all_${moduleName}_${anchor}"`;
|
2021-07-05 03:39:17 +00:00
|
|
|
})
|
|
|
|
// Prefix all in-page links with current module name
|
|
|
|
.replace(/<a href="#(?!DEP[0-9]{4})([^"]+)"/g, (match, anchor) => {
|
2021-12-23 01:32:10 +00:00
|
|
|
return `<a href="#all_${moduleName}_${anchor}"`;
|
2021-07-05 03:39:17 +00:00
|
|
|
})
|
2021-10-16 04:45:29 +00:00
|
|
|
// Update footnote id attributes on anchors
|
|
|
|
.replace(/<a href="([^"]+)" id="(user-content-fn[^"]+)"/g, (match, href, id) => {
|
2021-12-23 01:32:10 +00:00
|
|
|
return `<a href="${href}" id="all_${moduleName}_${id}"`;
|
2021-10-16 04:45:29 +00:00
|
|
|
})
|
|
|
|
// Update footnote id attributes on list items
|
2021-12-23 01:32:10 +00:00
|
|
|
.replace(/<(\S+) id="(user-content-fn[^"]+)"/g, (match, tagName, id) => {
|
|
|
|
return `<${tagName} id="all_${moduleName}_${id}"`;
|
2021-10-16 04:45:29 +00:00
|
|
|
})
|
2021-07-05 03:39:17 +00:00
|
|
|
// Prefix all links to other docs modules with those module names
|
|
|
|
.replace(/<a href="((\w[^#"]*)\.html)#/g, (match, href, linkModule) => {
|
|
|
|
if (!htmlFiles.includes(href)) return match;
|
2021-12-23 01:32:10 +00:00
|
|
|
return `<a href="#all_${linkModule}_`;
|
2018-06-27 18:14:54 +00:00
|
|
|
})
|
|
|
|
.trim() + '\n';
|
|
|
|
|
|
|
|
// Mark source as seen.
|
2022-01-24 11:46:47 +00:00
|
|
|
seen.add(href);
|
2018-06-27 18:14:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 16:46:56 +00:00
|
|
|
// Replace various mentions of index with all.
|
|
|
|
let all = toc.replace(/index\.html/g, 'all.html')
|
2020-08-23 19:26:41 +00:00
|
|
|
.replace('<a href="all.html">', '<a href="index.html">')
|
2018-07-03 16:46:56 +00:00
|
|
|
.replace('index.json', 'all.json')
|
|
|
|
.replace('api-section-index', 'api-section-all')
|
2018-07-07 22:33:06 +00:00
|
|
|
.replace('data-id="index"', 'data-id="all"')
|
|
|
|
.replace(/<li class="edit_on_github">.*?<\/li>/, '');
|
2018-06-27 18:14:54 +00:00
|
|
|
|
|
|
|
// Clean up the title.
|
|
|
|
all = all.replace(/<title>.*?\| /, '<title>');
|
|
|
|
|
|
|
|
// Insert the combined table of contents.
|
2021-01-24 02:58:26 +00:00
|
|
|
const tocStart = /<!-- TOC -->/.exec(all);
|
2018-06-27 18:14:54 +00:00
|
|
|
all = all.slice(0, tocStart.index + tocStart[0].length) +
|
2021-01-24 02:58:26 +00:00
|
|
|
'<details id="toc" open><summary>Table of contents</summary>\n' +
|
2018-06-27 18:14:54 +00:00
|
|
|
'<ul>\n' + contents + '</ul>\n' +
|
2021-01-24 02:58:26 +00:00
|
|
|
'</details>\n' +
|
2018-06-27 18:14:54 +00:00
|
|
|
all.slice(tocStart.index + tocStart[0].length);
|
|
|
|
|
|
|
|
// Replace apicontent with the concatenated set of apicontents from each source.
|
2021-01-12 15:29:21 +00:00
|
|
|
const apiStart = /<\w+ id="apicontent">\s*/.exec(all);
|
2020-06-30 19:35:28 +00:00
|
|
|
const apiEnd = all.lastIndexOf('<!-- API END -->');
|
2022-06-27 16:31:44 +00:00
|
|
|
all = all.slice(0, apiStart.index + apiStart[0].length)
|
|
|
|
.replace(
|
|
|
|
'\n</head>',
|
|
|
|
buildCSSForFlavoredJS(new Set(Array.from(
|
|
|
|
apicontent.matchAll(/(?<=<pre class="with-)\d+(?=-chars">)/g),
|
|
|
|
(x) => Number(x[0])
|
|
|
|
))) + '\n</head>'
|
|
|
|
) +
|
2018-06-27 18:14:54 +00:00
|
|
|
apicontent +
|
2020-06-30 19:35:28 +00:00
|
|
|
all.slice(apiEnd);
|
2018-06-27 18:14:54 +00:00
|
|
|
|
|
|
|
// Write results.
|
2021-06-08 10:44:33 +00:00
|
|
|
fs.writeFileSync(new URL('./all.html', source), all, 'utf8');
|
2018-07-19 16:12:38 +00:00
|
|
|
|
|
|
|
// Validate all hrefs have a target.
|
2021-07-18 14:04:00 +00:00
|
|
|
const idRe = / id="([^"]+)"/g;
|
2022-01-09 13:32:56 +00:00
|
|
|
const ids = new Set([...all.matchAll(idRe)].map((match) => match[1]));
|
2018-07-19 16:12:38 +00:00
|
|
|
|
2021-07-18 14:04:00 +00:00
|
|
|
const hrefRe = / href="#([^"]+)"/g;
|
2022-01-09 03:46:56 +00:00
|
|
|
const hrefMatches = all.matchAll(hrefRe);
|
|
|
|
for (const match of hrefMatches) {
|
2018-07-19 16:12:38 +00:00
|
|
|
if (!ids.has(match[1])) throw new Error(`link not found: ${match[1]}`);
|
|
|
|
}
|