base for output and styling

This commit is contained in:
Satria 2026-05-25 18:44:42 +07:00
commit bc21240e9f
7 changed files with 366 additions and 1 deletions

View file

@ -1 +1,60 @@
console.log("Hello via Bun!");
import { readdir } from "node:fs/promises";
import { YAML } from "bun";
import metadata from "./metadata.json";
const templateRaw = await Bun.file("./template.html").text();
const template = templateRaw
.replace("[[NAME]]", metadata.name)
.replace("[[YEAR]]", new Date().getFullYear().toString());
const posts = await readdir("./posts");
const defaultFrontmatter = {
thumb: "",
title: "Untitled",
desc: "",
date: "00-00-0000", // DD-MM-YYYY
tags: [] as never[],
draft: true,
};
function fetchFrontmatter(raw?: string) {
try {
if (!raw) throw new Error("No frontmatter found.");
return { ...defaultFrontmatter, ...(YAML.parse(raw) as typeof defaultFrontmatter) };
} catch {
console.warn("Failed to parse frontmatter. Using default values.");
return { ...defaultFrontmatter };
}
}
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, {
tables: true,
strikethrough: true,
tasklists: true,
autolinks: true,
headings: true,
hardSoftBreaks: true,
wikiLinks: true,
underline: true,
latexMath: true,
collapseWhitespace: true,
permissiveAtxHeaders: true,
noIndentedCodeBlocks: true,
noHtmlBlocks: true,
noHtmlSpans: true,
tagFilter: true,
});
const render = template
.replaceAll("[[TITLE]]", `${metadata.title} - ${frontmatter.title}`)
.replace("[[CONTENT]]", html);
await Bun.write(`./dist/posts/${post.replace(".md", ".html")}`, render);
});