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