import * as fs from 'fs';
import * as path from 'path';
const MDX_DIR = path.join(process.cwd(), 'content/blog');
const TARGET_FILE = process.argv[2] ? path.resolve(process.argv[2]) : null;
function fixFencedMermaid(content: string): string {
// Regex to find fenced mermaid blocks
// ```mermaid
// graph TD...
// ```
const fencedRegex = /```mermaid\n([\s\S]*?)\n```/g;
return content.replace(fencedRegex, (match, code) => {
// Generate a random ID or use a placeholder
const id = `diagram-${Math.random().toString(36).substr(2, 9)}`;
return `
${code.trim()}
`;
});
}
function processFiles() {
if (TARGET_FILE) {
console.log(`Processing single file: ${TARGET_FILE}`);
if (fs.existsSync(TARGET_FILE)) {
const content = fs.readFileSync(TARGET_FILE, 'utf8');
const fixed = fixFencedMermaid(content);
if (content !== fixed) {
fs.writeFileSync(TARGET_FILE, fixed);
console.log(`✅ Fixed fenced mermaid in: ${TARGET_FILE}`);
} else {
console.log(`- No changes needed.`);
}
} else {
console.error(`File not found: ${TARGET_FILE}`);
}
return;
}
const files = fs.readdirSync(MDX_DIR).filter(f => f.endsWith('.mdx'));
let fixCount = 0;
for (const file of files) {
const filePath = path.join(MDX_DIR, file);
const content = fs.readFileSync(filePath, 'utf8');
const fixed = fixFencedMermaid(content);
if (content !== fixed) {
fs.writeFileSync(filePath, fixed);
fixCount++;
console.log(`✅ Fixed fenced mermaid in: ${file}`);
}
}
console.log(`\nTotal fixed: ${fixCount}`);
}
processFiles();