1
1
import fs from "fs/promises" ;
2
+ import { merge } from "./helpers.js" ;
2
3
3
4
const basePath = new URL (
4
5
"../../inputfiles/mdn/files/en-us/web/api/" ,
@@ -68,41 +69,19 @@ async function walkDirectory(dir: URL): Promise<URL[]> {
68
69
return results ;
69
70
}
70
71
71
- function generateSlug ( content : string ) : string {
72
- const match = content . match ( / s l u g : \s * [ " ' ] ? ( [ ^ " ' \n ] + ) [ " ' ] ? / ) ;
73
- if ( ! match ) throw new Error ( "Slug not found" ) ;
72
+ function generateSlug ( content : string ) : string [ ] {
73
+ const match = content . match ( / s l u g : \s * [ " ' ] ? ( [ ^ " ' \n ] + ) [ " ' ] ? / ) ! ;
74
74
const url = match [ 1 ] . split ( ":" ) . pop ( ) ! ;
75
75
const parts = url . split ( "/" ) . slice ( 2 ) ; // remove first 2 segments
76
- const result = parts . join ( "/" ) ;
77
- return result ;
76
+ return parts ;
78
77
}
79
78
80
- function urlToNestedObject ( url : string , text : string ) : Record < string , any > {
81
- const keys = url . split ( "/" ) ;
82
- return keys . reduceRight < Record < string , any > > ( ( acc , key ) => ( { [ key ] : acc } ) , {
79
+ function urlToNestedObject ( url : string [ ] , text : string ) : Record < string , any > {
80
+ return url . reduceRight < Record < string , any > > ( ( acc , key ) => ( { [ key ] : acc } ) , {
83
81
__comment : text ,
84
82
} ) ;
85
83
}
86
84
87
- function deepMerge (
88
- target : Record < string , any > ,
89
- source : Record < string , any > ,
90
- ) : Record < string , any > {
91
- for ( const key in source ) {
92
- if (
93
- source [ key ] &&
94
- typeof source [ key ] === "object" &&
95
- ! Array . isArray ( source [ key ] )
96
- ) {
97
- if ( ! target [ key ] ) target [ key ] = { } ;
98
- deepMerge ( target [ key ] , source [ key ] ) ;
99
- } else {
100
- target [ key ] = source [ key ] ;
101
- }
102
- }
103
- return target ;
104
- }
105
-
106
85
export async function generateDescriptions ( ) : Promise < Record < string , any > > {
107
86
const stats = await fs . stat ( basePath ) ;
108
87
if ( ! stats . isDirectory ( ) ) {
@@ -121,7 +100,7 @@ export async function generateDescriptions(): Promise<Record<string, any>> {
121
100
const slug = generateSlug ( content ) ;
122
101
const summary = extractSummary ( content ) ;
123
102
const nested = urlToNestedObject ( slug , summary ) ;
124
- deepMerge ( results , nested as any ) ;
103
+ merge ( results , nested ) ;
125
104
} catch ( error ) {
126
105
console . warn ( `Skipping ${ fileURL . href } : ${ error } ` ) ;
127
106
}
0 commit comments