Files
mintel.me/scripts/extract-to-mdx.js
Marc Mintel 66b80117b1
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 6s
Build & Deploy / 🏗️ Build (push) Failing after 7m53s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🩺 Smoke Test (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 1s
Refactor: remove PayloadCMS, use MDX for content and TSX for design, use direct email for contact
2026-05-05 10:51:11 +02:00

71 lines
1.9 KiB
JavaScript

import fs from 'fs';
import path from 'path';
const rawData = fs.readFileSync('./tmp_posts.json', 'utf8');
const data = JSON.parse(rawData);
function parseNode(node) {
if (node.type === 'text') {
let text = node.text;
if (node.format === 1) text = `**${text}**`;
if (node.format === 2) text = `*${text}*`;
if (node.format === 8) text = `\`${text}\``;
return text;
}
if (node.type === 'paragraph') {
return node.children.map(parseNode).join('') + '\n\n';
}
if (node.type === 'heading') {
const level = node.tag ? node.tag.replace('h', '') : '2';
const hashes = '#'.repeat(parseInt(level));
return `${hashes} ${node.children.map(parseNode).join('')}\n\n`;
}
if (node.type === 'list') {
const isOrdered = node.listType === 'number';
return node.children.map((li, i) => {
const prefix = isOrdered ? `${i + 1}.` : '-';
return `${prefix} ${li.children.map(parseNode).join('')}`;
}).join('\n') + '\n\n';
}
if (node.type === 'quote') {
return `> ${node.children.map(parseNode).join('')}\n\n`;
}
if (node.type === 'link') {
return `[${node.children.map(parseNode).join('')}](${node.fields.url})`;
}
if (node.children) {
return node.children.map(parseNode).join('');
}
return '';
}
const outDir = './apps/web/src/content/posts';
fs.mkdirSync(outDir, { recursive: true });
for (const post of data.docs) {
const frontmatter = `---
title: "${post.title.replace(/"/g, '\\"')}"
date: "${post.date}"
description: "${post.description ? post.description.replace(/"/g, '\\"') : ''}"
tags: [${(post.tags || []).map(t => `"${t.tag}"`).join(', ')}]
---
`;
let markdown = frontmatter;
if (post.content && post.content.root && post.content.root.children) {
markdown += post.content.root.children.map(parseNode).join('');
}
const filename = `${post.slug}.mdx`;
fs.writeFileSync(path.join(outDir, filename), markdown.trim() + '\n');
console.log(`Wrote ${filename}`);
}