site: add frontmatter parsing support
This commit is contained in:
committed by
Johannes Kirschbauer
parent
7112f608a7
commit
84ab04fc06
@@ -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)};`;
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user