/** * ตรวจ/ซ่อม `href` ของช่องทางติดต่อใน content.json ให้กดใช้ได้จริง * * ปัญหาที่เจอ: href ว่าง, เป็น `www.…` ไม่มี scheme, เบอร์ไม่มี `tel:`, * facebook ติด `fbclid`, หรือ label "Call" แต่ค่าไม่ใช่เบอร์ * * ใช้: node scripts/audit-contacts.mjs # ดูอย่างเดียว * node scripts/audit-contacts.mjs --fix # เขียน href ที่แก้ได้กลับลงไฟล์ (สำรองก่อน) */ import fs from "node:fs"; import path from "node:path"; const CONTENT_PATH = path.join("data", "content", "content.json"); const FIX = process.argv.includes("--fix"); const PHONE_LABEL = /call|โทร|phone|tel|เบอร์|มือถือ/i; function emptyish(value) { const text = String(value ?? "") .trim() .toLowerCase(); return ( text === "" || ["-", "–", "n/a", "null", "ไม่มี", "ไม่มีข้อมูล"].includes(text) ); } function normalizePhone(raw) { const text = String(raw ?? "").trim(); if (text === "") return null; const hasPlus = text.startsWith("+"); const digits = text.replace(/[^\d]/g, ""); if (digits.length < 8 || digits.length > 15) return null; if (hasPlus) return `+${digits}`; if (digits.startsWith("66")) return `+${digits}`; if (digits.startsWith("0")) return `+66${digits.slice(1)}`; return `+${digits}`; } /** เหมือน app/lib/contact-link.ts (สคริปต์เป็น .mjs จึงต้องพอร์ตกฎมาใช้เอง) */ function resolve(channel) { const value = String(channel.value ?? "").trim(); const href = String(channel.href ?? "").trim(); const label = String(channel.label ?? "").toLowerCase(); const candidates = [href, value].filter((item) => !emptyish(item)); if (candidates.length === 0) return { href: null, kind: null }; for (const candidate of candidates) { const lower = candidate.toLowerCase(); if (lower.startsWith("tel:")) { const phone = normalizePhone(candidate.slice(4)); if (phone) return { href: `tel:${phone}`, kind: "tel" }; } if (lower.startsWith("mailto:")) { return { href: `mailto:${candidate.slice(7).trim()}`, kind: "mailto" }; } if (/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(candidate)) { return { href: `mailto:${candidate}`, kind: "mailto" }; } if ( /^https?:\/\//i.test(candidate) || /^line:/i.test(candidate) || /^@[\w.\-_]+$/.test(candidate) || /^[\w-]+(\.[\w-]+)+(\/|$)/.test(candidate) ) { let url = candidate; if (/^@[\w.\-_]+$/.test(url)) url = `https://line.me/R/ti/p/~${url.slice(1)}`; if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(url)) url = `https://${url}`; try { const parsed = new URL(url); if (parsed.protocol === "http:") parsed.protocol = "https:"; if (parsed.hostname === "web.facebook.com") parsed.hostname = "www.facebook.com"; for (const key of [...parsed.searchParams.keys()]) { if ( [ "fbclid", "gclid", "_rdc", "_rdr", "igshid", "mibextid", "ref", ].includes(key.toLowerCase()) || key.toLowerCase().startsWith("utm_") ) { parsed.searchParams.delete(key); } } return { href: parsed.toString(), kind: "web" }; } catch { return { href: null, kind: null }; } } const digits = candidate.replace(/\D/g, ""); if ( digits.length >= 8 && digits.length <= 15 && !/[a-z@]/i.test(candidate) ) { const phone = normalizePhone(candidate); if (phone) return { href: `tel:${phone}`, kind: "tel" }; } } return { href: null, kind: null }; } const raw = fs.readFileSync(CONTENT_PATH, "utf8"); const content = JSON.parse(raw); const rows = []; const phoneOwners = new Map(); let changed = 0; for (const cinema of content.cinemas ?? []) { const contacts = cinema.contacts ?? []; if (contacts.length === 0) { rows.push({ slug: cinema.slug, label: "—", value: "—", href: null, kind: null, note: "ไม่มีช่องทางติดต่อ", }); continue; } for (const channel of contacts) { const resolved = resolve(channel); const notes = []; if (!resolved.href) notes.push("ลิงก์ใช้ไม่ได้"); if (PHONE_LABEL.test(channel.label ?? "") && resolved.kind !== "tel") { notes.push("label โทร แต่ค่าไม่ใช่เบอร์"); } if (resolved.kind === "tel") { const owner = phoneOwners.get(resolved.href); if (owner && owner !== cinema.slug) notes.push(`เบอร์ซ้ำกับ ${owner}`); else phoneOwners.set(resolved.href, cinema.slug); } if (channel.href && channel.href !== resolved.href) notes.push("href จะถูกปรับ"); rows.push({ slug: cinema.slug, label: channel.label, value: channel.value, href: resolved.href, kind: resolved.kind, note: notes.join(", "), }); if (FIX && resolved.href !== channel.href) { channel.href = resolved.href; changed++; } } } for (const row of rows) { const state = row.href ? row.kind.padEnd(6) : "ไม่มี "; console.log( `${state} ${row.slug} · ${row.label} · ${String(row.value).slice(0, 42)}${row.note ? ` ⚠ ${row.note}` : ""}`, ); if (row.href && row.href !== row.value) console.log(` → ${row.href}`); } const withoutContacts = rows.filter((row) => row.label === "—").length; console.log( `\nสรุป: ${rows.length - withoutContacts} ช่องทางจาก ${(content.cinemas ?? []).length - withoutContacts} โรง · ไม่มีช่องทางติดต่อ ${withoutContacts} โรง`, ); if (FIX) { const stamp = new Date().toISOString().replace(/[:.]/g, "-"); fs.writeFileSync(`${CONTENT_PATH}.bak-contacts-${stamp}`, raw); fs.writeFileSync(CONTENT_PATH, `${JSON.stringify(content, null, 2)}\n`); console.log( `ปรับ href ${changed} จุด · สำรองไว้ที่ ${CONTENT_PATH}.bak-contacts-${stamp}`, ); } else { console.log("ยังไม่เขียนไฟล์ — ใส่ --fix เพื่อบันทึก href ที่ปรับแล้ว"); }