styling and seo

This commit is contained in:
Satria 2026-05-27 20:48:03 +07:00
commit c1ad1593b6
5 changed files with 72 additions and 26 deletions

View file

@ -1,18 +1,18 @@
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());
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: "00-00-0000", // DD-MM-YYYY
date: "0000-00-00", // YYYY-MM-DD
tags: [] as never[],
draft: true,
};
@ -20,10 +20,11 @@ const defaultFrontmatter = {
function fetchFrontmatter(raw?: string) {
try {
if (!raw) throw new Error("No frontmatter found.");
return { ...defaultFrontmatter, ...(YAML.parse(raw) as typeof defaultFrontmatter) };
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 };
return { ...defaultFrontmatter, date: new Date(NaN) };
}
}
@ -36,24 +37,19 @@ posts.forEach(async (post) => {
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}`)
.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);