11#!/usr/bin/env node
22
3+ import { selectAll } from 'hast-util-select' ;
34import { mkdir , opendir , readFile , rm , writeFile } from 'node:fs/promises' ;
45import * as path from 'node:path' ;
56import rehypeParse from 'rehype-parse' ;
67import rehypeRemark from 'rehype-remark' ;
78import remarkGfm from 'remark-gfm' ;
89import remarkStringify from 'remark-stringify' ;
910import { unified } from 'unified' ;
10- import { remove } from 'unist-util-remove' ;
1111
1212const root = process . cwd ( ) ; // fix this
1313const INPUT_DIR = path . join ( root , '.next' , 'server' , 'app' ) ;
@@ -20,8 +20,16 @@ export const genMDFromHTML = async (source, target) => {
2020 String (
2121 await unified ( )
2222 . use ( rehypeParse )
23- . use ( rehypeRemark )
24- . use ( ( ) => tree => remove ( tree , { type : 'text' , value : 'Copied' } ) )
23+ // Need the `main div > hgroup` selector for the headers
24+ . use ( ( ) => tree => selectAll ( 'main div > hgroup, div#main' , tree ) )
25+ // If we don't do this wrapping, rehypeRemark just returns an empty string -- yeah WTF?
26+ . use ( ( ) => tree => ( {
27+ type : 'element' ,
28+ tagName : 'div' ,
29+ properties : { } ,
30+ children : tree ,
31+ } ) )
32+ . use ( rehypeRemark , { document : false } )
2533 . use ( remarkGfm )
2634 . use ( remarkStringify )
2735 . process ( text )
@@ -42,6 +50,7 @@ async function main() {
4250 // See https://github.com/nodejs/node/issues/48820
4351 const dir = await opendir ( INPUT_DIR , { recursive : true , bufferSize : 1024 } ) ;
4452 for await ( const dirent of dir ) {
53+ if ( counter >= 100 ) break ;
4554 if ( dirent . name . endsWith ( '.html' ) && dirent . isFile ( ) ) {
4655 const sourcePath = path . join ( dirent . parentPath || dirent . path , dirent . name ) ;
4756 const targetDir = path . join (
0 commit comments