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:
92
scripts/rename-posts.mjs
Normal file
92
scripts/rename-posts.mjs
Normal file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Benennt content/de/post/*.json5 um: _slug und Dateiname = slug (ohne führendes /).
|
||||
* Kollisionen erhalten Suffix -1, -2, …
|
||||
* 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 POST_DIR = path.join(CONTENT_DE, "post");
|
||||
|
||||
function slugFromPost(s) {
|
||||
if (!s || typeof s !== "string") return "";
|
||||
return s
|
||||
.trim()
|
||||
.replace(/^\//, "")
|
||||
.replace(/\.html?$/i, "")
|
||||
.replace(/_/g, "-")
|
||||
.replace(/[^a-z0-9-]/gi, "-")
|
||||
.replace(/-+/g, "-")
|
||||
.replace(/^-|-$/g, "");
|
||||
}
|
||||
|
||||
function parseJson5(str) {
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(POST_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(POST_DIR, file), "utf8");
|
||||
const data = parseJson5(raw);
|
||||
if (!data) continue;
|
||||
const slugVal = data.slug || "";
|
||||
let base = slugFromPost(slugVal) || oldSlug;
|
||||
if (!base) base = "post";
|
||||
const count = (baseCount.get(base) || 0) + 1;
|
||||
baseCount.set(base, count);
|
||||
const newSlug = count === 1 ? base : base + "-" + (count - 1);
|
||||
oldToNew.set(oldSlug, newSlug);
|
||||
}
|
||||
|
||||
console.log("Slug-Map (Anzahl:", oldToNew.size, "):", Object.fromEntries([...oldToNew.entries()].slice(0, 10)), "...");
|
||||
|
||||
for (const file of files) {
|
||||
const oldSlug = file.replace(/\.json5$/, "");
|
||||
const newSlug = oldToNew.get(oldSlug);
|
||||
if (!newSlug) continue;
|
||||
const filePath = path.join(POST_DIR, file);
|
||||
const data = parseJson5(fs.readFileSync(filePath, "utf8"));
|
||||
data._slug = newSlug;
|
||||
const newPath = path.join(POST_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 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. Posts umbenannt und Referenzen aktualisiert.");
|
||||
Reference in New Issue
Block a user