fix(contacts): ช่องทางติดต่อกดใช้ได้จริง (tel:/mailto:/โซเชียล) + ซ่อมข้อมูลเดิม
ปัญหา: href ในข้อมูลดิบไม่สม่ำเสมอ (ว่าง / www. ไม่มี scheme / เบอร์ไม่มี tel: / facebook ติด fbclid) ทำให้กดแล้วไม่เกิดอะไร และปุ่มโทรใช้ไม่ได้ - app/lib/contact-link.ts: resolveContact() จับคู่ลิงก์ตามชนิด · เบอร์ → tel:+66… (แปลง 0 นำหน้า, ตัดขีด/ช่องว่าง/วงเล็บ) · อีเมล → mailto: · LINE @id → line.me · เว็บ → https + ตัด fbclid/_rdc/utm_* - ContactPanel: ไอคอนตามชนิด (phone/mail/fb/ig/line/ticket/globe), tel/mailto เปิดแท็บเดิม, เว็บเปิดแท็บใหม่; แถวที่ไม่มีลิงก์แสดงเป็นข้อความ (ไม่หลอกให้กด) - sanitizeCinemas: คำนวณ href ใหม่ทุกครั้งที่บันทึก → ข้อมูลเก่าไม่กลับมาเพี้ยน - ContactsEditor: auto-format ตอนออกจากช่อง + พรีวิว "✓ กดโทรได้ → tel:+66…" - scripts/audit-contacts.mjs [--fix]: ตรวจ/ซ่อมทั้งไฟล์ (แก้ไป 13 จุด, สำรองข้อมูลแล้ว) - contact-link.spec.ts 13 tests · docs: admin-guide/api/DESIGN
This commit is contained in:
1 parent
2c5918a614
commit
42572020d8
9 files changed
+656
-26
No files matched your search
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* ทดสอบการจับคู่ลิงก์ของช่องทางติดต่อ — เคสที่พังจริงบนหน้าเว็บ
|
||||
* (เบอร์กดไม่ได้, facebook ติด fbclid, `www.…` ไม่มี scheme, LINE ID)
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
contactHref,
|
||||
looksLikePhone,
|
||||
normalizePhone,
|
||||
resolveContact,
|
||||
} from "@/lib/contact-link";
|
||||
|
||||
describe("normalizePhone", () => {
|
||||
it("แปลงเบอร์ไทย 0 นำหน้าเป็น +66 และตัดขีด/ช่องว่าง", () => {
|
||||
expect(normalizePhone("098-869-0997")).toBe("+66988690997");
|
||||
expect(normalizePhone("098 869 0997")).toBe("+66988690997");
|
||||
expect(normalizePhone("(02) 123-4567")).toBe("+6621234567");
|
||||
});
|
||||
|
||||
it("รองรับที่ขึ้นต้น 66 หรือ +66 อยู่แล้ว", () => {
|
||||
expect(normalizePhone("66812345678")).toBe("+66812345678");
|
||||
expect(normalizePhone("+66 81 234 5678")).toBe("+66812345678");
|
||||
});
|
||||
|
||||
it("ไม่ใช่เบอร์ → null", () => {
|
||||
expect(normalizePhone("12")).toBeNull();
|
||||
expect(normalizePhone("housesamyan")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("looksLikePhone", () => {
|
||||
it("แยกเบอร์ออกจากข้อความ", () => {
|
||||
expect(looksLikePhone("098-869-0997")).toBe(true);
|
||||
expect(looksLikePhone("www.housesamyan.com")).toBe(false);
|
||||
expect(looksLikePhone("House Samyan")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveContact", () => {
|
||||
it("Call + เบอร์ → tel: (กดโทรได้จริง)", () => {
|
||||
const resolved = resolveContact({
|
||||
label: "Call",
|
||||
value: "098-869-0997",
|
||||
href: null,
|
||||
});
|
||||
expect(resolved.href).toBe("tel:+66988690997");
|
||||
expect(resolved.kind).toBe("tel");
|
||||
expect(resolved.external).toBe(false);
|
||||
});
|
||||
|
||||
it("เบอร์ที่ href ผิดรูปแบบ → แก้ให้ถูก", () => {
|
||||
expect(
|
||||
contactHref({ label: "Call", value: "098-869-0997", href: "0988690997" }),
|
||||
).toBe("tel:+66988690997");
|
||||
expect(
|
||||
contactHref({
|
||||
label: "Call",
|
||||
value: "098-869-0997",
|
||||
href: "tel:0988690997",
|
||||
}),
|
||||
).toBe("tel:+66988690997");
|
||||
});
|
||||
|
||||
it("Facebook → https + ตัด fbclid/_rdc + เปลี่ยน web. เป็น www.", () => {
|
||||
const resolved = resolveContact({
|
||||
label: "Facebook",
|
||||
value: "Century Sukhumvit",
|
||||
href: "https://web.facebook.com/CenturySukhumvit/?_rdc=1&_rdr",
|
||||
});
|
||||
expect(resolved.kind).toBe("web");
|
||||
expect(resolved.external).toBe(true);
|
||||
expect(resolved.href).toBe("https://www.facebook.com/CenturySukhumvit/");
|
||||
});
|
||||
|
||||
it("เว็บที่ไม่มี scheme → เติม https", () => {
|
||||
expect(
|
||||
contactHref({
|
||||
label: "Website",
|
||||
value: "www.housesamyan.com",
|
||||
href: null,
|
||||
}),
|
||||
).toBe("https://www.housesamyan.com/");
|
||||
});
|
||||
|
||||
it("Instagram ที่ติด fbclid → ตัดออก", () => {
|
||||
const href = contactHref({
|
||||
label: "Instagram",
|
||||
value: "centurysukhumvit",
|
||||
href: "https://www.instagram.com/centurysukhumvit?fbclid=abc123",
|
||||
});
|
||||
expect(href).toBe("https://www.instagram.com/centurysukhumvit");
|
||||
});
|
||||
|
||||
it("อีเมล → mailto:", () => {
|
||||
expect(
|
||||
resolveContact({ label: "อีเมล", value: "[email protected]" }).href,
|
||||
).toBe("mailto:[email protected]");
|
||||
});
|
||||
|
||||
it("LINE ID (@id) → line.me", () => {
|
||||
expect(resolveContact({ label: "Line", value: "@alterflix" }).href).toBe(
|
||||
"https://line.me/R/ti/p/~alterflix",
|
||||
);
|
||||
});
|
||||
|
||||
it("ค่าไม่มีข้อมูล/ว่าง → null (ไม่สร้างลิงก์หลอก)", () => {
|
||||
expect(resolveContact({ label: "Call", value: "-" }).href).toBeNull();
|
||||
expect(resolveContact({ label: "Call", value: "" }).href).toBeNull();
|
||||
expect(
|
||||
resolveContact({ label: "Website", value: "ไม่มีข้อมูล" }).href,
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("ค่าแปลก ๆ ที่ไม่ใช่ทั้งเบอร์และโดเมน → null", () => {
|
||||
expect(
|
||||
resolveContact({ label: "Call", value: "โทรสอบถาม" }).href,
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* แปลง "ช่องทางติดต่อ" ของโรงหนังให้เป็นลิงก์ที่กดใช้ได้จริง
|
||||
*
|
||||
* ปัญหาเดิม: ข้อมูลเก็บ `href` ดิบจากหลังบ้าน (บางทีว่าง, บางทีเป็น `www.…` ไม่มี scheme,
|
||||
* เบอร์โทรไม่ขึ้นต้นด้วย `tel:`) → หน้าเว็บได้ `<a>` ที่กดแล้วไม่เกิดอะไร
|
||||
*
|
||||
* กติกาที่นี่ (ใช้ทั้งตอนบันทึกในหลังบ้านและตอนแสดงผล):
|
||||
* - เบอร์โทร → `tel:+66…` (แปลง 0 นำหน้าเป็น +66, ตัดขีด/วงเล็บ/ช่องว่าง)
|
||||
* - อีเมล → `mailto:`
|
||||
* - LINE ID (`@abc`) → `https://line.me/R/ti/p/~abc`
|
||||
* - เว็บ/โซเชียล → บังคับ `https://`, แปลง `web.facebook.com` → `www.facebook.com`
|
||||
* และตัดพารามิเตอร์ติดตาม (fbclid, _rdc, utm_*, igshid)
|
||||
*/
|
||||
|
||||
export type ContactKind = "tel" | "mailto" | "web";
|
||||
|
||||
export type ResolvedContact = {
|
||||
/** href พร้อมใช้ — null = ไม่มีข้อมูลที่ใช้เป็นลิงก์ได้ */
|
||||
href: string | null;
|
||||
kind: ContactKind | null;
|
||||
/** true = ลิงก์ออกเว็บนอก (เปิดแท็บใหม่) */
|
||||
external: boolean;
|
||||
};
|
||||
|
||||
const EMPTY_VALUES = new Set([
|
||||
"-",
|
||||
"–",
|
||||
"—",
|
||||
"n/a",
|
||||
"na",
|
||||
"null",
|
||||
"undefined",
|
||||
"ไม่มี",
|
||||
"ไม่มีข้อมูล",
|
||||
]);
|
||||
|
||||
const TRACKING_PARAMS = [
|
||||
"fbclid",
|
||||
"gclid",
|
||||
"_rdc",
|
||||
"_rdr",
|
||||
"igshid",
|
||||
"ref",
|
||||
"ref_src",
|
||||
"mibextid",
|
||||
];
|
||||
|
||||
function isTrackingParam(name: string): boolean {
|
||||
const key = name.toLowerCase();
|
||||
return TRACKING_PARAMS.includes(key) || key.startsWith("utm_");
|
||||
}
|
||||
|
||||
/** แปลงเบอร์เป็นรูปแบบที่กดโทรได้: `+66812345678` (คืน null ถ้าไม่ใช่เบอร์ที่ใช้ได้) */
|
||||
export function normalizePhone(raw: string): string | null {
|
||||
const trimmed = raw.trim();
|
||||
if (trimmed === "") return null;
|
||||
|
||||
const hasPlus = trimmed.startsWith("+");
|
||||
const digits = trimmed.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}`;
|
||||
}
|
||||
|
||||
/** ดูว่าข้อความนี้ "หน้าตาเป็นเบอร์โทร" ไหม (ใช้ตัดสินก่อนถือว่าเป็นเว็บ) */
|
||||
export function looksLikePhone(raw: string): boolean {
|
||||
const trimmed = raw.trim();
|
||||
if (trimmed === "") return false;
|
||||
if (/[a-z@]/i.test(trimmed)) return false;
|
||||
const digits = trimmed.replace(/\D/g, "");
|
||||
return digits.length >= 8 && digits.length <= 15;
|
||||
}
|
||||
|
||||
function stripTracking(url: URL): URL {
|
||||
const keep = new URLSearchParams();
|
||||
url.searchParams.forEach((value, key) => {
|
||||
if (!isTrackingParam(key)) keep.append(key, value);
|
||||
});
|
||||
const query = keep.toString();
|
||||
url.search = query === "" ? "" : `?${query}`;
|
||||
return url;
|
||||
}
|
||||
|
||||
function normalizeWebUrl(raw: string): string | null {
|
||||
let candidate = raw.trim();
|
||||
if (candidate === "") return null;
|
||||
|
||||
// LINE ID แบบ @id
|
||||
if (/^@[\w.\-_]+$/.test(candidate)) {
|
||||
return `https://line.me/R/ti/p/~${candidate.slice(1)}`;
|
||||
}
|
||||
if (/^line:/i.test(candidate)) {
|
||||
candidate = candidate.replace(/^line:\/\//i, "https://line.me/R/ti/p/~");
|
||||
}
|
||||
|
||||
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(candidate)) {
|
||||
// `www.x.com` / `x.com/path` / `x.com` → https
|
||||
if (!/^[\w-]+(\.[\w-]+)+/.test(candidate)) return null;
|
||||
candidate = `https://${candidate}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(candidate);
|
||||
if (url.protocol === "http:") url.protocol = "https:";
|
||||
if (url.hostname === "web.facebook.com") url.hostname = "www.facebook.com";
|
||||
url.hostname = url.hostname.toLowerCase();
|
||||
return stripTracking(url).toString();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function looksLikeEmail(raw: string): boolean {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(raw.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* แปลงช่องทางติดต่อหนึ่งรายการ → ลิงก์ที่ใช้ได้
|
||||
* ลำดับความเชื่อ: `href` ที่ผู้ใช้กรอก → ลักษณะของ `value` → label
|
||||
*/
|
||||
export function resolveContact(channel: {
|
||||
label?: string;
|
||||
value?: string;
|
||||
href?: string | null;
|
||||
}): ResolvedContact {
|
||||
const value = (channel.value ?? "").trim();
|
||||
const href = (channel.href ?? "").trim();
|
||||
const label = (channel.label ?? "").trim().toLowerCase();
|
||||
|
||||
const candidates = [href, value].filter(
|
||||
(item) => item !== "" && !EMPTY_VALUES.has(item.toLowerCase()),
|
||||
);
|
||||
if (candidates.length === 0)
|
||||
return { href: null, kind: null, external: false };
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const lower = candidate.toLowerCase();
|
||||
|
||||
// 1) เบอร์โทรที่ระบุชัด
|
||||
if (lower.startsWith("tel:")) {
|
||||
const phone = normalizePhone(candidate.slice(4));
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel", external: false };
|
||||
}
|
||||
|
||||
// 2) อีเมล
|
||||
if (lower.startsWith("mailto:")) {
|
||||
const address = candidate.slice(7).trim();
|
||||
if (looksLikeEmail(address)) {
|
||||
return { href: `mailto:${address}`, kind: "mailto", external: false };
|
||||
}
|
||||
}
|
||||
if (looksLikeEmail(candidate)) {
|
||||
return { href: `mailto:${candidate}`, kind: "mailto", external: false };
|
||||
}
|
||||
|
||||
// 3) ลิงก์เว็บ/โซเชียล (ตัดสินจาก scheme หรือรูปทรงโดเมน)
|
||||
if (
|
||||
/^https?:\/\//i.test(candidate) ||
|
||||
/^line:/i.test(candidate) ||
|
||||
/^@[\w.\-_]+$/.test(candidate) ||
|
||||
/^[\w-]+(\.[\w-]+)+(\/|$)/.test(candidate)
|
||||
) {
|
||||
const url = normalizeWebUrl(candidate);
|
||||
if (url) return { href: url, kind: "web", external: true };
|
||||
}
|
||||
|
||||
// 4) เบอร์โทรที่ไม่ได้ใส่ tel:
|
||||
if (
|
||||
looksLikePhone(candidate) &&
|
||||
/call|โทร|phone|tel|เบอร์|มือถือ/.test(label + " " + value)
|
||||
) {
|
||||
const phone = normalizePhone(candidate);
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel", external: false };
|
||||
}
|
||||
if (looksLikePhone(candidate)) {
|
||||
const phone = normalizePhone(candidate);
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel", external: false };
|
||||
}
|
||||
}
|
||||
|
||||
return { href: null, kind: null, external: false };
|
||||
}
|
||||
|
||||
/** ค่าที่ควรบันทึกกลับลง `href` (ใช้ตอน sanitize/หลังบ้าน) */
|
||||
export function contactHref(channel: {
|
||||
label?: string;
|
||||
value?: string;
|
||||
href?: string | null;
|
||||
}): string | null {
|
||||
return resolveContact(channel).href;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import path from "node:path";
|
||||
|
||||
import { sanitizeArticleBlocks } from "@/lib/article";
|
||||
import type { Cinema } from "@/lib/cinemas";
|
||||
import { contactHref } from "@/lib/contact-link";
|
||||
import {
|
||||
seedAbout,
|
||||
seedContent,
|
||||
@@ -126,10 +127,14 @@ export function sanitizeCinemas(input: unknown): Cinema[] {
|
||||
contacts: Array.isArray(raw.contacts)
|
||||
? raw.contacts.map((contact) => {
|
||||
const entry = (contact ?? {}) as Record<string, unknown>;
|
||||
const label = asString(entry.label);
|
||||
const value = asString(entry.value);
|
||||
const href = typeof entry.href === "string" ? entry.href : null;
|
||||
// บังคับ href ให้เป็นลิงก์ที่กดใช้ได้จริง (tel:/mailto:/https:) ทุกครั้งที่บันทึก
|
||||
return {
|
||||
label: asString(entry.label),
|
||||
value: asString(entry.value),
|
||||
href: typeof entry.href === "string" ? entry.href : null,
|
||||
label,
|
||||
value,
|
||||
href: contactHref({ label, value, href }),
|
||||
};
|
||||
})
|
||||
: [],
|
||||
|
||||
Reference in new issue
Block a user