stacks/bun-plugin-markdown
publicClone
Push over the same URL. A password will not work: create a token under access tokens and use it in place of one.
- .config
- .github
- .vscode
- docs
- examples
- src
- test
- .editorconfig 147 B
- .gitattributes 12 B
- .gitignore 110 B
- build.ts 568 B
- bun.lock 50.7 KB
- bunfig.toml 136 B
- CHANGELOG.md 5.8 KB
- deps.yaml 32 B
- LICENSE.md 1.1 KB
- package.json 2.9 KB
- pantry.lock 261 B
- README.md 6.3 KB
- test-bun-loader.sh 418 B
- tsconfig.json 627 B

Bun Frontmatter Markdown Loader
A Bun loader and plugin for handling Markdown files with frontmatter.
Features
- Parse frontmatter from markdown files
- Convert markdown to HTML
- Support for various output modes, including:
- HTML rendering
- Raw markdown body
- Metadata
- React component generation
- Vue component and render functions
Installation
bun add bun-plugin-markdownUsage
As a Bun Plugin
The simplest way to use this package is as a Bun plugin:
import { markdownPlugin } from 'bun-plugin-markdown'
// Now you can import markdown files directly
import myContent from './content/page.md'
// Register the plugin globally
Bun.plugin(markdownPlugin)
console.log(myContent.html) // Rendered HTML
console.log(myContent.attributes) // Frontmatter attributesCustom Plugin Configuration
You can customize the behavior of the plugin:
import { frontmatterMarkdownPlugin, Mode } from 'bun-plugin-markdown'
// Register the plugin with custom options
Bun.plugin(frontmatterMarkdownPlugin({
mode: [Mode.HTML, Mode.BODY, Mode.META],
// Add other options as needed
}))With Bun.build
import { frontmatterMarkdownPlugin, Mode } from 'bun-plugin-markdown'
await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
plugins: [
frontmatterMarkdownPlugin({
mode: [Mode.HTML, Mode.BODY]
})
]
})With React Support
For React support, install the required peer dependencies:
bun add @babel/core @babel/preset-react -DThen use the plugin with React mode:
import { frontmatterMarkdownPlugin, Mode } from 'bun-plugin-markdown'
Bun.plugin(frontmatterMarkdownPlugin({
mode: [Mode.HTML, Mode.REACT],
react: {
root: 'markdown-content' // Optional custom root class
}
}))
// In your component
import myContent from './content/page.md'
function MyComponent() {
return (
<div>
<h1>{myContent.attributes.title}</h1>
{/* Render the React component */}
{myContent.react()}
</div>
)
}With Vue Support
For Vue support, install the required peer dependencies:
bun add vue-template-compiler @vue/component-compiler-utils -DThen use the plugin with Vue mode:
import { frontmatterMarkdownPlugin, Mode } from 'bun-plugin-markdown'
// In your component
import myContent from './content/page.md'
Bun.plugin(frontmatterMarkdownPlugin({
mode: [Mode.HTML, Mode.VUE_COMPONENT],
vue: {
root: 'markdown-content' // Optional custom root class
}
}))
export default {
components: {
MarkdownContent: myContent.vue.component
},
template: `
<div>
<h1>README.md</h1>
<markdown-content />
</div>
`,
data() {
return {
title: myContent.attributes.title
}
}
}With Bun Fullstack Server
Bun's fullstack dev server can use the markdown plugin when configured in your bunfig.toml:
[serve.static]
plugins = [ "bun-plugin-markdown/plugin" ]Then you can import markdown files directly in your HTML:
<!DOCTYPE html>
<html>
<head>
<title>Markdown Viewer</title>
<style>
/* Your styles here */
</style>
</head>
<body>
<div id="app"></div>
<script type="module">
// Import markdown file directly
import content from './content.md';
// Use the processed content
document.getElementById('app').innerHTML = content.html;
console.log(content.attributes); // Access frontmatter
</script>
</body>
</html>And use it in your server:
import { serve } from 'bun'
// Create your HTML template
const template = `/* HTML with markdown import */`
serve({
port: 3000,
development: true,
routes: {
'/': new Response(template, {
headers: { 'Content-Type': 'text/html' }
})
}
})To see a complete example, run:
bun run fullstackAvailable Modes
The loader supports different output modes:
Mode.HTML: Generates HTML from the markdownMode.BODY: Includes the raw markdown bodyMode.META: Includes metadata about the source fileMode.REACT: Generates a React componentMode.VUE_COMPONENT: Generates a Vue componentMode.VUE_RENDER_FUNCTIONS: Generates Vue render functions
License
MIT
Testing
bun testChangelog
Please see our releases page for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Community
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
Postcardware
"Software that is free, but hopes for a postcard." We love receiving postcards from around the world showing where Stacks is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
Sponsors
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
[codecov-href]: https://codecov.io/gh/stacksjs/ts-starter -->