1
1
import fs from "fs/promises" ;
2
- import { merge } from "./helpers.js" ;
3
2
4
3
const basePath = new URL (
5
4
"../../inputfiles/mdn/files/en-us/web/api/" ,
@@ -76,10 +75,16 @@ function generateSlug(content: string): string[] {
76
75
return parts ;
77
76
}
78
77
79
- function urlToNestedObject ( url : string [ ] , text : string ) : Record < string , any > {
80
- return url . reduceRight < Record < string , any > > ( ( acc , key ) => ( { [ key ] : acc } ) , {
81
- __comment : text ,
82
- } ) ;
78
+ function ensureLeaf (
79
+ obj : Record < string , any > ,
80
+ keys : string [ ] ,
81
+ ) : Record < string , any > {
82
+ let leaf = obj ;
83
+ for ( const key of keys ) {
84
+ leaf [ key ] ??= { } ;
85
+ leaf = leaf [ key ] ;
86
+ }
87
+ return leaf ;
83
88
}
84
89
85
90
export async function generateDescriptions ( ) : Promise < Record < string , any > > {
@@ -99,8 +104,8 @@ export async function generateDescriptions(): Promise<Record<string, any>> {
99
104
const content = await fs . readFile ( fileURL , "utf-8" ) ;
100
105
const slug = generateSlug ( content ) ;
101
106
const summary = extractSummary ( content ) ;
102
- const nested = urlToNestedObject ( slug , summary ) ;
103
- merge ( results , nested ) ;
107
+ const leaf = ensureLeaf ( results , slug ) ;
108
+ leaf . __comment = summary ;
104
109
} catch ( error ) {
105
110
console . warn ( `Skipping ${ fileURL . href } : ${ error } ` ) ;
106
111
}
0 commit comments