import axios from 'axios'; import * as fs from 'fs'; import * as path from 'path'; const WP_URL = 'https://klz-cables.com'; async function fetchAllPosts() { let page = 1; let allPosts: any[] = []; while (true) { console.log(`Fetching posts page ${page}...`); try { const response = await axios.get(`${WP_URL}/wp-json/wp/v2/posts`, { params: { per_page: 100, page: page, _embed: true } }); const posts = response.data; if (posts.length === 0) break; allPosts = allPosts.concat(posts); page++; } catch (error: any) { if (error.response && error.response.status === 400) { // End of pagination break; } console.error('Error fetching posts:', error); break; } } return allPosts; } function generateMdxContent(post: any, locale: 'en' | 'de') { const frontmatter = { title: post.title.rendered, date: post.date, excerpt: post.excerpt.rendered.replace(/<[^>]*>/g, '').trim(), featuredImage: post._embedded?.['wp:featuredmedia']?.[0]?.source_url || null, locale: locale }; return `--- ${JSON.stringify(frontmatter, null, 2)} --- # ${post.title.rendered} ${post.content.rendered} `; } async function run() { const posts = await fetchAllPosts(); console.log(`Fetched ${posts.length} posts.`); for (const post of posts) { // Determine locale. // If using Polylang, we might check categories or tags, or a specific field if exposed. // Or we can check the link structure if it contains /de/ or /en/ (though API link might be different) // Let's try to guess from the link or content language detection if needed. // For now, let's assume we can filter by category or just save all and manually sort if needed. // Actually, Polylang usually exposes 'lang' in the API if configured, or we might need to fetch by lang. // Simple heuristic: check if link contains '/de/' const locale = post.link.includes('/de/') ? 'de' : 'en'; const mdx = generateMdxContent(post, locale); const outDir = path.join(process.cwd(), 'data', 'blog', locale); if (!fs.existsSync(outDir)) { fs.mkdirSync(outDir, { recursive: true }); } const filename = `${post.slug}.mdx`; fs.writeFileSync(path.join(outDir, filename), mdx); console.log(`Saved ${filename} (${locale})`); } } run().catch(console.error);