Blog/bun_plugins/onImport-markdown-loader.ts

30 lines
907 B
TypeScript

import { main, type BunPlugin } from 'bun';
import { loadMetadata } from "./utils";
import matter from 'gray-matter';
import { marked } from 'marked';
import { AppShell } from "../src/frontend/AppShell";
import AppShellPage from "../src/frontend/AppShell.html" with { type: "text" };
import { renderToString } from "react-dom/server";
const markdownLoader: BunPlugin = {
name: 'markdown-loader',
setup(build) {
// Plugin implementation
build.onLoad({filter: /\.md$/}, async args => {
console.log("Loading markdown file:", args.path);
const {data, content } = matter(await Bun.file(args.path).text());
loadMetadata(args.path, data);
const html = marked.parse(content);
// JSX Approach
console.log(renderToString(AppShell({ post: html })))
return {
contents: renderToString(AppShell({ post: html })),
loader: 'html',
};
});
},
};
export default markdownLoader;