@@ -6,7 +6,7 @@ import { join, resolve } from 'node:path';
66import HTMLMinifier from '@minify-html/node' ;
77
88import { getRemarkRehype } from '../../utils/remark.mjs' ;
9- import dropdowns from '../legacy-html/utils/buildDropdowns .mjs' ;
9+ import { replaceTemplateValues } from '../legacy-html/utils/replaceTemplateValues .mjs' ;
1010import tableOfContents from '../legacy-html/utils/tableOfContents.mjs' ;
1111
1212/**
@@ -40,67 +40,109 @@ export default {
4040
4141 dependsOn : 'legacy-html' ,
4242
43+ /**
44+ * Process a chunk of template values from the dependency.
45+ * Extracts toc and content from each entry for aggregation.
46+ * @param {Input } fullInput
47+ * @param {number[] } itemIndices
48+ */
49+ processChunk ( fullInput , itemIndices ) {
50+ const results = [ ] ;
51+
52+ for ( const idx of itemIndices ) {
53+ const entry = fullInput [ idx ] ;
54+
55+ // Skip the index entry
56+ if ( entry . api === 'index' ) {
57+ continue ;
58+ }
59+
60+ results . push ( {
61+ api : entry . api ,
62+ section : entry . section ,
63+ toc : entry . toc ,
64+ content : entry . content ,
65+ } ) ;
66+ }
67+
68+ return results ;
69+ } ,
70+
4371 /**
4472 * Generates the `all.html` file from the `legacy-html` generator
4573 * @param {Input } input
4674 * @param {Partial<GeneratorOptions> } options
75+ * @returns {AsyncGenerator<Array<{api: string; section: string; toc: string; content: string}>> }
4776 */
48- async generate ( input , { version, releases, output } ) {
49- const inputWithoutIndex = input . filter ( entry => entry . api !== 'index' ) ;
50-
51- // Gets a Remark Processor that parses Markdown to minified HTML
52- const remarkWithRehype = getRemarkRehype ( ) ;
53-
54- // Current directory path relative to the `index.mjs` file
55- // from the `legacy-html` generator, as all the assets are there
56- const baseDir = resolve ( import . meta. dirname , '..' , 'legacy-html' ) ;
57-
58- // Reads the API template.html file to be used as a base for the HTML files
59- const apiTemplate = await readFile ( join ( baseDir , 'template.html' ) , 'utf-8' ) ;
60-
61- // Aggregates all individual Table of Contents into one giant string
62- const aggregatedToC = inputWithoutIndex . map ( entry => entry . toc ) . join ( '\n' ) ;
63-
64- // Aggregates all individual content into one giant string
65- const aggregatedContent = inputWithoutIndex
66- . map ( entry => entry . content )
67- . join ( '\n' ) ;
68-
69- // Creates a "mimic" of an `ApiDocMetadataEntry` which fulfils the requirements
70- // for generating the `tableOfContents` with the `tableOfContents.parseNavigationNode` parser
71- const sideNavigationFromValues = inputWithoutIndex . map ( entry => ( {
72- api : entry . api ,
73- heading : { data : { depth : 1 , name : entry . section } } ,
74- } ) ) ;
75-
76- // Generates the global Table of Contents (Sidebar Navigation)
77- const parsedSideNav = remarkWithRehype . processSync (
78- tableOfContents ( sideNavigationFromValues , {
79- maxDepth : 1 ,
80- parser : tableOfContents . parseNavigationNode ,
81- } )
82- ) ;
83-
84- const generatedAllTemplate = apiTemplate
85- . replace ( '__ID__' , 'all' )
86- . replace ( / _ _ F I L E N A M E _ _ / g, 'all' )
87- . replace ( '__SECTION__' , 'All' )
88- . replace ( / _ _ V E R S I O N _ _ / g, `v${ version . version } ` )
89- . replace ( / _ _ T O C _ _ / g, tableOfContents . wrapToC ( aggregatedToC ) )
90- . replace ( / _ _ G T O C _ _ / g, parsedSideNav )
91- . replace ( '__CONTENT__' , aggregatedContent )
92- . replace ( / _ _ T O C _ P I C K E R _ _ / g, dropdowns . buildToC ( aggregatedToC ) )
93- . replace ( / _ _ G T O C _ P I C K E R _ _ / g, '' )
94- . replace ( '__ALTDOCS__' , dropdowns . buildVersions ( 'all' , '' , releases ) )
95- . replace ( '__EDIT_ON_GITHUB__' , '' ) ;
96-
97- // We minify the html result to reduce the file size and keep it "clean"
98- const minified = HTMLMinifier . minify ( Buffer . from ( generatedAllTemplate ) , { } ) ;
77+ async * generate ( input , { version, releases, output, worker } ) {
78+ // Collect all chunks as they stream in
79+ const allChunks = [ ] ;
9980
81+ for await ( const chunkResult of worker . stream ( input , input , { } ) ) {
82+ allChunks . push ( ...chunkResult ) ;
83+
84+ yield chunkResult ;
85+ }
86+
87+ // After all chunks are collected, build and write the final file
10088 if ( output ) {
89+ // Gets a Remark Processor that parses Markdown to minified HTML
90+ const remarkWithRehype = getRemarkRehype ( ) ;
91+
92+ // Current directory path relative to the `index.mjs` file
93+ // from the `legacy-html` generator, as all the assets are there
94+ const baseDir = resolve ( import . meta. dirname , '..' , 'legacy-html' ) ;
95+
96+ // Reads the API template.html file to be used as a base for the HTML files
97+ const apiTemplate = await readFile (
98+ join ( baseDir , 'template.html' ) ,
99+ 'utf-8'
100+ ) ;
101+
102+ // Aggregates all individual Table of Contents into one giant string
103+ const aggregatedToC = allChunks . map ( entry => entry . toc ) . join ( '\n' ) ;
104+
105+ // Aggregates all individual content into one giant string
106+ const aggregatedContent = allChunks
107+ . map ( entry => entry . content )
108+ . join ( '\n' ) ;
109+
110+ // Creates a "mimic" of an `ApiDocMetadataEntry` which fulfils the requirements
111+ // for generating the `tableOfContents` with the `tableOfContents.parseNavigationNode` parser
112+ const sideNavigationFromValues = allChunks . map ( entry => ( {
113+ api : entry . api ,
114+ heading : { data : { depth : 1 , name : entry . section } } ,
115+ } ) ) ;
116+
117+ // Generates the global Table of Contents (Sidebar Navigation)
118+ const parsedSideNav = remarkWithRehype . processSync (
119+ tableOfContents ( sideNavigationFromValues , {
120+ maxDepth : 1 ,
121+ parser : tableOfContents . parseNavigationNode ,
122+ } )
123+ ) ;
124+
125+ const templateValues = {
126+ api : 'all' ,
127+ added : '' ,
128+ section : 'All' ,
129+ version : `v${ version . version } ` ,
130+ toc : aggregatedToC ,
131+ nav : String ( parsedSideNav ) ,
132+ content : aggregatedContent ,
133+ } ;
134+
135+ const generatedAllTemplate = replaceTemplateValues (
136+ apiTemplate ,
137+ templateValues ,
138+ releases ,
139+ { skipGitHub : true , skipGtocPicker : true }
140+ ) ;
141+
142+ // We minify the html result to reduce the file size and keep it "clean"
143+ const minified = HTMLMinifier . minify ( Buffer . from ( generatedAllTemplate ) ) ;
144+
101145 await writeFile ( join ( output , 'all.html' ) , minified ) ;
102146 }
103-
104- return minified ;
105147 } ,
106148} ;
0 commit comments