@@ -22,7 +22,9 @@ let apicontent = '';
2222const seen = new Set ( [ 'all.html' , 'index.html' ] ) ;
2323
2424for ( const link of toc . match ( / < a .* ?> / g) ) {
25- const href = / h r e f = " ( .* ?) " / . exec ( link ) [ 1 ] ;
25+ const hrefMatch = / h r e f = " ( .* ?) " / . exec ( link ) ;
26+ if ( ! hrefMatch ) continue ;
27+ const href = hrefMatch [ 1 ] ;
2628 if ( ! htmlFiles . includes ( href ) || seen . has ( href ) ) continue ;
2729 const data = fs . readFileSync ( new URL ( `./${ href } ` , source ) , 'utf8' ) ;
2830
@@ -69,6 +71,11 @@ for (const link of toc.match(/<a.*?>/g)) {
6971 seen . add ( href ) ;
7072}
7173
74+ const aggregatedToc =
75+ '<details role="navigation" id="toc" open><summary>Table of contents</summary>\n' +
76+ '<ul>\n' + contents + '</ul>\n' +
77+ '</details>\n' ;
78+
7279// Replace various mentions of index with all.
7380let all = toc . replace ( / i n d e x \. h t m l / g, 'all.html' )
7481 . replace ( '<a href="all.html">' , '<a href="index.html">' )
@@ -81,12 +88,28 @@ let all = toc.replace(/index\.html/g, 'all.html')
8188all = all . replace ( / < t i t l e > .* ?\| / , '<title>' ) ;
8289
8390// Insert the combined table of contents.
84- const tocStart = / < ! - - T O C - - > / . exec ( all ) ;
85- all = all . slice ( 0 , tocStart . index + tocStart [ 0 ] . length ) +
86- '<details id="toc" open><summary>Table of contents</summary>\n' +
87- '<ul>\n' + contents + '</ul>\n' +
88- '</details>\n' +
89- all . slice ( tocStart . index + tocStart [ 0 ] . length ) ;
91+ const tocPlaceholder = '<!-- TOC -->' ;
92+ if ( all . includes ( tocPlaceholder ) ) {
93+ all = all . replace (
94+ tocPlaceholder ,
95+ `${ tocPlaceholder } \n${ aggregatedToc } ` ,
96+ ) ;
97+ } else {
98+ const tocElement = / < d e t a i l s [ ^ > ] + i d = " t o c " [ ^ > ] * > [ \s \S ] * ?< \/ d e t a i l s > / ;
99+ if ( ! tocElement . test ( all ) ) {
100+ throw new Error ( 'Failed to locate a TOC container in index.html' ) ;
101+ }
102+ all = all . replace ( tocElement , aggregatedToc ) ;
103+ }
104+
105+ const tocPickerRegex =
106+ / < d i v c l a s s = " t o c " > < u l i d = " t o c - p i c k e r " > [ \s \S ] * ?< \/ u l > \s * < \/ d i v > / ;
107+ if ( tocPickerRegex . test ( all ) ) {
108+ all = all . replace (
109+ tocPickerRegex ,
110+ `<div class="toc"><ul id="toc-picker">\n${ contents } </ul></div>` ,
111+ ) ;
112+ }
90113
91114// Replace apicontent with the concatenated set of apicontents from each source.
92115const apiStart = / < \w + r o l e = " m a i n " i d = " a p i c o n t e n t " > \s * / . exec ( all ) ;
0 commit comments