#!/usr/bin/env node /** * Benennt content/de/link_list/*.json5 um: _slug und Dateiname = "link-list-" + slugify(headline). * Kollisionen (z. B. gleiche Headline "Links") erhalten Suffix -1, -2, … * Aktualisiert alle Referenzen in content/de (pages, posts, …). */ import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const CONTENT_DE = path.join(__dirname, "..", "content", "de"); const LINK_LIST_DIR = path.join(CONTENT_DE, "link_list"); function slugify(s) { if (!s || typeof s !== "string") return ""; return s .trim() .toLowerCase() .replace(/\s+/g, "-") .replace(/[ää]/g, "ae") .replace(/[öö]/g, "oe") .replace(/[üü]/g, "ue") .replace(/ß/g, "ss") .replace(/[^a-z0-9-]/g, "-") .replace(/-+/g, "-") .replace(/^-|-$/g, ""); } function parseJson5(str) { try { return JSON.parse(str); } catch { return null; } } const files = fs.readdirSync(LINK_LIST_DIR).filter((f) => f.endsWith(".json5")).sort(); const oldToNew = new Map(); const baseCount = new Map(); for (const file of files) { const oldSlug = file.replace(/\.json5$/, ""); const raw = fs.readFileSync(path.join(LINK_LIST_DIR, file), "utf8"); const data = parseJson5(raw); if (!data) continue; const headline = (data.headline || oldSlug).trim(); let base = slugify(headline) || slugify(oldSlug); if (!base) base = "link-list"; const count = (baseCount.get(base) || 0) + 1; baseCount.set(base, count); const newSlug = count === 1 ? "link-list-" + base : "link-list-" + base + "-" + (count - 1); oldToNew.set(oldSlug, newSlug); } console.log("Slug-Map:", Object.fromEntries(oldToNew)); for (const file of files) { const oldSlug = file.replace(/\.json5$/, ""); const newSlug = oldToNew.get(oldSlug); if (!newSlug) continue; const filePath = path.join(LINK_LIST_DIR, file); const data = parseJson5(fs.readFileSync(filePath, "utf8")); data._slug = newSlug; const newPath = path.join(LINK_LIST_DIR, newSlug + ".json5"); fs.writeFileSync(newPath, JSON.stringify(data, null, 2) + "\n", "utf8"); if (newPath !== filePath) fs.unlinkSync(filePath); } function walkDir(dir, fn) { const entries = fs.readdirSync(dir, { withFileTypes: true }); for (const e of entries) { const full = path.join(dir, e.name); if (e.isDirectory()) walkDir(full, fn); else if (e.name.endsWith(".json5")) fn(full); } } walkDir(CONTENT_DE, (filePath) => { let content = fs.readFileSync(filePath, "utf8"); let changed = false; for (const [oldSlug, newSlug] of oldToNew) { if (oldSlug === newSlug) continue; const regex = new RegExp('"' + oldSlug.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + '"', "g"); if (regex.test(content)) { content = content.replace(regex, '"' + newSlug + '"'); changed = true; } } if (changed) fs.writeFileSync(filePath, content, "utf8"); }); console.log("Fertig. Link-Listen umbenannt und Referenzen aktualisiert.");