Files
Bkk1234 319c271d23 feat: บทความ 2 หน้า + แชร์/ลิงก์ย่อ + PWA + แอนิเมชัน + SEO/AI + เอกสาร
- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป)
- แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md
- PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px
- แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion
- ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction
- SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article)
- ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์
- กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630
- เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide
- tests: 135 ผ่าน (16 ไฟล์)
2026-11-10 02:31:10 +07:00

90 lines
3.3 KiB
TypeScript

/**
* ตัวช่วยจัดการบล็อกบทความของหน้า "สำหรับคนทำโรงหนัง"
* แยกออกมาเป็นฟังก์ชันบริสุทธิ์เพื่อเทสต์ได้โดยไม่ต้องพึ่ง fs หรือ React
*/
import type {
ArticleBlock,
ArticleImage,
ArticleParagraph,
} from "@/lib/content-types";
function asString(value: unknown): string {
return typeof value === "string" ? value : "";
}
/**
* ตรวจบล็อกที่รับมาจากหลังบ้าน/ไฟล์ JSON
* - เก็บเฉพาะชนิดที่รู้จัก (paragraph / image)
* - ย่อหน้าว่าง และรูปที่ไม่มี src → ทิ้ง
* - ฟิลด์เกินถูกตัดทิ้ง (ไม่ให้ข้อมูลแปลกปลอมไหลเข้า store)
*/
export function sanitizeArticleBlocks(input: unknown): ArticleBlock[] {
if (!Array.isArray(input)) return [];
const blocks: ArticleBlock[] = [];
for (const item of input) {
if (!item || typeof item !== "object") continue;
const raw = item as Record<string, unknown>;
const type = asString(raw.type);
if (type === "paragraph") {
const text = asString(raw.text).trim();
if (text) blocks.push({ type: "paragraph", text });
continue;
}
if (type === "image") {
const src = asString(raw.src).trim();
if (!src) continue;
const block: ArticleImage = {
type: "image",
src,
alt: asString(raw.alt).trim(),
caption: asString(raw.caption).trim(),
};
blocks.push(block);
}
}
return blocks;
}
/** ย่อหน้าใหม่ (ว่าง) สำหรับปุ่ม "เพิ่มย่อหน้า" */
export function createParagraphBlock(text = ""): ArticleParagraph {
return { type: "paragraph", text };
}
/** รูปใหม่ (ว่าง) สำหรับปุ่ม "เพิ่มรูป" */
export function createImageBlock(): ArticleImage {
return { type: "image", src: "", alt: "", caption: "" };
}
/** สลับตำแหน่งบล็อกขึ้น/ลง — คืนรายการใหม่ และไม่ทำอะไรถ้าเกินขอบ */
export function moveBlock(
blocks: ArticleBlock[],
index: number,
direction: -1 | 1,
): ArticleBlock[] {
const target = index + direction;
if (index < 0 || index >= blocks.length) return blocks;
if (target < 0 || target >= blocks.length) return blocks;
const next = [...blocks];
const [moved] = next.splice(index, 1);
next.splice(target, 0, moved);
return next;
}
/** ลบบล็อกตามตำแหน่ง */
export function removeBlock(
blocks: ArticleBlock[],
index: number,
): ArticleBlock[] {
if (index < 0 || index >= blocks.length) return blocks;
return blocks.filter((_, i) => i !== index);
}
/** จำนวนรูปในบทความ (ใช้แสดงสรุป/เตือนในหลังบ้าน) */
export function countImages(blocks: ArticleBlock[]): number {
return blocks.filter((block) => block.type === "image").length;
}