1
1
/* eslint-disable no-console */
2
2
import 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" ;
6
6
import { reporter } from "vfile-reporter" ;
7
7
import { remark } from "remark" ;
8
8
import remarkCodeTitles from "./remark-code-titles.js" ;
@@ -22,8 +22,8 @@ import rehypeStringify from "rehype-stringify";
22
22
23
23
dotenv . config ( ) ;
24
24
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" ) ;
27
27
const file = await remark ( )
28
28
. use ( remarkPresetLintMarkdownStyleGuide )
29
29
. use ( remarkLintMaximumHeadingLength , false )
@@ -62,8 +62,12 @@ const build = async (filename) => {
62
62
. use ( rehypeStringify )
63
63
. process ( md ) ;
64
64
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>
67
71
<html>
68
72
<head>
69
73
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
@@ -184,7 +188,8 @@ const build = async (filename) => {
184
188
let messageCount = 0 ;
185
189
for ( const filename of files ) {
186
190
console . log ( `Building: ${ filename } ...` ) ;
187
- messageCount += await build ( filename ) ;
191
+ const filePath = resolve ( cwd ( ) , filename ) ;
192
+ messageCount += await build ( filePath ) ;
188
193
console . log ( "" ) ;
189
194
}
190
195
0 commit comments