| 123456789101112131415161718192021222324252627282930313233343536373839 |
- import path from 'node:path';
- import fs from 'node:fs/promises';
- import type {Plugin} from '@docusaurus/types';
- type Options = {
- sourceDir: string;
- routeBasePath: string;
- };
- export default function copyMdPlugin(
- _context: unknown,
- options: Options,
- ): Plugin<void> {
- return {
- name: 'copy-md',
- async postBuild({siteDir, outDir}) {
- const src = path.join(siteDir, options.sourceDir);
- const dest = path.join(outDir, options.routeBasePath);
- await fs.mkdir(dest, {recursive: true});
- await copyMarkdownFiles(src, dest);
- },
- };
- }
- async function copyMarkdownFiles(src: string, dest: string): Promise<void> {
- const entries = await fs.readdir(src, {withFileTypes: true});
- for (const entry of entries) {
- const srcPath = path.join(src, entry.name);
- const destPath = path.join(dest, entry.name);
- if (entry.isDirectory()) {
- await fs.mkdir(destPath, {recursive: true});
- await copyMarkdownFiles(srcPath, destPath);
- continue;
- }
- if (entry.isFile() && /\.(md|mdx)$/i.test(entry.name)) {
- await fs.copyFile(srcPath, destPath);
- }
- }
- }
|