|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Build all.html by combining the generated toc and apicontent from each |
| 4 | +// of the generated html files. |
| 5 | + |
| 6 | +const fs = require('fs'); |
| 7 | + |
| 8 | +const source = `${__dirname}/../../out/doc/api`; |
| 9 | + |
| 10 | +// Get a list of generated API documents. |
| 11 | +const htmlFiles = fs.readdirSync(source, 'utf8') |
| 12 | + .filter((name) => name.includes('.html') && name !== 'all.html'); |
| 13 | + |
| 14 | +// Read the table of contents. |
| 15 | +const toc = fs.readFileSync(source + '/_toc.html', 'utf8'); |
| 16 | + |
| 17 | +// Extract (and concatenate) the toc and apicontent from each document. |
| 18 | +let contents = ''; |
| 19 | +let apicontent = ''; |
| 20 | + |
| 21 | +// Identify files that should be skipped. As files are processed, they |
| 22 | +// are added to this list to prevent dupes. |
| 23 | +const seen = { |
| 24 | + 'all.html': true, |
| 25 | + 'index.html': true |
| 26 | +}; |
| 27 | + |
| 28 | +for (const link of toc.match(/<a.*?>/g)) { |
| 29 | + const href = /href="(.*?)"/.exec(link)[1]; |
| 30 | + if (!htmlFiles.includes(href) || seen[href]) continue; |
| 31 | + const data = fs.readFileSync(source + '/' + href, 'utf8'); |
| 32 | + |
| 33 | + // Split the doc. |
| 34 | + const match = /(<\/ul>\s*)?<\/div>\s*<div id="apicontent">/.exec(data); |
| 35 | + |
| 36 | + contents += data.slice(0, match.index) |
| 37 | + .replace(/[\s\S]*?<div id="toc">\s*<h2>.*?<\/h2>\s*(<ul>\s*)?/, ''); |
| 38 | + |
| 39 | + apicontent += data.slice(match.index + match[0].length) |
| 40 | + .replace(/(<\/div>\s*)*\s*<script[\s\S]*/, '') |
| 41 | + .replace(/<a href="(\w[^#"]*)#/g, (match, href) => { |
| 42 | + return htmlFiles.includes(href) ? '<a href="#' : match; |
| 43 | + }) |
| 44 | + .trim() + '\n'; |
| 45 | + |
| 46 | + // Mark source as seen. |
| 47 | + seen[href] = true; |
| 48 | +} |
| 49 | + |
| 50 | +// Replace various mentions of _toc with all. |
| 51 | +let all = toc.replace(/_toc\.html/g, 'all.html') |
| 52 | + .replace('_toc.json', 'all.json') |
| 53 | + .replace('api-section-_toc', 'api-section-all') |
| 54 | + .replace('data-id="_toc"', 'data-id="all"'); |
| 55 | + |
| 56 | +// Clean up the title. |
| 57 | +all = all.replace(/<title>.*?\| /, '<title>'); |
| 58 | + |
| 59 | +// Insert the combined table of contents. |
| 60 | +const tocStart = /<div id="toc">\s*<h2>.*?<\/h2>\s*/.exec(all); |
| 61 | +all = all.slice(0, tocStart.index + tocStart[0].length) + |
| 62 | + '<ul>\n' + contents + '</ul>\n' + |
| 63 | + all.slice(tocStart.index + tocStart[0].length); |
| 64 | + |
| 65 | +// Replace apicontent with the concatenated set of apicontents from each source. |
| 66 | +const apiStart = /<div id="apicontent">\s*/.exec(all); |
| 67 | +const apiEnd = /(\s*<\/div>)*\s*<script /.exec(all); |
| 68 | +all = all.slice(0, apiStart.index + apiStart[0].length) + |
| 69 | + apicontent + |
| 70 | + all.slice(apiEnd.index); |
| 71 | + |
| 72 | +// Write results. |
| 73 | +fs.writeFileSync(source + '/all.html', all, 'utf8'); |
0 commit comments