101 lines
3.0 KiB
JavaScript
101 lines
3.0 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Benennt content/de/link/*.json5 um: _slug und Dateiname werden zu "link-" + slugify(name).
|
|
* Aktualisiert alle Referenzen in content/de (navigation, link_list, etc.).
|
|
*/
|
|
|
|
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 LINK_DIR = path.join(CONTENT_DE, "link");
|
|
|
|
function slugify(s) {
|
|
if (!s || typeof s !== "string") return "";
|
|
return s
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/\s+/g, "-")
|
|
.replace(/[ää]/g, "ae")
|
|
.replace(/[öö]/g, "oe")
|
|
.replace(/[üü]/g, "ue")
|
|
.replace(/ß/g, "ss")
|
|
.replace(/[^a-z0-9-]/g, "-")
|
|
.replace(/-+/g, "-")
|
|
.replace(/^-|-$/g, "");
|
|
}
|
|
|
|
function parseJson5(str) {
|
|
try {
|
|
return JSON.parse(str);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const linkFiles = fs.readdirSync(LINK_DIR).filter((f) => f.endsWith(".json5"));
|
|
const oldToNew = new Map();
|
|
const newSlugs = new Set();
|
|
|
|
for (const file of linkFiles) {
|
|
const oldSlug = file.replace(/\.json5$/, "");
|
|
const raw = fs.readFileSync(path.join(LINK_DIR, file), "utf8");
|
|
const data = parseJson5(raw);
|
|
if (!data) continue;
|
|
const name = (data.name || data.linkName || oldSlug).trim();
|
|
let base = slugify(name) || slugify(oldSlug);
|
|
if (!base) base = "link";
|
|
let newSlug = "link-" + base;
|
|
let n = 0;
|
|
while (newSlugs.has(newSlug)) {
|
|
n++;
|
|
newSlug = "link-" + base + "-" + n;
|
|
}
|
|
newSlugs.add(newSlug);
|
|
oldToNew.set(oldSlug, newSlug);
|
|
}
|
|
|
|
console.log("Slug-Map:", Object.fromEntries(oldToNew));
|
|
|
|
// 1) Neue Link-Dateien schreiben, alte löschen
|
|
for (const file of linkFiles) {
|
|
const oldSlug = file.replace(/\.json5$/, "");
|
|
const newSlug = oldToNew.get(oldSlug);
|
|
if (!newSlug) continue;
|
|
const filePath = path.join(LINK_DIR, file);
|
|
const data = parseJson5(fs.readFileSync(filePath, "utf8"));
|
|
data._slug = newSlug;
|
|
data.internal = newSlug;
|
|
const newPath = path.join(LINK_DIR, newSlug + ".json5");
|
|
fs.writeFileSync(newPath, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
if (newPath !== filePath) fs.unlinkSync(filePath);
|
|
}
|
|
|
|
// 2) Referenzen in allen content/de/**/*.json5 ersetzen
|
|
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 regex = new RegExp('"' + oldSlug.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + '"', "g");
|
|
if (regex.test(content)) {
|
|
content = content.replace(regex, '"' + newSlug + '"');
|
|
changed = true;
|
|
}
|
|
}
|
|
if (changed) fs.writeFileSync(filePath, content, "utf8");
|
|
});
|
|
|
|
console.log("Fertig. Links umbenannt und Referenzen aktualisiert.");
|