Files
rustycms/scripts/page-add-prefix.mjs

64 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* Fügt allen Pages das Präfix "page-" hinzu: Dateiname und _slug werden zu "page-" + bisheriger Slug.
* Aktualisiert Referenzen in content/de (alle .json5 nur "links"-Arrays können Page-Slugs enthalten;
* Einzelne Ersetzung pro Slug, um Kollisionen mit Tag-Namen etc. zu vermeiden).
*/
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 PAGE_DIR = path.join(CONTENT_DE, "page");
function parseJson5(str) {
try {
return JSON.parse(str);
} catch {
return null;
}
}
const files = fs.readdirSync(PAGE_DIR).filter((f) => f.endsWith(".json5"));
const oldToNew = new Map();
for (const file of files) {
const oldSlug = file.replace(/\.json5$/, "");
if (oldSlug.startsWith("page-")) continue;
oldToNew.set(oldSlug, "page-" + oldSlug);
}
console.log("Präfix page- für", oldToNew.size, "Pages");
for (const [oldSlug, newSlug] of oldToNew) {
const filePath = path.join(PAGE_DIR, oldSlug + ".json5");
const data = parseJson5(fs.readFileSync(filePath, "utf8"));
data._slug = newSlug;
const newPath = path.join(PAGE_DIR, newSlug + ".json5");
fs.writeFileSync(newPath, JSON.stringify(data, null, 2) + "\n", "utf8");
fs.unlinkSync(filePath);
}
const NAV_DIR = path.join(CONTENT_DE, "navigation");
for (const file of fs.readdirSync(NAV_DIR).filter((f) => f.endsWith(".json5"))) {
const filePath = path.join(NAV_DIR, file);
let content = fs.readFileSync(filePath, "utf8");
let changed = false;
for (const [oldSlug, newSlug] of oldToNew) {
const escaped = oldSlug.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const re = oldSlug === "links"
? new RegExp('"links"(?!\\s*:)', "g")
: new RegExp('"' + escaped + '"', "g");
const newContent = content.replace(re, '"' + newSlug + '"');
if (newContent !== content) {
content = newContent;
changed = true;
}
}
if (changed) fs.writeFileSync(filePath, content, "utf8");
}
console.log("Fertig. Page-Präfix gesetzt und Referenzen aktualisiert.");