11import fs from "fs/promises" ;
2- import { basename , dirname , relative , resolve , sep } from "path" ;
3- import { fileURLToPath } from "url" ;
4-
2+ import { basename } from "path" ;
53const basePath = new URL (
64 "../../inputfiles/mdn/files/en-us/web/api/" ,
75 import . meta. url ,
86) ;
9- const baseDir = fileURLToPath ( basePath ) ;
107
118function extractSummary ( markdown : string ) : string {
129 // Remove frontmatter (--- at the beginning)
@@ -48,16 +45,18 @@ function extractSummary(markdown: string): string {
4845 return sentenceMatch ? sentenceMatch [ 0 ] : normalizedText . split ( " " ) [ 0 ] || "" ;
4946}
5047
51- async function walkDirectory ( dirPath : string ) : Promise < string [ ] > {
52- let results : string [ ] = [ ] ;
48+ async function walkDirectory ( dir : URL ) : Promise < URL [ ] > {
49+ const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
50+ let results : URL [ ] = [ ] ;
5351
54- const entries = await fs . readdir ( dirPath , { withFileTypes : true } ) ;
5552 for ( const entry of entries ) {
56- const fullPath = resolve ( dirPath , entry . name ) ;
53+ const fullPath = new URL ( `${ entry . name } /` , dir ) ;
54+ const fullFile = new URL ( entry . name , dir ) ;
55+
5756 if ( entry . isDirectory ( ) ) {
5857 results = results . concat ( await walkDirectory ( fullPath ) ) ;
5958 } else if ( entry . isFile ( ) && entry . name === "index.md" ) {
60- results . push ( fullPath ) ;
59+ results . push ( fullFile ) ;
6160 }
6261 }
6362
@@ -74,20 +73,21 @@ export async function generateDescriptions(): Promise<Record<string, string>> {
7473
7574 const results : Record < string , string > = { } ;
7675 try {
77- const indexPaths = await walkDirectory ( baseDir ) ;
76+ const indexPaths = await walkDirectory ( basePath ) ;
7877
79- for ( const filePath of indexPaths ) {
78+ for ( const fileURL of indexPaths ) {
8079 try {
81- const content = await fs . readFile ( filePath , "utf-8" ) ;
80+ const content = await fs . readFile ( fileURL , "utf-8" ) ;
8281
8382 const titleMatch = content . match ( / t i t l e : \s * [ " ' ] ? ( [ ^ " ' \n ] + ) [ " ' ] ? / ) ;
84- const filename = basename ( filePath . toString ( ) ) ;
8583 const title = titleMatch
8684 ? titleMatch [ 1 ] . replace ( / e x t e n s i o n $ / , "" ) . split ( ":" ) [ 0 ]
87- : filename || "" ;
88- // Get relative path to the file, excluding index.md
89- const relPath = relative ( baseDir , dirname ( filePath ) ) ;
90- const parentKey = relPath . split ( sep ) . filter ( Boolean ) . join ( "." ) ;
85+ : basename ( fileURL . pathname ) || "" ;
86+
87+ const relPath = fileURL . pathname
88+ . replace ( basePath . pathname , "" )
89+ . replace ( / \/ i n d e x \. m d $ / , "" ) ;
90+ const parentKey = relPath . split ( "/" ) . filter ( Boolean ) . join ( "." ) ;
9191 const fullKey = parentKey . includes ( "." )
9292 ? parentKey . includes ( title . toLowerCase ( ) )
9393 ? parentKey
@@ -97,7 +97,7 @@ export async function generateDescriptions(): Promise<Record<string, string>> {
9797 const summary = extractSummary ( content ) ;
9898 results [ fullKey ] = summary ;
9999 } catch ( error ) {
100- console . warn ( `Skipping ${ filePath } : ${ error } ` ) ;
100+ console . warn ( `Skipping ${ fileURL . href } : ${ error } ` ) ;
101101 }
102102 }
103103 } catch ( error ) {
0 commit comments