56 lines
1.9 KiB
JavaScript
56 lines
1.9 KiB
JavaScript
#!/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(", "));
|