Files
ViciDialWebsite/scripts/export-wp.mjs
T
jamespandClaude Opus 5 63c2f6c0e7 Preserve the line breaks that space out page sections
Page content separates its major sections with leading <br><br>, which
the old site renders as a blank line before each section heading. Those
breaks were being lost, leaving every section butted up against the
previous one.

Two causes. The content contains malformed `</br>` closing tags, which
browsers treat as a line break but an HTML parser discards, so they
never survived the HTML->Markdown conversion; they're now rewritten to
<br />. And Markdown drops a hard break at the start of a paragraph, so
turndown now emits literal <br /> tags instead of the trailing-two-space
form -- inline, with no trailing newline, since a lone tag on its own
line starts a raw HTML block (CommonMark type 7) and would stop the rest
of the paragraph being parsed as Markdown at all.

Verified against the live old site: paragraph count, <br> count, bold
heading count and per-paragraph break placement now all match exactly.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 19:29:39 -04:00

188 lines
7.7 KiB
JavaScript

#!/usr/bin/env node
// One-time (re-runnable) export of the WordPress content into Astro content collections.
// Requires read-only DB creds via env vars: WP_DB_USER, WP_DB_PASSWORD
// (host/db default to the known wordpress_vicidial instance, override via WP_DB_HOST/WP_DB_NAME).
//
// WP_DB_USER=viciadmin WP_DB_PASSWORD=... node scripts/export-wp.mjs
import mysql from 'mysql2/promise';
import TurndownService from 'turndown';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, '..');
const DB_HOST = process.env.WP_DB_HOST || '192.168.202.2';
const DB_USER = process.env.WP_DB_USER;
const DB_PASSWORD = process.env.WP_DB_PASSWORD;
const DB_NAME = process.env.WP_DB_NAME || 'wordpress_vicidial';
const TABLE_PREFIX = 'vhmain_';
if (!DB_USER || !DB_PASSWORD) {
console.error('Set WP_DB_USER and WP_DB_PASSWORD (read-only WordPress DB creds) before running.');
process.exit(1);
}
const UPLOADS_SRC = '/srv/www/vhosts/vicidial-old/wp-content/uploads';
const UPLOADS_DEST = path.join(ROOT, 'src/assets/uploads');
const PAGES_DEST = path.join(ROOT, 'src/content/pages');
const POSTS_DEST = path.join(ROOT, 'src/content/posts');
const NAV_DEST = path.join(ROOT, 'src/data/nav.json');
const turndown = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced' });
// Emit literal <br /> rather than Markdown's trailing-two-spaces hard break. Markdown drops a
// hard break at the start of a paragraph, and this content uses leading breaks deliberately to
// space out sections -- raw tags survive that round-trip, and render identically.
// Deliberately no trailing newline: a lone tag on its own line starts a raw HTML block
// (CommonMark type 7), which would stop the rest of the paragraph being parsed as Markdown.
turndown.addRule('lineBreak', {
filter: 'br',
replacement: () => '<br />',
});
// This DB's post_content has literal backslash-n sequences from a past export/import,
// not real newlines -- normalize before treating it as HTML/text. The content also contains
// malformed `</br>` closing tags, which browsers (and therefore the old site) render as a
// line break, but an HTML parser drops -- rewrite them so those breaks survive.
function fixLiteralNewlines(text) {
return text
.replace(/\\r\\n/g, '\n')
.replace(/\\n/g, '\n')
.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n')
.replace(/<\/br\s*>/gi, '<br />');
}
// Classic-editor content is stored without <p> tags and rendered through WordPress's
// wpautop filter at display time. Reproduce that so converted Markdown keeps paragraph breaks.
function wpautop(text) {
return text
.split(/\n\s*\n/)
.map((block) => block.trim())
.filter(Boolean)
.map((block) => {
if (/^<(h[1-6]|div|ul|ol|li|table|blockquote|p)[ >]/i.test(block)) return block;
return `<p>${block.replace(/\n/g, '<br>\n')}</p>`;
})
.join('\n');
}
function frontmatterString(value) {
return `"${String(value).replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}
function rewriteImageUrls(html, fromDir) {
const relToUploads = path.relative(fromDir, UPLOADS_DEST);
return html.replace(
/(?:https?:\/\/(?:wp\.vicidial\.(?:com|net)|www\.vicidial\.com))?\/wp-content\/uploads\/([^\s"')]+)/g,
(_match, relPath) => `${relToUploads}/${relPath}`
);
}
async function copyAttachments() {
// Copy the whole uploads tree rather than just the 82 attachment rows: WordPress generates
// multiple resized variants per image (e.g. "-1008x1024.png") that content <img> tags/srcsets
// reference directly but that aren't tracked as their own attachment posts in the DB.
await fs.rm(UPLOADS_DEST, { recursive: true, force: true });
await fs.cp(UPLOADS_SRC, UPLOADS_DEST, { recursive: true });
console.log(`Copied uploads tree to ${UPLOADS_DEST}`);
}
async function exportPostType(conn, postType, destDir, { attachmentPaths, thumbnails }) {
const [rows] = await conn.execute(
`SELECT ID, post_title, post_name, post_date, post_content, post_excerpt
FROM ${TABLE_PREFIX}posts WHERE post_type = ? AND post_status = 'publish' ORDER BY post_date DESC`,
[postType]
);
await fs.mkdir(destDir, { recursive: true });
for (const row of rows) {
const slug = row.post_name || String(row.ID);
let html = fixLiteralNewlines(row.post_content || '');
if (!/^\s*<(p|div|h[1-6]|ul|ol|table)[ >]/i.test(html)) {
html = wpautop(html);
}
html = rewriteImageUrls(html, destDir);
const markdown = turndown.turndown(html);
let excerpt = fixLiteralNewlines(row.post_excerpt || '');
if (!excerpt) {
const plain = html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
// 60 words: what the theme's front-page loop sets ($sbExcerptLength = 60).
// The slideshow truncates this further to 30 at render, as the theme does.
excerpt = plain.split(' ').slice(0, 60).join(' ');
}
const frontmatter = [`title: ${frontmatterString(row.post_title)}`, `slug: ${frontmatterString(slug)}`];
if (postType === 'post') {
frontmatter.push(`date: ${frontmatterString(row.post_date.toISOString())}`);
frontmatter.push(`excerpt: ${frontmatterString(excerpt)}`);
const thumbId = thumbnails.get(row.ID);
const thumbFile = thumbId && attachmentPaths.get(Number(thumbId));
if (thumbFile) {
const relToUploads = path.relative(destDir, UPLOADS_DEST);
frontmatter.push(`featuredImage: ${frontmatterString(`${relToUploads}/${thumbFile}`)}`);
}
}
await fs.writeFile(path.join(destDir, `${slug}.md`), `---\n${frontmatter.join('\n')}\n---\n\n${markdown}\n`);
}
console.log(`Wrote ${rows.length} ${postType}(s) to ${destDir}`);
}
async function exportNav(conn) {
const [navRows] = await conn.execute(
`SELECT p.ID, p.menu_order, pm_obj.meta_value AS object_id
FROM ${TABLE_PREFIX}posts p
JOIN ${TABLE_PREFIX}postmeta pm_obj ON pm_obj.post_id = p.ID AND pm_obj.meta_key = '_menu_item_object_id'
WHERE p.post_type = 'nav_menu_item' AND p.post_status = 'publish'
ORDER BY p.menu_order`
);
const targetIds = navRows.map((r) => Number(r.object_id));
const [targetRows] = targetIds.length
? await conn.query(`SELECT ID, post_title, post_name FROM ${TABLE_PREFIX}posts WHERE ID IN (?)`, [targetIds])
: [[]];
const targets = new Map(targetRows.map((r) => [r.ID, r]));
const nav = navRows.map((r) => {
const target = targets.get(Number(r.object_id));
return { title: target?.post_title ?? '', href: `/${target?.post_name ?? ''}` };
});
await fs.mkdir(path.dirname(NAV_DEST), { recursive: true });
await fs.writeFile(NAV_DEST, `${JSON.stringify(nav, null, 2)}\n`);
console.log(`Wrote ${nav.length} nav items to ${NAV_DEST}`);
}
async function main() {
const conn = await mysql.createConnection({ host: DB_HOST, user: DB_USER, password: DB_PASSWORD, database: DB_NAME });
const [attachmentRows] = await conn.execute(
`SELECT p.ID, pm.meta_value AS file FROM ${TABLE_PREFIX}posts p
JOIN ${TABLE_PREFIX}postmeta pm ON pm.post_id = p.ID AND pm.meta_key = '_wp_attached_file'
WHERE p.post_type = 'attachment'`
);
const attachmentPaths = new Map(attachmentRows.map((r) => [r.ID, r.file]));
const [thumbRows] = await conn.execute(`SELECT post_id, meta_value FROM ${TABLE_PREFIX}postmeta WHERE meta_key = '_thumbnail_id'`);
const thumbnails = new Map(thumbRows.map((r) => [r.post_id, r.meta_value]));
await copyAttachments();
await exportPostType(conn, 'page', PAGES_DEST, { attachmentPaths, thumbnails });
await exportPostType(conn, 'post', POSTS_DEST, { attachmentPaths, thumbnails });
await exportNav(conn);
await conn.end();
}
main().catch((err) => {
console.error(err);
process.exit(1);
});