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 { 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 { 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); } } }