60 lines
No EOL
1.7 KiB
TypeScript
60 lines
No EOL
1.7 KiB
TypeScript
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);
|
|
}); |