56 lines
No EOL
1.9 KiB
TypeScript
56 lines
No EOL
1.9 KiB
TypeScript
import { readdir } from "node:fs/promises";
|
|
import { YAML } from "bun";
|
|
import metadata from "./metadata.json";
|
|
import templateRaw from "./template.html" with { type: "text" };
|
|
const templateStr = templateRaw as unknown as string;
|
|
const template = templateStr
|
|
.replaceAll("[[NAME]]", metadata.name)
|
|
.replaceAll("[[YEAR]]", new Date().getFullYear().toString());
|
|
const posts = await readdir("./posts");
|
|
|
|
const defaultFrontmatter = {
|
|
thumb: "",
|
|
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)
|
|
.replaceAll("[[THUMB]]", frontmatter.thumb)
|
|
.replace("[[CONTENT]]", html);
|
|
|
|
await Bun.write(`./dist/posts/${post.replace(".md", ".html")}`, render);
|
|
}); |