|
| 1 | +const fs = require('node:fs'); |
| 2 | +const path = require('node:path'); |
| 3 | + |
| 4 | +/** |
| 5 | + * Extract frontmatter from markdown content |
| 6 | + */ |
| 7 | +function extractFrontmatter(markdown) { |
| 8 | + const frontmatterMatch = markdown.match(/^---([\s\S]*?)---/); |
| 9 | + if (!frontmatterMatch) { |
| 10 | + return { title: null, description: null, content: markdown }; |
| 11 | + } |
| 12 | + |
| 13 | + const fmContent = frontmatterMatch[1]; |
| 14 | + const contentWithoutFM = markdown.slice(frontmatterMatch[0].length).trimStart(); |
| 15 | + |
| 16 | + // Simple frontmatter parse for title and description (YAML-ish) |
| 17 | + const lines = fmContent.split(/\r?\n/); |
| 18 | + let title = null; |
| 19 | + let description = null; |
| 20 | + |
| 21 | + for (const line of lines) { |
| 22 | + const [key, ...rest] = line.split(':'); |
| 23 | + if (!key) continue; |
| 24 | + const value = rest.join(':').trim().replace(/^["']|["']$/g, ''); // Remove quotes |
| 25 | + |
| 26 | + if (key.trim() === 'title') title = value; |
| 27 | + if (key.trim() === 'description') description = value; |
| 28 | + } |
| 29 | + |
| 30 | + return { title, description, content: contentWithoutFM }; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Recursively walk directory and find markdown files |
| 35 | + */ |
| 36 | +function walk(dir, extFilter = ['.md', '.mdx']) { |
| 37 | + const results = []; |
| 38 | + |
| 39 | + if (!fs.existsSync(dir)) { |
| 40 | + return results; |
| 41 | + } |
| 42 | + |
| 43 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 44 | + const fullPath = path.join(dir, entry.name); |
| 45 | + |
| 46 | + if (entry.isDirectory()) { |
| 47 | + results.push(...walk(fullPath, extFilter)); |
| 48 | + } else if ( |
| 49 | + !entry.name.startsWith('_') && |
| 50 | + extFilter.includes(path.extname(entry.name)) |
| 51 | + ) { |
| 52 | + results.push(fullPath); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return results; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Process markdown file: remove frontmatter only (keep original content) |
| 61 | + */ |
| 62 | +function processMarkdownFile(filePath) { |
| 63 | + let content = fs.readFileSync(filePath, 'utf-8'); |
| 64 | + const frontmatterMatch = content.match(/^---([\s\S]*?)---/); |
| 65 | + |
| 66 | + // If there's frontmatter, remove it and return the rest |
| 67 | + if (frontmatterMatch) { |
| 68 | + return content.slice(frontmatterMatch[0].length).trimStart(); |
| 69 | + } |
| 70 | + |
| 71 | + // No frontmatter, return as-is |
| 72 | + return content; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Convert file path to URL path (handles index files) |
| 77 | + */ |
| 78 | +function rewritePath(relPath, baseUrl = '/docs/') { |
| 79 | + // Remove leading slash if present |
| 80 | + let path = relPath.replace(/^\//, ''); |
| 81 | + |
| 82 | + // Handle index files - convert /path/index.md to /path.md |
| 83 | + if (path.endsWith('/index.md') || path.endsWith('/index.mdx')) { |
| 84 | + path = path.replace(/\/index\.(md|mdx)$/, '.md'); |
| 85 | + } |
| 86 | + |
| 87 | + // Ensure .md extension |
| 88 | + if (!path.endsWith('.md')) { |
| 89 | + path = path.replace(/\.mdx$/, '.md'); |
| 90 | + } |
| 91 | + |
| 92 | + return baseUrl + path; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Get the relative path from docs directory |
| 97 | + */ |
| 98 | +function getRelativePath(filePath, docsDir) { |
| 99 | + return path.relative(docsDir, filePath); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Copy original markdown files (just remove frontmatter) to output directory |
| 104 | + * Files are organized by their doc ID (file path without extension) |
| 105 | + */ |
| 106 | +function copyMarkdownFiles(context, outputDir) { |
| 107 | + console.log('Copying markdown files for copy-to-markdown feature...'); |
| 108 | + |
| 109 | + const docsDir = path.resolve(context.siteDir, 'docs'); |
| 110 | + const contentFiles = walk(docsDir); |
| 111 | + |
| 112 | + // Create markdown output directory |
| 113 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 114 | + |
| 115 | + let processedCount = 0; |
| 116 | + |
| 117 | + for (const file of contentFiles) { |
| 118 | + try { |
| 119 | + const relPath = getRelativePath(file, docsDir); |
| 120 | + // Remove frontmatter but keep original content |
| 121 | + const processedContent = processMarkdownFile(file); |
| 122 | + |
| 123 | + // Use the file path as the doc ID (remove extension) |
| 124 | + // This matches what metadata.id returns |
| 125 | + let docId = relPath.replace(/\.(md|mdx)$/, ''); |
| 126 | + |
| 127 | + // Handle index files - they map to their parent directory |
| 128 | + // e.g., getting-started/index.md -> getting-started |
| 129 | + if (path.basename(docId) === 'index') { |
| 130 | + docId = path.dirname(docId); |
| 131 | + // If it's the root index, keep it as 'index' |
| 132 | + if (docId === '.' || docId === '') { |
| 133 | + docId = 'index'; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Normalize path separators for cross-platform compatibility |
| 138 | + docId = docId.replace(/\\/g, '/'); |
| 139 | + |
| 140 | + // Create output path: markdown/getting-started/introduction.md |
| 141 | + const outputPath = path.join(outputDir, docId + '.md'); |
| 142 | + |
| 143 | + // Create directory structure |
| 144 | + fs.mkdirSync(path.dirname(outputPath), { recursive: true }); |
| 145 | + |
| 146 | + // Write processed markdown (frontmatter removed, but original content preserved) |
| 147 | + fs.writeFileSync(outputPath, processedContent, 'utf-8'); |
| 148 | + processedCount++; |
| 149 | + } catch (error) { |
| 150 | + console.warn(`Error processing ${file}:`, error.message); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + console.log(`Copied ${processedCount} markdown files to ${outputDir}`); |
| 155 | +} |
| 156 | + |
| 157 | +module.exports = function markdownExtractPlugin(context, options) { |
| 158 | + return { |
| 159 | + name: 'markdown-extract', |
| 160 | + async postBuild({ outDir, routes }) { |
| 161 | + // Only copy to build directory for production (not to static to save space) |
| 162 | + // Files are only generated during build, not in static directory |
| 163 | + const markdownOutputDir = path.join(outDir, 'markdown'); |
| 164 | + copyMarkdownFiles(context, markdownOutputDir); |
| 165 | + }, |
| 166 | + }; |
| 167 | +}; |
0 commit comments