RustyCMS: file-based headless CMS — API, Admin UI (content, types, assets), Docker/Caddy, image transform; only demo type and demo content in version control
Made-with: Cursor
This commit is contained in:
63
scripts/page-add-prefix.mjs
Normal file
63
scripts/page-add-prefix.mjs
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/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.");
|
||||
Reference in New Issue
Block a user