/** * Layar Kosong โ€” JSON-LD Schema Injector V5.6 * * Runtime: Bun only * - No Node.js imports (node:fs, node:path, process, etc.). * - Uses Bun.file(), Bun.write(), Bun.argv and Bun's native HTMLRewriter. * - Safe to run repeatedly: a source fingerprint prevents unnecessary rewrites, * while changed HTML automatically triggers a fresh schema build. * * Run: * bun --check inject-schema.ts * bun run inject-schema.ts * bun run inject-schema.ts --force * bun run inject-schema.ts --audit * * V5.6 adds multi-platform video detection, non-destructive JSON-LD ownership, * graph validation across preserved schema scripts, read-only audit mode, * BreadcrumbList graph node linked from WebPage, and clean BlogPosting without keywords. */ type ArticleEntry = [string, string, string, string, string?]; type ArtikelData = Record; type SchemaNode = Record; interface PrimaryImageMeta { url: string; contentUrl: string; width?: number; height?: number; caption: string; encodingFormat?: string; } interface ExtractedMeta { articleUrl: string; categoryUrl: string; headline: string; pageTitle: string; description: string; siteName: string; authorName: string; language: string; datePublished: string | null; dateModified: string | null; licenseUrl: string; articleSection: string; primaryImage: PrimaryImageMeta | null; articleAuthorProfile: string; articlePublisherProfile: string; twitterCreatorProfile: string; twitterSiteProfile: string; fediverseProfile: string; } interface ImageSnapshot { src: string; width: string; height: string; alt: string; } interface ParsedDocument { meta: ExtractedMeta; wordCount: number; } // ----------------------------------------------------------------------------- // Configuration // ----------------------------------------------------------------------------- const BASE_URL = "https://dalam.web.id"; const SITE_NAME = "Layar Kosong"; const SITE_DESCRIPTION = "Catatan digital tentang teknologi, sejarah, sosial, budaya, dan kehidupan."; const AUTHOR = "Fakhrul Rijal"; const AUTHOR_URL = `${BASE_URL}/about`; const AUTHOR_IMAGE_URL = `${BASE_URL}/img/profpic.webp`; const AUTHOR_JOB_TITLE = "Web Developer & Content Creator"; const BLOG_DESCRIPTION = "Membaca Peran Teknologi Komunitas dan Kehidupan Masa Kini."; const EDITORIAL_CONTACT_URL = "https://t.me/frijal"; const LANGUAGE = "id-ID"; const RELEASE_VERSION = "5.6"; const SCHEMA_ARTICLE_TYPE = "BlogPosting"; const LOGO_URL = `${BASE_URL}/favicon.svg`; const LOGO_WIDTH = 1024; const LOGO_HEIGHT = 1024; const FORCE_RESCHEMA = Bun.argv.includes("--force"); const AUDIT_ONLY = Bun.argv.includes("--audit"); // Bump this whenever the schema structure or extraction logic changes. const SIGNATURE_VERSION = 11; const SIGNATURE_TEXT_PREFIX = "schema_oleh_Fakhrul_Rijal"; const ALLOWED_CATEGORIES = new Set([ "gaya-hidup", "jejak-sejarah", "lainnya", "olah-media", "opini-sosial", "sistem-terbuka", "warta-tekno", ]); const IGNORED_WORDCOUNT_SELECTOR = [ "head", "script", "style", "noscript", "svg", "canvas", "iframe", "template", "footer", "header", "nav", "aside", "#related-articles-grid", "#response", "#progress", "#internal-nav", "#related-marquee-section", "#related-marquee-container", ".search-floating-container", "#floatingSearchResults", "#layar-kosong-header", ].join(", "); const PERSON_SAME_AS = [ "https://codeberg.org/frijal", "https://github.com/frijal", "https://facebook.com/frijal", "https://linkedin.com/in/frijal", "https://www.instagram.com/frijal", "https://x.com/responaja", "https://threads.net/frijal", "https://mastodon.social/@frijal", "https://www.youtube.com/@frijal", "https://www.reddit.com/user/Fearless_Economics69", "https://tiktok.com/@gibah.dilarang", "https://tumblr.com/frijal", "https://t.me/frijal", "https://gravatar.com/pelankiri", ]; const ORG_SAME_AS = [ "https://facebook.com/frijalpage", "https://x.com/responaja", "https://www.youtube.com/@frijal", ]; type VideoPlatform = "youtube" | "vimeo" | "dailymotion" | "facebook" | "tiktok" | "html5"; interface DetectedVideo { platform: VideoPlatform; canonicalUrl: string; embedUrl?: string; contentUrl?: string; platformId?: string; thumbnailUrl?: string; sourceAttribute: string; } const cleanBaseUrl = BASE_URL.replace(/\/+$/, ""); const siteUrl = cleanBaseUrl; const searchUrlTemplate = `${cleanBaseUrl}/search/?q={search_term_string}`; // ----------------------------------------------------------------------------- // General utilities // ----------------------------------------------------------------------------- function slugify(value: unknown): string { return String(value ?? "") .toLowerCase() .trim() .replace(/^[^\p{L}\p{N}\s]*/u, "") .replace(/ & /g, "-and-") .replace(/[^\p{L}\p{N}\s-]/gu, "") .replace(/\s+/g, "-") .replace(/-+/g, "-") .replace(/^-|-$/g, ""); } function categoryNameClean(category: string): string { return String(category) .trim() .replace(/^[^\p{L}\p{N}\s]*/u, "") .replace(/-+/g, " ") .replace(/\b\p{L}/gu, (character) => character.toUpperCase()); } function decodeHtmlEntities(value: string): string { return value .replace(/\x26nbsp;/gi, " ") .replace(/\x26amp;/gi, "&") .replace(/\x26lt;/gi, "<") .replace(/\x26gt;/gi, ">") .replace(/\x26quot;/gi, '"') .replace(/\x26#39;|\x26apos;/gi, "'") .replace(/\x26#(\d+);/g, (match, decimal: string) => { const codePoint = Number(decimal); return Number.isInteger(codePoint) && codePoint >= 0 && codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : match; }) .replace(/\x26#x([0-9a-f]+);/gi, (match, hexadecimal: string) => { const codePoint = Number.parseInt(hexadecimal, 16); return Number.isInteger(codePoint) && codePoint >= 0 && codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : match; }); } function cleanText(value: unknown): string { return decodeHtmlEntities(String(value ?? "")).replace(/\s+/g, " ").trim(); } function normalizeDescription(value: string): string { return cleanText(value).slice(0, 300); } function normalizeLocale(value: string): string { const locale = cleanText(value); return locale ? locale.replace(/_/g, "-") : LANGUAGE; } /** * Resolve only HTTP(S) URLs. Relative and protocol-relative URLs are resolved * against the canonical site origin; javascript:, data:, and malformed URLs * are rejected. */ function absoluteUrl( value: string | undefined | null, fallback = "", ): string { const raw = cleanText(value); if (!raw) return fallback; try { const parsed = new URL(raw, `${cleanBaseUrl}/`); if (parsed.protocol !== "http:" && parsed.protocol !== "https:") { return fallback; } return parsed.toString(); } catch { return fallback; } } function toPositiveInt(value: unknown): number | undefined { const number = Number.parseInt(String(value ?? ""), 10); return Number.isFinite(number) && number > 0 ? number : undefined; } function uniqueClean(items: string[]): string[] { const seen = new Set(); const result: string[] = []; for (const item of items) { const value = cleanText(item); if (!value) continue; const key = value.toLocaleLowerCase("id-ID"); if (seen.has(key)) continue; seen.add(key); result.push(value); } return result; } function safeJsonLd(data: unknown): string { return JSON.stringify(data) .replace(//g, "\\u003E") .replace(/&/g, "\\u0026"); } /** Join repository-relative paths without importing node:path. */ function joinRelativePath(...segments: string[]): string { const parts: string[] = []; for (const segment of segments) { const normalized = String(segment).replace(/\\/g, "/"); for (const part of normalized.split("/")) { if (!part || part === ".") continue; if (part === "..") { throw new Error(`Path traversal tidak diizinkan: ${segment}`); } parts.push(part); } } return parts.join("/"); } /** * ISO timestamp validator for Article dates. * A date-only value or a timestamp without an explicit timezone is rejected; * the injector will not invent a publication time or timezone. */ function normalizeIsoTimestamp(value: string): string | null { const raw = cleanText(value); if (!raw) return null; const hasExplicitTimezone = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/i.test(raw); if (!hasExplicitTimezone) return null; const date = new Date(raw); if (!Number.isFinite(date.getTime())) return null; return date.toISOString(); } function hashText(value: string): string { // Stable FNV-1a fingerprint; used only to detect whether source HTML changed. let hash = 0x811c9dc5; for (let index = 0; index < value.length; index++) { hash ^= value.charCodeAt(index); hash = Math.imul(hash, 0x01000193); } return (hash >>> 0).toString(16).padStart(8, "0"); } // ----------------------------------------------------------------------------- // Signature and existing JSON-LD cleanup // ----------------------------------------------------------------------------- function stripSchemaSignature(html: string): string { // Signature aktif dan signature lama berbasis noscript. // Fingerprint ikut dibuang agar tidak mengganggu proses regenerasi. const noscriptMarker = /