11#!/usr/bin/env node
22
33import { selectAll } from 'hast-util-select' ;
4+ import { existsSync } from 'node:fs' ;
45import { mkdir , opendir , readFile , rm , writeFile } from 'node:fs/promises' ;
56import * as path from 'node:path' ;
67import rehypeParse from 'rehype-parse' ;
@@ -9,7 +10,14 @@ import remarkGfm from 'remark-gfm';
910import remarkStringify from 'remark-stringify' ;
1011import { unified } from 'unified' ;
1112
12- const root = process . cwd ( ) ; // fix this
13+ let root = process . cwd ( ) ;
14+ while ( ! existsSync ( path . join ( root , 'package.json' ) ) ) {
15+ const parent = path . dirname ( root ) ;
16+ if ( parent === root ) {
17+ throw new Error ( 'Could not find package.json in parent directories' ) ;
18+ }
19+ root = parent ;
20+ }
1321const INPUT_DIR = path . join ( root , '.next' , 'server' , 'app' ) ;
1422const OUTPUT_DIR = path . join ( root , 'public' , 'md-exports' ) ;
1523
@@ -38,7 +46,7 @@ export const genMDFromHTML = async (source, target) => {
3846} ;
3947
4048async function main ( ) {
41- console . log ( ' 🚀 Starting markdown export generation...' ) ;
49+ console . log ( ` 🚀 Starting markdown generation from: ${ INPUT_DIR } ` ) ;
4250 console . log ( `📁 Output directory: ${ OUTPUT_DIR } ` ) ;
4351
4452 // Clear output directory
0 commit comments