@@ -33,10 +33,10 @@ export interface TocOrderInsertions {
3333 } ;
3434}
3535
36- const isInsertionCandidateNode = ( node : ToC , associated_products : AssociatedProduct [ ] = [ ] ) : boolean => {
37- const nodeInProducts = node . options ?. project && associated_products . find ( ( p ) => p . name === node . options ?. project ) ;
38- const nodeHasNoChildren = ! node . children || node . children . length === 0 ;
39- return ! ! ( nodeHasNoChildren && nodeInProducts ) ;
36+ const isInsertionCandidateNode = ( node : ToC , toBeInserted : Set < string > = new Set ( [ ] ) ) : boolean => {
37+ const projectName = node . options ?. project ;
38+ const nodeInProducts = projectName && toBeInserted . has ( projectName ) ;
39+ return ! ! nodeInProducts ;
4040} ;
4141
4242const mergeNode = ( node : ToC , tocs : ToCInsertions , currentProject ) => {
@@ -100,15 +100,16 @@ export const traverseAndMerge = (
100100 const { project } = metadata ;
101101
102102 const toctree = hasAssociations ( metadata ) ? umbrellaToCs . original : umbrellaToCs . urlified ;
103-
103+ const toBeInserted = new Set ( associated_products . map ( ( p ) => p . name ) ) ;
104104 let queue = [ toctree ] ;
105- while ( queue ?. length ) {
105+ while ( queue ?. length && toBeInserted . size ) {
106106 let next = queue . shift ( ) ;
107107 // TODO: We can exit early here once we've found all the nodes.
108108 // We should track remaining insertions in a set and add some break logic.
109- if ( next && isInsertionCandidateNode ( next , associated_products ) ) {
109+ if ( next && isInsertionCandidateNode ( next , toBeInserted ) ) {
110110 next = mergeNode ( next , tocInsertions , project ) ;
111111 metadata . toctreeorder = mergeTocTreeOrder ( metadata , next , tocOrderInsertions ) ;
112+ toBeInserted . delete ( next . options ?. project ) ;
112113 } else if ( next ?. children ) {
113114 queue = [ ...queue , ...next . children ] ;
114115 }
0 commit comments