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:
102
scripts/normalize-fullwidth-banner-slugs.mjs
Normal file
102
scripts/normalize-fullwidth-banner-slugs.mjs
Normal file
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Normalisiert alle Fullwidth-Banner-Dateien: _slug und Dateiname = "fullwidth-banner-" + beschreibender Teil.
|
||||
* - component-fullwidth-banner-X → fullwidth-banner-X
|
||||
* - component-fullwidthbanner-X → fullwidth-banner-X
|
||||
* - X-fullwidthbanner / homepage-fullwidthbanner → fullwidth-banner-X
|
||||
* - 2512_banner_downloads → fullwidth-banner-2512-banner-downloads
|
||||
* 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 BANNER_DIR = path.join(CONTENT_DE, "fullwidth_banner");
|
||||
|
||||
function normalize(base) {
|
||||
let rest = base.replace(/_/g, "-").toLowerCase();
|
||||
if (rest.startsWith("component-fullwidth-banner-")) {
|
||||
rest = rest.slice(27);
|
||||
} else if (rest.startsWith("component-fullwidthbanner-")) {
|
||||
rest = rest.slice(26);
|
||||
} else if (rest.endsWith("-fullwidthbanner")) {
|
||||
rest = rest.slice(0, -16);
|
||||
} else if (rest.endsWith("-fullwidth-banner")) {
|
||||
rest = rest.slice(0, -17);
|
||||
} else {
|
||||
// z. B. "2512_banner_downloads" -> "2512-banner-downloads"
|
||||
rest = rest.replace(/_/g, "-").toLowerCase();
|
||||
}
|
||||
return "fullwidth-banner-" + rest;
|
||||
}
|
||||
|
||||
function parseJson5(str) {
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(BANNER_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(BANNER_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("Fullwidth-Banner-Slug-Map:", Object.fromEntries(oldToNew));
|
||||
|
||||
for (const file of files) {
|
||||
const base = file.replace(/\.json5$/, "");
|
||||
const filePath = path.join(BANNER_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;
|
||||
const newPath = path.join(BANNER_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. Fullwidth-Banner-Slugs normalisiert und Referenzen aktualisiert.");
|
||||
Reference in New Issue
Block a user