import path from "node:path"; import fs from "node:fs/promises"; import { matter } from "toml-matter"; type MatterFile = ReturnType; export type Post = { input: string; output: string; basename: string; url: string; title: string; date: Date; description: string | undefined; taxonomies: Record; }; export async function getPost(filename: string): Promise { return matter(await fs.readFile(filename, "utf8")); } export async function readPosts( root: string, inputDir: string, outputDir: string, ): Promise<{ posts: Array; tags: Set }> { let tags = new Set(); let posts = new Array(); const subdir = path.join(root, inputDir); for (let pathname of await fs.readdir(subdir)) { const pathFromRoot = path.join(subdir, pathname); const stat = await fs.stat(pathFromRoot); if (stat.isFile() && path.extname(pathname) === ".md") { if (pathname !== "_index.md") { const input = pathFromRoot; const output = pathFromRoot .replace(root, outputDir) .replace(".md", "/index.html"); const url = pathFromRoot.replace(root, "").replace(".md", "/"); const file = await getPost(input); (file.data["taxonomies"] as any)?.tags?.map((t: string) => tags.add(t.toLowerCase()), ); posts.push({ input, output, basename: path.basename(pathname, ".md"), url, ...file.data, } as Post); } } } return { posts: posts.sort((a, b) => b.date.getTime() - a.date.getTime()), tags, }; }