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:
99
scripts/normalize-html-slugs.mjs
Normal file
99
scripts/normalize-html-slugs.mjs
Normal file
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Normalisiert alle HTML-Dateien: _slug und Dateiname = "html-" + beschreibender Teil.
|
||||
* - component-html-X → html-X
|
||||
* - redirectTo-X → html-X
|
||||
* - X-html / page_links_embedded-html → html-X (mit - statt _)
|
||||
* Aktualisiert alle Referenzen in content/de.
|
||||
*/
|
||||
|
||||
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 HTML_DIR = path.join(CONTENT_DE, "html");
|
||||
|
||||
function normalize(base) {
|
||||
let rest = base.replace(/_/g, "-").toLowerCase();
|
||||
if (rest.startsWith("component-html-")) {
|
||||
rest = rest.slice(15);
|
||||
} else if (rest.startsWith("redirectto-")) {
|
||||
rest = rest.slice(11);
|
||||
} else if (rest.endsWith("-html")) {
|
||||
rest = rest.slice(0, -5);
|
||||
} else if (rest.startsWith("html-")) {
|
||||
rest = rest.slice(5);
|
||||
}
|
||||
return "html-" + rest;
|
||||
}
|
||||
|
||||
function parseJson5(str) {
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(HTML_DIR).filter((f) => f.endsWith(".json5")).sort();
|
||||
const oldToNew = new Map();
|
||||
const used = new Set();
|
||||
|
||||
for (const file of files) {
|
||||
const base = file.replace(/\.json5$/, "");
|
||||
const raw = fs.readFileSync(path.join(HTML_DIR, file), "utf8");
|
||||
const data = parseJson5(raw);
|
||||
const oldSlug = data?._slug || base;
|
||||
let newSlug = normalize(base);
|
||||
let n = 0;
|
||||
while (used.has(newSlug)) {
|
||||
n++;
|
||||
newSlug = normalize(base) + (n === 1 ? "-1" : "-" + n);
|
||||
}
|
||||
used.add(newSlug);
|
||||
oldToNew.set(oldSlug, newSlug);
|
||||
}
|
||||
|
||||
console.log("HTML-Slug-Map:", Object.fromEntries(oldToNew));
|
||||
|
||||
for (const file of files) {
|
||||
const base = file.replace(/\.json5$/, "");
|
||||
const filePath = path.join(HTML_DIR, file);
|
||||
const data = parseJson5(fs.readFileSync(filePath, "utf8"));
|
||||
const oldSlug = data?._slug || base;
|
||||
const newSlug = oldToNew.get(oldSlug);
|
||||
if (!newSlug) continue;
|
||||
data._slug = newSlug;
|
||||
if (data.name && data.name === oldSlug) data.name = newSlug;
|
||||
const newPath = path.join(HTML_DIR, newSlug + ".json5");
|
||||
fs.writeFileSync(newPath, JSON.stringify(data, null, 2) + "\n", "utf8");
|
||||
if (path.basename(newPath) !== file) 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 escaped = oldSlug.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const newContent = content.replace(new RegExp('"' + escaped + '"', "g"), '"' + newSlug + '"');
|
||||
if (newContent !== content) {
|
||||
content = newContent;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) fs.writeFileSync(filePath, content, "utf8");
|
||||
});
|
||||
|
||||
console.log("Fertig. HTML-Slugs normalisiert und Referenzen aktualisiert.");
|
||||
Reference in New Issue
Block a user