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';
|
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.
|
|
|
|
const seen = {
|
|
|
|
'all.html': true,
|
|
|
|
'index.html': true
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const link of toc.match(/<a.*?>/g)) {
|
|
|
|
const href = /href="(.*?)"/.exec(link)[1];
|
|
|
|
if (!htmlFiles.includes(href) || seen[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
|
|
|
|
|
|
|
contents += data.slice(0, match.index)
|
2021-01-12 15:29:21 +00:00
|
|
|
.replace(/[\s\S]*?id="toc"[^>]*>\s*<\w+>.*?<\/\w+>\s*(<ul>\s*)?/, '');
|
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>')
|
2018-06-27 18:14:54 +00:00
|
|
|
.replace(/<a href="(\w[^#"]*)#/g, (match, href) => {
|
|
|
|
return htmlFiles.includes(href) ? '<a href="#' : match;
|
|
|
|
})
|
|
|
|
.trim() + '\n';
|
|
|
|
|
|
|
|
// Mark source as seen.
|
|
|
|
seen[href] = true;
|
|
|
|
}
|
|
|
|
|
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 -->');
|
2018-06-27 18:14:54 +00:00
|
|
|
all = all.slice(0, apiStart.index + apiStart[0].length) +
|
|
|
|
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.
|
|
|
|
const ids = new Set();
|
2021-07-18 14:04:00 +00:00
|
|
|
const idRe = / id="([^"]+)"/g;
|
2018-07-19 16:12:38 +00:00
|
|
|
let match;
|
|
|
|
while (match = idRe.exec(all)) {
|
|
|
|
ids.add(match[1]);
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:04:00 +00:00
|
|
|
const hrefRe = / href="#([^"]+)"/g;
|
2018-07-19 16:12:38 +00:00
|
|
|
while (match = hrefRe.exec(all)) {
|
|
|
|
if (!ids.has(match[1])) throw new Error(`link not found: ${match[1]}`);
|
|
|
|
}
|