wip page links and display

This commit is contained in:
kbe
2025-08-19 11:34:40 +02:00
parent dd3a0240f9
commit 140552a35f
4 changed files with 142 additions and 46 deletions

View File

@@ -4,15 +4,22 @@ const he = require('he');
const DATA_DIR = path.join(__dirname, '..', 'data', 'wordpress');
const CONTENT_DIR = path.join(__dirname, '..', 'content');
const PAGES_DIR = path.join(CONTENT_DIR, 'pages');
function generateContent() {
const posts = JSON.parse(fs.readFileSync(path.join(DATA_DIR, 'posts.json'), 'utf8'));
// Ensure content directory exists
const pages = JSON.parse(fs.readFileSync(path.join(DATA_DIR, 'pages.json'), 'utf8'));
// Ensure content directories exist
if (!fs.existsSync(CONTENT_DIR)) {
fs.mkdirSync(CONTENT_DIR, { recursive: true });
}
if (!fs.existsSync(PAGES_DIR)) {
fs.mkdirSync(PAGES_DIR, { recursive: true });
}
// Process posts
posts.forEach(post => {
const slug = post.slug;
const date = new Date(post.date);
@@ -61,7 +68,58 @@ function generateContent() {
contentHtml = contentHtml
.replace(/<p>\s*<\/p>/g, '') // Remove empty paragraphs
.replace(/<\/p>\s*<p>/g, '\n\n') // Replace paragraph breaks with newlines
.replace(/<\/p>\s*<p>/g, '\n\n'); // Replace paragraph breaks with newlines
const content = `---
${Object.entries(frontmatter)
.map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
.join('\n')}
---
${contentHtml.trim()}`;
fs.writeFileSync(path.join(contentDir, 'index.md'), content);
});
// Process pages
pages.forEach(page => {
const slug = page.slug;
const contentDir = path.join(PAGES_DIR, slug);
if (!fs.existsSync(contentDir)) {
fs.mkdirSync(contentDir, { recursive: true });
}
const frontmatter = {
title: he.decode(page.title.rendered),
slug: slug,
type: "pages",
layout: "single",
wordpress_id: page.id,
date: page.date,
modified: page.modified,
draft: page.status !== 'publish',
aliases: [`/${slug}/`]
};
// Decode HTML entities in the content and clean up HTML tags
let contentHtml = he.decode(page.content.rendered);
// Convert absolute URLs in a href to relative URLs
contentHtml = contentHtml.replace(/<a\s+[^>]*href="([^"]+)"[^>]*>/g, (match, href) => {
// Check if the href is an absolute URL (starts with http:// or https://)
if (href.startsWith('http://') || href.startsWith('https://')) {
// Extract the path part of the URL
const url = new URL(href);
// Return the modified a tag with relative URL
return match.replace(href, url.pathname);
}
return match;
});
contentHtml = contentHtml
.replace(/<p>\s*<\/p>/g, '') // Remove empty paragraphs
.replace(/<\/p>\s*<p>/g, '\n\n'); // Replace paragraph breaks with newlines
const content = `---
${Object.entries(frontmatter)
@@ -74,6 +132,7 @@ ${contentHtml.trim()}`;
});
console.log(`✅ Generated ${posts.length} content files`);
console.log(`✅ Generated ${pages.length} page files`);
}
generateContent();