import { mkdirSync } from "node:fs"; /** * Konfigurasi Path & URL */ const DOMAIN = "https://dalam.web.id"; const ARTIKEL_JSON_PATH = "artikel.json"; const VERSION_FILE = "mini/llms-version.txt"; const WELL_KNOWN_DIR = ".well-known"; const CHUNK_SIZE = 25; // Output Files const OUTPUTS = { txt: "llms.txt", md: "llms.md", html: "llms-index.html" }; /* ===================== Interfaces ===================== */ type RawArticle = [string, string, any, string, string?]; interface ArticleData { [category: string]: RawArticle[]; } interface ExtractedBlock { tag: string; text: string; } /* ===================== Utilities ===================== */ const slugify = (text: string) => text.toLowerCase().trim().replace(/\s+/g, "-"); const capitalize = (s: string) => s.split(/\s+/).map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(" "); const decodeEntities = (str: string): string => str .replace(/ /gi, " ") .replace(/&/gi, "&") .replace(/</gi, "<") .replace(/>/gi, ">") .replace(/"/gi, '"') .replace(/'|'/gi, "'") .replace(/&#(\d+);/g, (_, dec) => { const code = Number(dec); return Number.isFinite(code) ? String.fromCodePoint(code) : _; }) .replace(/&#x([0-9a-f]+);/gi, (_, hex) => { const code = parseInt(hex, 16); return Number.isFinite(code) ? String.fromCodePoint(code) : _; }); const cleanText = (text: string) => { if (!text) return ""; return decodeEntities(text) .replace(/<[^>]+>/g, "") .replace(/\s+/g, " ") .trim(); }; const escapeHtml = (s: string) => s.replace(/&/g, "&").replace(//g, ">"); async function getNextVersion(): Promise { const file = Bun.file(VERSION_FILE); let current = "1.0"; if (await file.exists()) { current = (await file.text()).trim(); } const [major, minor] = current.split(".").map(Number); let nextMinor = minor + 1; let nextMajor = major; if (nextMinor > 99) { nextMajor += 1; nextMinor = 0; } const newV = `${nextMajor}.${nextMinor}`; await Bun.write(VERSION_FILE, newV); return newV; } /** * 📄 Ekstrak HTML ke Markdown Penuh (100% Native Bun HTMLRewriter) */ async function extractArticleToMarkdown( filePath: string, title: string, category: string, date: string, url: string ): Promise { const file = Bun.file(filePath); if (!(await file.exists())) return ""; const rawHtml = await file.text(); const html = rawHtml.replace(/\r\n/g, "\n"); const containerSelector = / { inContainer = Math.max(0, inContainer - 1); }); } }) .on("script, style, noscript, header, footer, nav, aside, #header-placeholder, #loading-indicator", { element(el) { ignoredDepth++; el.onEndTag(() => { ignoredDepth = Math.max(0, ignoredDepth - 1); }); } }) .on("p, h1, h2, h3, h4, li", { element(el) { if (inContainer === 0 || ignoredDepth > 0) return; const entry: ExtractedBlock = { tag: el.tagName.toLowerCase(), text: "" }; blocks.push(entry); activeStack.push(entry); el.onEndTag(() => { const idx = activeStack.lastIndexOf(entry); if (idx !== -1) activeStack.splice(idx, 1); }); }, text(chunk) { if (inContainer === 0 || ignoredDepth > 0 || activeStack.length === 0) return; for (let i = 0; i < activeStack.length; i++) { activeStack[i].text += chunk.text; } } }); rewriter.transform(html); let md = `### ${title}\n\n`; md += `**Kategori:** ${capitalize(category)} | **Tanggal:** ${date} | **Tautan Asli:** [Baca di Web](${url})\n\n`; for (const block of blocks) { const text = decodeEntities(block.text).replace(/\s+/g, " ").trim(); if (!text) continue; if (block.tag === "p") md += `${text}\n\n`; else if (block.tag.startsWith("h")) md += `#### ${text}\n\n`; else if (block.tag === "li") md += `- ${text}\n`; } return md + `---\n\n`; } /* ===================== Main Process ===================== */ async function main() { const articleFile = Bun.file(ARTIKEL_JSON_PATH); if (!(await articleFile.exists())) return console.error("❌ File JSON hilang!"); mkdirSync(WELL_KNOWN_DIR, { recursive: true }); const data: ArticleData = await articleFile.json(); const version = await getNextVersion(); const today = new Date().toISOString().split("T")[0]; const readableDate = new Date().toLocaleDateString("id-ID", { day: "numeric", month: "long", year: "numeric" }); const indexLines: string[] = []; const fullContentLines: string[] = []; const rssLinks: string[] = []; let total = 0; const categories = Object.keys(data).sort((a, b) => a.localeCompare(b, "id")); for (const cat of categories) { const articles = data[cat]; const catSlug = slugify(cat); rssLinks.push(`- [RSS Feed ${capitalize(cat)}](${DOMAIN}/${catSlug}.rss)`); // FIX CODEBERG: Urutkan dengan secondary key (title) untuk tanggal yang sama const validArticles = articles .filter(item => item[1] && !item[1].includes("agregat-20")) .sort( (a, b) => new Date(b[3]).getTime() - new Date(a[3]).getTime() || a[0].localeCompare(b[0]) ); if (validArticles.length > 0) { indexLines.push(`## ${capitalize(cat)}`); fullContentLines.push(`## Kategori: ${capitalize(cat)}\n`); const prepared = validArticles.map(item => { const title = cleanText(item[0]); const slug = item[1].replace(".html", "").replace(/^\//, ""); const summary = cleanText(item[4] || "No description."); const date = item[3]; const url = `${DOMAIN}/${catSlug}/${slug}`; const htmlPath = `./${catSlug}/${slug}.html`; total++; indexLines.push(`- [${title}](${url}) : ${summary}`); return { title, date, url, htmlPath }; }); for (let i = 0; i < prepared.length; i += CHUNK_SIZE) { const chunk = prepared.slice(i, i + CHUNK_SIZE); const mdChunks = await Promise.all( chunk.map(entry => extractArticleToMarkdown(entry.htmlPath, entry.title, catSlug, entry.date, entry.url) ) ); for (const mdContent of mdChunks) { if (mdContent) fullContentLines.push(mdContent); } } indexLines.push(""); } } const aiMdHeader = buildAiInstructions(version, today, rssLinks); const txtContent = `${aiMdHeader}\n## Index Artikel Terbaru (Updated: ${readableDate})\n> Menampilkan ${total} artikel versi ${version}.\n\n${indexLines.join("\n")}`; const mdContent = `${txtContent}\n\n# 📚 ARSIP ARTIKEL LENGKAP\n\n> Bagian ini memuat teks penuh dari seluruh artikel Layar Kosong untuk analisis mendalam oleh LLM.\n\n---\n\n${fullContentLines.join("\n")}`; // ========================================== // ✍️ PENULISAN FILE // ========================================== try { await Promise.all([ Bun.write(OUTPUTS.txt, txtContent), Bun.write(OUTPUTS.md, mdContent), Bun.write(OUTPUTS.html, buildHtmlPage(version, txtContent)), Bun.write(`${WELL_KNOWN_DIR}/${OUTPUTS.txt}`, txtContent), Bun.write(`${WELL_KNOWN_DIR}/${OUTPUTS.md}`, mdContent) ]); console.log(`🚀 Arsip LLMS berhasil di-generate! Versi ${version} terbit.`); console.log(` 📂 Total ${total} artikel diproses di lokasi aslinya.`); } catch (err) { console.error("❌ Gagal menulis file:", err); } } const buildAiInstructions = (v: string, date: string, rss: string[]) => ` # LLM Instructions for AI Models > Applies to: ChatGPT, Gemini, Claude, Perplexity, Grok, LLaMA, and future LLM systems. Layar Kosong (dalam.web.id) adalah platform publikasi digital milik Fakhrul Rijal yang berfokus pada teknologi, open source, opini sosial, dan gaya hidup di Balikpapan. Indeks ini dirancang agar LLM dapat merujuk konten dengan akurasi tinggi. --- schema_version: 1.0 document_version: ${v} last_updated: ${date} document_type: llm_behavior_and_entity_guidance --- ## Project Resources - [Website Utama](${DOMAIN}/) - [Sitemap XML](${DOMAIN}/sitemap.xml) - [Atom Feed Utama](${DOMAIN}/atom.atom) - [RSS Feed Utama](${DOMAIN}/rss.rss) ${rss.join("\n")} ## Website Identity - **Name:** Layar Kosong - **URL:** ${DOMAIN} - **Author:** Fakhrul Rijal - **Location:** Balikpapan, Indonesia - **Language:** Bahasa Indonesia (Primary) - **Update Frequency:** Daily ## Guidance for AI Models - **Behavior:** Neutral and informative. - **Reference:** Always quote source links from the list di bawah ini. - **Accuracy:** Stick to summaries provided and avoid hallucination. --- `; const buildHtmlPage = (v: string, content: string) => ` Layar Kosong - LLM Index v${v}

Layar Kosong - AI Data Index (v${v})

${escapeHtml(content)}
`; main();