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 ...`, ); 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("") ) { return false; } if ( child.type === "paragraph" && child.children && child.children.length === 1 && child.children[0].text === "" ) { 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("") ) { child.text = child.text.replace("", "").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();