base for output and styling
This commit is contained in:
parent
6ca55b72c4
commit
bc21240e9f
7 changed files with 366 additions and 1 deletions
61
index.ts
61
index.ts
|
|
@ -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);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue