final tweaks before deploy
This commit is contained in:
parent
550ce3dabc
commit
a9e4ec8973
6 changed files with 104 additions and 157 deletions
50
index.ts
50
index.ts
|
|
@ -2,19 +2,28 @@ import { readdir } from "node:fs/promises";
|
|||
import { YAML } from "bun";
|
||||
import metadata from "./metadata.json";
|
||||
import templateRaw from "./templates/post.html" with { type: "text" };
|
||||
const templateStr = templateRaw as unknown as string;
|
||||
const template = templateStr
|
||||
import homeRaw from "./templates/home.html" with { type: "text" };
|
||||
|
||||
const year = new Date().getFullYear().toString();
|
||||
const applyVars = (raw: unknown) => (raw as string)
|
||||
.replaceAll("[[SITE]]", metadata.title)
|
||||
.replaceAll("[[NAME]]", metadata.name)
|
||||
.replaceAll("[[TIMEZONE]]", metadata.timezone)
|
||||
.replaceAll("[[YEAR]]", new Date().getFullYear().toString());
|
||||
const posts = await readdir("./posts");
|
||||
.replaceAll("[[YEAR]]", year);
|
||||
|
||||
const template = applyVars(templateRaw);
|
||||
const home = applyVars(homeRaw);
|
||||
|
||||
const posts = await readdir("./posts");
|
||||
const postLinks: {
|
||||
title: string;
|
||||
date: Date;
|
||||
link: string;
|
||||
}[] = [];
|
||||
const defaultFrontmatter = {
|
||||
title: "Untitled",
|
||||
desc: "",
|
||||
date: "0000-00-00", // YYYY-MM-DD
|
||||
tags: [] as never[],
|
||||
draft: true,
|
||||
};
|
||||
|
||||
|
|
@ -29,28 +38,45 @@ function fetchFrontmatter(raw?: string) {
|
|||
}
|
||||
}
|
||||
|
||||
posts.forEach(async (post) => {
|
||||
console.log(`Processing ${post}...`);
|
||||
await Promise.all(posts.map(async (post) => {
|
||||
const raw = (await Bun.file(`./posts/${post}`).text()).split("^---");
|
||||
|
||||
const content = raw.at(-1) || "No content";
|
||||
|
||||
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("[[REVISIONS]]", `${metadata.revisions}/${post}`)
|
||||
.replaceAll("[[DESC]]", frontmatter.desc)
|
||||
.replace("[[CONTENT]]", html);
|
||||
|
||||
await Bun.write(`./dist/posts/${post.replace(".md", ".html")}`, render);
|
||||
});
|
||||
|
||||
if (!frontmatter.draft) postLinks.push({
|
||||
title: frontmatter.title,
|
||||
date: frontmatter.date,
|
||||
link: `/posts/${post.replace(".md", ".html")}`,
|
||||
});
|
||||
|
||||
console.log(`Built page for ${frontmatter.draft ? 'draft' : 'post'}: ${frontmatter.title}`);
|
||||
// TODO: table of contents generation
|
||||
}));
|
||||
|
||||
const parsedPosts = postLinks
|
||||
.sort((a, b) => b.date.getTime() - a.date.getTime())
|
||||
.map((post) => `<li><a href="${post.link}">${post.title}</a> - <i>${post.date.toDateString()}</i></li>`)
|
||||
.join("\n");
|
||||
const render = home
|
||||
.replace("[[ABOUT]]", Bun.markdown.html(metadata.description))
|
||||
.replace("[[MAIN]]", metadata.main)
|
||||
.replace("[[POSTS]]", parsedPosts);
|
||||
|
||||
await Bun.write("./dist/index.html", render);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue