|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | +const __dirname = path.dirname(__filename); |
| 7 | + |
| 8 | +function fixMdxFile(filePath) { |
| 9 | + let content = fs.readFileSync(filePath, 'utf-8'); |
| 10 | + let changed = false; |
| 11 | + |
| 12 | + // Fix LinkButton tags specifically - they're case sensitive |
| 13 | + content = content.replace(/<linkButton([^>]*?)\s*\/\s*>([^<]+)<\/LinkButton>/gi, (match, attributes, innerContent) => { |
| 14 | + changed = true; |
| 15 | + return `<LinkButton${attributes}>${innerContent}</LinkButton>`; |
| 16 | + }); |
| 17 | + |
| 18 | + // Fix self-closing tags that shouldn't be self-closing |
| 19 | + const containerTags = ['CardTip', 'CardWarning', 'CardInfo', 'CardError', 'CardHelp', 'LinkButton', 'DownloadCard']; |
| 20 | + for (const tag of containerTags) { |
| 21 | + // Fix cases like <CardTip />content</CardTip> |
| 22 | + const regex = new RegExp(`<${tag}([^>]*?)\\s*\\/\\s*>([^<]+)<\\/${tag}>`, 'gi'); |
| 23 | + content = content.replace(regex, (match, attributes, innerContent) => { |
| 24 | + changed = true; |
| 25 | + return `<${tag}${attributes}>${innerContent}</${tag}>`; |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + // Fix self-closing tags |
| 30 | + const selfClosingTags = ['br', 'img', 'hr', 'input', 'meta', 'link']; |
| 31 | + for (const tag of selfClosingTags) { |
| 32 | + const regex = new RegExp(`<${tag}([^>]*?)>(?!</)`, 'gi'); |
| 33 | + const newContent = content.replace(regex, (match, attributes) => { |
| 34 | + if (!attributes.trim().endsWith('/')) { |
| 35 | + changed = true; |
| 36 | + return `<${tag}${attributes} />`; |
| 37 | + } |
| 38 | + return match; |
| 39 | + }); |
| 40 | + content = newContent; |
| 41 | + } |
| 42 | + |
| 43 | + // Fix unclosed br tags specifically |
| 44 | + content = content.replace(/<br>/gi, '<br />'); |
| 45 | + if (content !== fs.readFileSync(filePath, 'utf-8')) changed = true; |
| 46 | + |
| 47 | + if (changed) { |
| 48 | + fs.writeFileSync(filePath, content); |
| 49 | + console.log(`Fixed ${filePath}`); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function processDirectory(dirPath) { |
| 54 | + const entries = fs.readdirSync(dirPath, { withFileTypes: true }); |
| 55 | + |
| 56 | + for (const entry of entries) { |
| 57 | + const fullPath = path.join(dirPath, entry.name); |
| 58 | + |
| 59 | + if (entry.isDirectory()) { |
| 60 | + processDirectory(fullPath); |
| 61 | + } else if (entry.isFile() && entry.name.endsWith('.mdx')) { |
| 62 | + fixMdxFile(fullPath); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Process src/pages directory |
| 68 | +const pagesDir = path.join(__dirname, 'src', 'pages'); |
| 69 | +processDirectory(pagesDir); |
| 70 | + |
| 71 | +console.log('MDX fixes complete!'); |
0 commit comments