Skip to content

Commit 4b561dc

Browse files
committed
perf: add html txt
1 parent 6c44c1f commit 4b561dc

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

docs/.vitepress/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from 'vitepress';
22
import typedocSidebar from '../api/typedoc-sidebar.json';
3-
import { transformHtml } from './plugins';
3+
import { transformHtml, buildEnd } from './plugins';
44

55
export default defineConfig({
66
title: 'GKD',
@@ -91,4 +91,5 @@ export default defineConfig({
9191
},
9292
cleanUrls: true,
9393
transformHtml,
94+
buildEnd,
9495
});

docs/.vitepress/plugins/mirror.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'node:fs/promises';
55
import process from 'node:process';
66
import type { Plugin } from 'vite';
77
import type selfPkgT from '../../package.json';
8+
import path from 'node:path';
89

910
const 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+
74115
const Parser =
75116
globalThis.DOMParser || new (await import('jsdom')).JSDOM().window.DOMParser;
76117
export const transformHtml = (code: string) => {

0 commit comments

Comments
 (0)