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:
55
scripts/normalize-text-fragment-slugs.mjs
Normal file
55
scripts/normalize-text-fragment-slugs.mjs
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Normalisiert alle text_fragment-Dateien: Präfix "text-fragment-", _slug und Dateiname.
|
||||
* Aktualisiert Referenzen in content/de (textFragments in searchable_text).
|
||||
*/
|
||||
|
||||
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 TEXT_FRAGMENT_DIR = path.join(CONTENT_DE, "text_fragment");
|
||||
|
||||
function parseJson5(str) {
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(TEXT_FRAGMENT_DIR).filter((f) => f.endsWith(".json5")).sort();
|
||||
const oldToNew = new Map();
|
||||
|
||||
for (const file of files) {
|
||||
const base = file.replace(/\.json5$/, "");
|
||||
const filePath = path.join(TEXT_FRAGMENT_DIR, file);
|
||||
const raw = fs.readFileSync(filePath, "utf8");
|
||||
const data = parseJson5(raw);
|
||||
const oldSlug = data?._slug ?? base;
|
||||
const newSlug = "text-fragment-" + oldSlug;
|
||||
oldToNew.set(oldSlug, newSlug);
|
||||
|
||||
const newData = { ...data, _slug: newSlug };
|
||||
const newFile = newSlug + ".json5";
|
||||
const newPath = path.join(TEXT_FRAGMENT_DIR, newFile);
|
||||
fs.writeFileSync(newPath, JSON.stringify(newData, null, 2) + "\n", "utf8");
|
||||
if (newFile !== file) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
// Referenzen in searchable_text (textFragments-Arrays) ersetzen
|
||||
const searchableDir = path.join(CONTENT_DE, "searchable_text");
|
||||
for (const file of fs.readdirSync(searchableDir).filter((f) => f.endsWith(".json5"))) {
|
||||
const filePath = path.join(searchableDir, file);
|
||||
let raw = fs.readFileSync(filePath, "utf8");
|
||||
for (const [oldSlug, newSlug] of oldToNew) {
|
||||
raw = raw.replace(new RegExp('"' + oldSlug.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + '"', "g"), '"' + newSlug + '"');
|
||||
}
|
||||
fs.writeFileSync(filePath, raw, "utf8");
|
||||
}
|
||||
|
||||
console.log("Normalized", oldToNew.size, "text_fragment slugs:", [...oldToNew.entries()].map(([a, b]) => a + " -> " + b).join(", "));
|
||||
Reference in New Issue
Block a user