|
| 1 | +import { glob } from "glob"; |
| 2 | +import { readFile, writeFile } from "node:fs/promises"; |
| 3 | + |
| 4 | +const BASE_PATH = "/documentation"; |
| 5 | +const DRY_RUN = process.argv.includes("--dry-run"); |
| 6 | + |
| 7 | +/** |
| 8 | + * Migrate internal links in MDX files to use the basePath prefix. |
| 9 | + * |
| 10 | + * This script finds all root-relative links that don't already have |
| 11 | + * the /documentation/ prefix and adds it. |
| 12 | + * |
| 13 | + * IMPORTANT: This script is designed for one-time migration of legacy content. |
| 14 | + * The remark-transform-links plugin handles link transformation at build time, |
| 15 | + * so this script should NOT be run on content that will be processed by that plugin. |
| 16 | + * Running both would result in double-prefixed links. |
| 17 | + * |
| 18 | + * Usage: |
| 19 | + * bun scripts/migrate-links.ts # Apply changes |
| 20 | + * bun scripts/migrate-links.ts --dry-run # Preview changes without writing |
| 21 | + */ |
| 22 | + |
| 23 | +// Type for replacement functions that match String.replace callback signature |
| 24 | +type ReplaceFn = (substring: string, ...args: string[]) => string; |
| 25 | + |
| 26 | +// Patterns to transform (root-relative without basePath) |
| 27 | +// These patterns match links that start with / but NOT /documentation/, /images/, /api/, etc. |
| 28 | +const LINK_PATTERNS: Array<{ regex: RegExp; replace: ReplaceFn }> = [ |
| 29 | + // Markdown links: [text](/path) - but not [text](/documentation/...) or [text](/images/...) etc. |
| 30 | + { |
| 31 | + regex: |
| 32 | + /(\[[^\]]+\]\()\/(?!documentation\/|images\/|api\/|ingest\/|_next\/)([^)]+\))/g, |
| 33 | + replace: (_match: string, prefix: string, path: string) => |
| 34 | + `${prefix}${BASE_PATH}/${path}`, |
| 35 | + }, |
| 36 | + // JSX href in Card/other components: href="/path" - but not href="/documentation/..." etc. |
| 37 | + { |
| 38 | + regex: |
| 39 | + /href="\/(?!documentation\/|images\/|api\/|ingest\/|_next\/)([^"]+)"/g, |
| 40 | + replace: (_match: string, path: string) => `href="${BASE_PATH}/${path}"`, |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +async function migrateLinks() { |
| 45 | + console.log("[Migrate Links] Starting link migration..."); |
| 46 | + if (DRY_RUN) { |
| 47 | + console.log("[Migrate Links] DRY RUN MODE - no files will be modified\n"); |
| 48 | + } |
| 49 | + |
| 50 | + const files = await glob("content/docs/**/*.mdx", { cwd: process.cwd() }); |
| 51 | + console.log(`[Migrate Links] Found ${files.length} MDX files to scan\n`); |
| 52 | + |
| 53 | + let totalChanges = 0; |
| 54 | + let filesChanged = 0; |
| 55 | + |
| 56 | + for (const file of files) { |
| 57 | + const content = await readFile(file, "utf-8"); |
| 58 | + let newContent = content; |
| 59 | + let fileChanges = 0; |
| 60 | + |
| 61 | + for (const pattern of LINK_PATTERNS) { |
| 62 | + const matches = [...newContent.matchAll(pattern.regex)]; |
| 63 | + fileChanges += matches.length; |
| 64 | + |
| 65 | + newContent = newContent.replace(pattern.regex, pattern.replace); |
| 66 | + } |
| 67 | + |
| 68 | + if (fileChanges > 0) { |
| 69 | + filesChanged++; |
| 70 | + totalChanges += fileChanges; |
| 71 | + |
| 72 | + if (DRY_RUN) { |
| 73 | + console.log(`[DRY RUN] ${file}: ${fileChanges} links would be updated`); |
| 74 | + |
| 75 | + // Show the actual changes for this file |
| 76 | + const originalLines = content.split("\n"); |
| 77 | + const newLines = newContent.split("\n"); |
| 78 | + |
| 79 | + for (let i = 0; i < originalLines.length; i++) { |
| 80 | + if (originalLines[i] !== newLines[i]) { |
| 81 | + console.log(` Line ${i + 1}:`); |
| 82 | + console.log(` - ${originalLines[i].trim()}`); |
| 83 | + console.log(` + ${newLines[i].trim()}`); |
| 84 | + } |
| 85 | + } |
| 86 | + console.log(""); |
| 87 | + } else { |
| 88 | + await writeFile(file, newContent); |
| 89 | + console.log(`[Migrate Links] ${file}: ${fileChanges} links updated`); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + console.log( |
| 95 | + `\n[Migrate Links] Summary: ${totalChanges} links ${DRY_RUN ? "would be" : ""} updated across ${filesChanged} files` |
| 96 | + ); |
| 97 | + |
| 98 | + if (DRY_RUN && totalChanges > 0) { |
| 99 | + console.log( |
| 100 | + "\n[Migrate Links] Run without --dry-run to apply these changes" |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +migrateLinks().catch((error) => { |
| 106 | + console.error("[Migrate Links] Error:", error); |
| 107 | + process.exit(1); |
| 108 | +}); |
0 commit comments