site: add frontmatter parsing support

This commit is contained in:
Glen Huang
2025-10-06 21:33:54 +08:00
committed by Johannes Kirschbauer
parent 7112f608a7
commit 84ab04fc06
6 changed files with 1466 additions and 11 deletions

View File

@@ -1,15 +1,32 @@
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import { matter } from "vfile-matter";
import { unified } from "unified";
import { VFile } from "vfile";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
export default defineConfig({
plugins: [
sveltekit(),
{
name: "markdown-loader",
transform(code, id) {
if (id.slice(-3) === ".md") {
return `export default ${JSON.stringify(code)};`;
}
async transform(code, id) {
if (id.slice(-3) !== ".md") return;
// TODO: move VFile into unified
const file = new VFile(code);
matter(file, { strip: true });
const html = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.process(String(file));
return `
export default ${JSON.stringify(String(html))};
export const frontmatter = ${JSON.stringify(file.data.matter)};`;
},
},
],