@@ -5,6 +5,7 @@ import fs from 'node:fs/promises';
55import process from 'node:process' ;
66import type { Plugin } from 'vite' ;
77import type selfPkgT from '../../package.json' ;
8+ import path from 'node:path' ;
89
910const selfPkg : typeof selfPkgT = JSON . parse (
1011 await fs . readFile ( process . cwd ( ) + '/package.json' , 'utf-8' ) ,
@@ -71,6 +72,46 @@ export const mirror = (): Plugin | undefined => {
7172 } ;
7273} ;
7374
75+ const posixPath = ( str : string ) : string => {
76+ if ( str . includes ( '\\' ) ) {
77+ return str . replaceAll ( '\\' , '/' ) ;
78+ }
79+ return str ;
80+ } ;
81+ async function * traverseDirectory (
82+ dir : string ,
83+ filter ?: ( subDirectory : string ) => boolean ,
84+ ) {
85+ const pathnames = ( await fs . readdir ( dir ) )
86+ . map ( ( s ) => posixPath ( path . join ( dir , s ) ) )
87+ . reverse ( ) ;
88+ while ( pathnames . length > 0 ) {
89+ const pathname = pathnames . pop ( ) ! ;
90+ const state = await fs . lstat ( pathname ) ;
91+ if ( state . isFile ( ) ) {
92+ yield pathname ;
93+ } else if ( state . isDirectory ( ) && ( ! filter || filter ( pathname ) ) ) {
94+ pathnames . push (
95+ ...( await fs . readdir ( pathname ) )
96+ . map ( ( s ) => posixPath ( path . join ( pathname , s ) ) )
97+ . reverse ( ) ,
98+ ) ;
99+ }
100+ }
101+ }
102+
103+ export const buildEnd = async ( ) => {
104+ if ( ! useMirror ) return ;
105+ for await ( const filePathName of traverseDirectory (
106+ process . cwd ( ) + '/.vitepress/dist' ,
107+ ) ) {
108+ if ( filePathName . endsWith ( '.html' ) ) {
109+ const textFileName = filePathName + '.txt' ;
110+ await fs . copyFile ( filePathName , textFileName ) ;
111+ }
112+ }
113+ } ;
114+
74115const Parser =
75116 globalThis . DOMParser || new ( await import ( 'jsdom' ) ) . JSDOM ( ) . window . DOMParser ;
76117export const transformHtml = ( code : string ) => {
0 commit comments