11/* eslint-disable no-console */
22import dotenv from "dotenv" ;
3- import { readFileSync , writeFileSync } from "node:fs" ;
4- import { dirname , basename } from "node:path" ;
5- import { argv } from "node:process" ;
3+ import { mkdir , readFile , writeFile } from "node:fs/promises " ;
4+ import { dirname , resolve , relative } from "node:path" ;
5+ import { argv , cwd } from "node:process" ;
66import { reporter } from "vfile-reporter" ;
77import { remark } from "remark" ;
88import remarkCodeTitles from "./remark-code-titles.js" ;
@@ -22,8 +22,8 @@ import rehypeStringify from "rehype-stringify";
2222
2323dotenv . config ( ) ;
2424
25- const build = async ( filename ) => {
26- const md = readFileSync ( filename , "utf-8" ) ;
25+ const build = async ( filePath ) => {
26+ const md = await readFile ( filePath , "utf-8" ) ;
2727 const file = await remark ( )
2828 . use ( remarkPresetLintMarkdownStyleGuide )
2929 . use ( remarkLintMaximumHeadingLength , false )
@@ -62,8 +62,12 @@ const build = async (filename) => {
6262 . use ( rehypeStringify )
6363 . process ( md ) ;
6464
65- const outfile = `${ dirname ( filename ) } /${ basename ( filename , ".md" ) } .html` ;
66- writeFileSync ( outfile , `<!DOCTYPE html>
65+ const rootPath = resolve ( import . meta. dirname , ".." ) ;
66+ const outPath = resolve ( rootPath , "web" ) ;
67+ const sourceRelativePath = relative ( rootPath , filePath ) ;
68+ const outfile = resolve ( outPath , sourceRelativePath ) . replace ( / \. m d $ / , ".html" ) ;
69+ await mkdir ( dirname ( outfile ) , { recursive : true } ) ;
70+ await writeFile ( outfile , `<!DOCTYPE html>
6771<html>
6872 <head>
6973 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
@@ -184,7 +188,8 @@ const build = async (filename) => {
184188 let messageCount = 0 ;
185189 for ( const filename of files ) {
186190 console . log ( `Building: ${ filename } ...` ) ;
187- messageCount += await build ( filename ) ;
191+ const filePath = resolve ( cwd ( ) , filename ) ;
192+ messageCount += await build ( filePath ) ;
188193 console . log ( "" ) ;
189194 }
190195
0 commit comments