From 8099bfb35c10f7ad4c7498312cd008e0ca84d6f1 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 23 Jan 2021 18:58:26 -0800 Subject: [PATCH] doc: remove TOC summary for pages with no TOC Remove the table of contents summary for pages with no table of contents. Currently, this affects at least index.html. PR-URL: https://github.com/nodejs/node/pull/37043 Reviewed-By: Antoine du Hamel --- doc/template.html | 5 +---- tools/doc/allhtml.js | 4 +++- tools/doc/html.js | 20 +++++++++++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/doc/template.html b/doc/template.html index 391b1a0ec7b924..f57bf50bb67334 100644 --- a/doc/template.html +++ b/doc/template.html @@ -55,10 +55,7 @@

Node.js __VERSION__ Documentation


-
- Table of Contents - __TOC__ -
+ __TOC__
__CONTENT__ diff --git a/tools/doc/allhtml.js b/tools/doc/allhtml.js index 5a24cee4292656..93197f812e10c6 100644 --- a/tools/doc/allhtml.js +++ b/tools/doc/allhtml.js @@ -59,9 +59,11 @@ let all = toc.replace(/index\.html/g, 'all.html') all = all.replace(/.*?\| /, '<title>'); // Insert the combined table of contents. -const tocStart = /<\w+ id="toc"[^>]*>\s*<\w+>.*?<\/\w+>\s*/.exec(all); +const tocStart = /<!-- TOC -->/.exec(all); all = all.slice(0, tocStart.index + tocStart[0].length) + + '<details id="toc" open><summary>Table of contents</summary>\n' + '<ul>\n' + contents + '</ul>\n' + + '</details>\n' + all.slice(tocStart.index + tocStart[0].length); // Replace apicontent with the concatenated set of apicontents from each source. diff --git a/tools/doc/html.js b/tools/doc/html.js index d35fd47b601e09..6eb3484052bc69 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -382,13 +382,19 @@ function buildToc({ filename, apilinks }) { node.children.push({ type: 'html', value: anchor }); }); - file.toc = unified() - .use(markdown) - .use(gfm) - .use(remark2rehype, { allowDangerousHtml: true }) - .use(raw) - .use(htmlStringify) - .processSync(toc).toString(); + if (toc !== '') { + file.toc = '<details id="toc" open><summary>Table of contents</summary>' + + unified() + .use(markdown) + .use(gfm) + .use(remark2rehype, { allowDangerousHtml: true }) + .use(raw) + .use(htmlStringify) + .processSync(toc).toString() + + '</details>'; + } else { + file.toc = '<!-- TOC -->'; + } }; }