import { readdir } from "node:fs/promises"; import { YAML } from "bun"; import metadata from "./metadata.json"; import templateRaw from "./templates/post.html" with { type: "text" }; const templateStr = templateRaw as unknown as string; const template = templateStr .replaceAll("[[SITE]]", metadata.title) .replaceAll("[[NAME]]", metadata.name) .replaceAll("[[TIMEZONE]]", metadata.timezone) .replaceAll("[[YEAR]]", new Date().getFullYear().toString()); const posts = await readdir("./posts"); const defaultFrontmatter = { title: "Untitled", desc: "", date: "0000-00-00", // YYYY-MM-DD tags: [] as never[], draft: true, }; function fetchFrontmatter(raw?: string) { try { if (!raw) throw new Error("No frontmatter found."); const parsed = { ...defaultFrontmatter, ...(YAML.parse(raw) as typeof defaultFrontmatter) }; return { ...parsed, date: new Date(parsed.date) }; } catch { console.warn("Failed to parse frontmatter. Using default values."); return { ...defaultFrontmatter, date: new Date(NaN) }; } } posts.forEach(async (post) => { console.log(`Processing ${post}...`); const raw = (await Bun.file(`./posts/${post}`).text()).split("^---"); const content = raw.at(-1) || "No content"; const frontmatter = fetchFrontmatter(raw.at(0)); const html = Bun.markdown.html(content, { autolinks: true, headings: true, underline: true, latexMath: true, tagFilter: true, }); const render = template .replaceAll("[[TITLE]]", `${metadata.title} - ${frontmatter.title}`) .replaceAll("[[POST_TITLE]]", frontmatter.title) .replaceAll("[[DATE]]", frontmatter.date.toDateString()) .replaceAll("[[REVISIONS]]", `https://git.satr14.my.id/satr14/ssg.md/commits/branch/main/posts/${post}`) .replaceAll("[[DESC]]", frontmatter.desc) .replace("[[CONTENT]]", html); await Bun.write(`./dist/posts/${post.replace(".md", ".html")}`, render); });