import fs from 'fs/promises';
import path from 'path';
import matter from 'gray-matter';
function lexicalToMarkdown(node: any, depth = 0): string {
if (!node) return '';
if (typeof node === 'string') return node;
if (node.type === 'text') {
let text = node.text || '';
if (node.format === 1) text = `**${text}**`; // Bold
if (node.format === 2) text = `*${text}*`; // Italic
return text;
}
if (node.type === 'heading') {
const level = parseInt((node.tag || 'h1').replace('h', '')) || 1;
const prefix = '#'.repeat(level);
const text = (node.children || []).map((c: any) => lexicalToMarkdown(c, depth)).join('');
return `${prefix} ${text}\n\n`;
}
if (node.type === 'paragraph') {
const text = (node.children || []).map((c: any) => lexicalToMarkdown(c, depth)).join('');
return text.trim() ? `${text}\n\n` : '\n';
}
if (node.type === 'list') {
const isOrdered = node.tag === 'ol';
const items = (node.children || [])
.map((c: any, index: number) => {
const prefix = isOrdered ? `${index + 1}.` : '-';
return `${prefix} ${lexicalToMarkdown(c, depth + 1).trim()}`;
})
.join('\n');
return `${items}\n\n`;
}
if (node.type === 'listitem') {
return (node.children || []).map((c: any) => lexicalToMarkdown(c, depth)).join('');
}
if (node.type === 'quote') {
const text = (node.children || []).map((c: any) => lexicalToMarkdown(c, depth)).join('');
return `> ${text}\n\n`;
}
if (node.type === 'link') {
const url = node.fields?.url || node.url || '';
const text = (node.children || []).map((c: any) => lexicalToMarkdown(c, depth)).join('');
return `[${text}](${url})`;
}
if (node.type === 'upload') {
const url = node.value?.url || '';
const alt = node.value?.alt || node.value?.filename || '';
return `\n\n`;
}
if (node.type === 'block') {
const blockType = node.fields?.blockType;
if (blockType === 'contactSection') {
return `\n\n`;
}
if (blockType === 'heroSection') {
return `\n\n`;
}
if (blockType === 'features') {
return `\n\n`;
}
// Generic MDX block wrapper if unknown
return `\n\n`;
}
if (node.children && Array.isArray(node.children)) {
return node.children.map((c: any) => lexicalToMarkdown(c, depth)).join('');
}
return '';
}
async function convertDir(dir: string) {
try {
const files = await fs.readdir(dir);
for (const f of files) {
if (!f.endsWith('.mdx')) continue;
const filePath = path.join(dir, f);
const fileContent = await fs.readFile(filePath, 'utf-8');
const { data, content } = matter(fileContent);
let ast = null;
try {
if (content.trim().startsWith('{')) {
ast = JSON.parse(content);
}
} catch (e) {
// Not JSON
}
if (ast && ast.root) {
const mdContent = lexicalToMarkdown(ast.root);
const newFileContent = matter.stringify(mdContent, data);
await fs.writeFile(filePath, newFileContent);
console.log(`Converted ${filePath}`);
}
}
} catch (error) {
console.error(`Failed reading directory ${dir}`, error);
}
}
async function main() {
const contentDir = path.join(process.cwd(), 'content');
const dirs = await fs.readdir(contentDir);
for (const dir of dirs) {
const fullPath = path.join(contentDir, dir);
const stat = await fs.stat(fullPath);
if (stat.isDirectory()) {
await convertDir(fullPath);
}
}
}
main().catch(console.error);