Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 10s
Build & Deploy / 🧪 QA (push) Failing after 2m24s
Build & Deploy / 🏗️ Build (push) Failing after 3m40s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🩺 Health Check (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 3s
88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import { getPayload } from "payload";
|
|
import configPromise from "./payload.config";
|
|
|
|
async function run() {
|
|
const payload = await getPayload({ config: configPromise });
|
|
const { docs } = await payload.find({
|
|
collection: "posts",
|
|
limit: 1000,
|
|
});
|
|
|
|
console.log(
|
|
`Found ${docs.length} posts. Checking for <TableOfContents />...`,
|
|
);
|
|
|
|
let updatedCount = 0;
|
|
|
|
const removeTOC = (node: any): boolean => {
|
|
let modified = false;
|
|
if (node.children && Array.isArray(node.children)) {
|
|
// Filter out raw text nodes or paragraph nodes that are exactly TableOfContents
|
|
const originalLength = node.children.length;
|
|
node.children = node.children.filter((child: any) => {
|
|
if (
|
|
child.type === "text" &&
|
|
child.text &&
|
|
child.text.includes("<TableOfContents />")
|
|
) {
|
|
return false;
|
|
}
|
|
if (
|
|
child.type === "paragraph" &&
|
|
child.children &&
|
|
child.children.length === 1 &&
|
|
child.children[0].text === "<TableOfContents />"
|
|
) {
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
if (node.children.length !== originalLength) {
|
|
modified = true;
|
|
}
|
|
|
|
// Also clean up any substrings in remaining text nodes
|
|
for (const child of node.children) {
|
|
if (
|
|
child.type === "text" &&
|
|
child.text &&
|
|
child.text.includes("<TableOfContents />")
|
|
) {
|
|
child.text = child.text.replace("<TableOfContents />", "").trim();
|
|
modified = true;
|
|
}
|
|
if (removeTOC(child)) {
|
|
modified = true;
|
|
}
|
|
}
|
|
}
|
|
return modified;
|
|
};
|
|
|
|
for (const doc of docs) {
|
|
if (doc.content?.root) {
|
|
const isModified = removeTOC(doc.content.root);
|
|
if (isModified) {
|
|
try {
|
|
await payload.update({
|
|
collection: "posts",
|
|
id: doc.id,
|
|
data: {
|
|
content: doc.content,
|
|
},
|
|
});
|
|
console.log(`Cleaned up TOC in "${doc.title}".`);
|
|
updatedCount++;
|
|
} catch (e) {
|
|
console.error(`Failed to update ${doc.title}:`, e.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`Cleanup complete. Modified ${updatedCount} posts.`);
|
|
process.exit(0);
|
|
}
|
|
|
|
run();
|