|
| 1 | +const fs = require("fs-extra"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +/** |
| 5 | + * Custom Docusaurus plugin that copies original markdown files |
| 6 | + * to the build directory alongside the generated HTML files |
| 7 | + */ |
| 8 | +async function copyMarkdownFilesPlugin(context, options) { |
| 9 | + return { |
| 10 | + name: "copy-markdown-files", |
| 11 | + async postBuild({ siteDir, outDir }) { |
| 12 | + console.log("📄 Copying original markdown files to build directory..."); |
| 13 | + |
| 14 | + try { |
| 15 | + // Define source directories to copy from |
| 16 | + const sourceDirs = [ |
| 17 | + path.join(siteDir, "docs"), |
| 18 | + path.join(siteDir, "blog"), |
| 19 | + ]; |
| 20 | + |
| 21 | + // Define corresponding target directories |
| 22 | + const targetDirs = [ |
| 23 | + path.join(outDir, "docs"), |
| 24 | + path.join(outDir, "blog"), |
| 25 | + ]; |
| 26 | + |
| 27 | + // Copy each source directory to its target |
| 28 | + for (let i = 0; i < sourceDirs.length; i++) { |
| 29 | + const sourceDir = sourceDirs[i]; |
| 30 | + const targetDir = targetDirs[i]; |
| 31 | + |
| 32 | + // Check if source directory exists |
| 33 | + if (await fs.pathExists(sourceDir)) { |
| 34 | + console.log( |
| 35 | + ` 📁 Copying ${path.basename(sourceDir)} directory...` |
| 36 | + ); |
| 37 | + |
| 38 | + // Ensure target directory exists |
| 39 | + await fs.ensureDir(targetDir); |
| 40 | + |
| 41 | + // Copy all markdown files recursively |
| 42 | + await copyMarkdownFilesRecursive(sourceDir, targetDir); |
| 43 | + |
| 44 | + console.log( |
| 45 | + ` ✅ Successfully copied ${path.basename(sourceDir)} files` |
| 46 | + ); |
| 47 | + } else { |
| 48 | + console.log( |
| 49 | + ` ⚠️ Source directory ${sourceDir} does not exist, skipping...` |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Copy static markdown files from src/pages if they exist |
| 55 | + const srcPagesDir = path.join(siteDir, "src", "pages"); |
| 56 | + if (await fs.pathExists(srcPagesDir)) { |
| 57 | + console.log(" 📄 Checking for markdown files in src/pages..."); |
| 58 | + await copyMarkdownFilesRecursive(srcPagesDir, outDir); |
| 59 | + } |
| 60 | + |
| 61 | + console.log("🎉 All markdown files copied successfully!"); |
| 62 | + } catch (error) { |
| 63 | + console.error("❌ Error copying markdown files:", error); |
| 64 | + throw error; |
| 65 | + } |
| 66 | + }, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Recursively copy markdown files from source to target directory |
| 72 | + * @param {string} sourceDir - Source directory path |
| 73 | + * @param {string} targetDir - Target directory path |
| 74 | + */ |
| 75 | +async function copyMarkdownFilesRecursive(sourceDir, targetDir) { |
| 76 | + const items = await fs.readdir(sourceDir); |
| 77 | + |
| 78 | + for (const item of items) { |
| 79 | + const sourcePath = path.join(sourceDir, item); |
| 80 | + const targetPath = path.join(targetDir, item); |
| 81 | + const stat = await fs.stat(sourcePath); |
| 82 | + |
| 83 | + if (stat.isDirectory()) { |
| 84 | + // Recursively copy subdirectories |
| 85 | + await fs.ensureDir(targetPath); |
| 86 | + await copyMarkdownFilesRecursive(sourcePath, targetPath); |
| 87 | + } else if ( |
| 88 | + stat.isFile() && |
| 89 | + (item.endsWith(".md") || item.endsWith(".mdx")) |
| 90 | + ) { |
| 91 | + // Copy markdown files |
| 92 | + await fs.copy(sourcePath, targetPath); |
| 93 | + console.log( |
| 94 | + ` 📄 Copied: ${path.relative( |
| 95 | + process.cwd(), |
| 96 | + sourcePath |
| 97 | + )} → ${path.relative(process.cwd(), targetPath)}` |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = copyMarkdownFilesPlugin; |
0 commit comments