import fs from "fs"; import path from "path"; import { createClient } from "@sanity/client"; import dotenv from "dotenv"; dotenv.config({ path: path.resolve(".env.local") }); const projectId = process.env.SANITY_STUDIO_PROJECT_ID || "gdvj8e5j"; const dataset = process.env.SANITY_STUDIO_DATASET || "production"; const token = process.env.SANITY_AUTH_TOKEN; const client = createClient({ projectId, dataset, token, apiVersion: "2024-10-31", useCdn: false, }); function createBlock(text, style = "normal") { return { _key: "block-" + Math.random().toString(36).slice(2, 9), _type: "block", style, markDefs: [], children: [ { _key: "span-" + Math.random().toString(36).slice(2, 9), _type: "span", marks: [], text, }, ], }; } const md = fs.readFileSync( "/Users/keira/Documents/GitHub/docs/content-backup/ngan-sach-tiec-cuoi-cao-cap-viet-nam-phan-2.md", "utf-8" ); const lines = md.split("\n"); const blocks = []; for (let line of lines) { const trimmed = line.trim(); if (!trimmed) continue; if (trimmed.startsWith("---") || trimmed.startsWith("> /icons/")) continue; if (trimmed.startsWith("# ")) blocks.push(createBlock(trimmed.replace(/^#\s+/, ""), "h1")); else if (trimmed.startsWith("## ")) blocks.push(createBlock(trimmed.replace(/^##\s+/, ""), "h2")); else if (trimmed.startsWith("### ")) blocks.push(createBlock(trimmed.replace(/^###\s+/, ""), "h3")); else if (trimmed.startsWith("#### ")) blocks.push(createBlock(trimmed.replace(/^####\s+/, ""), "h4")); else if (trimmed.startsWith("* ") || trimmed.startsWith("- ")) blocks.push(createBlock(trimmed.replace(/^[*|-]\s+/, ""), "normal")); else blocks.push(createBlock(trimmed, "normal")); } const doc = { _id: "post-vietnam-wedding-guide-part-2", _type: "post", title: "Vietnam Luxury Wedding Cost: A Complete Budget Guide (2025) - Part 2: Hidden Costs & Contingency", slug: { _type: "slug", current: "vietnam-wedding-guide-part-2", }, excerpt: "Deep-dive analysis of hidden resort venue clauses, overtime rigging penalties, outside vendor fees, and emergency financial contingency planning in Vietnam.", publishedAt: "2025-10-09T07:52:56.637Z", author: { _type: "reference", _ref: "author-nhi-vo", }, body: blocks, meta: { _type: "meta", title: "Vietnam Luxury Wedding Cost Guide - Part 2: Hidden Costs | VWG", description: "Avoid hidden costs in 5-star resort wedding contracts across Vietnam. Expert guidance on overtime rigging, outside vendor surcharges, and contingency reserves.", }, }; const res = await client.createOrReplace(doc); console.log("SUCCESS: Created post in Sanity! ID:", res._id);