A few simple helper functions to generate HTML from markdown. Just enough to get you started with markdown with Mastro.
By default, it uses micromark with micromark-extension-gfm under the hood.
Validate YAML frontmatter by bringing your own Standard Schema-compliant validation library (e.g. Zod, Valibot or validate.js).
deno add jsr:@mastrojs/markdown
pnpm add jsr:@mastrojs/markdown
bunx jsr add @mastrojs/markdown
import { markdownToHtml } from "@mastrojs/markdown";
const { content, meta } = await markdownToHtml(`
---
title: my title
---
hi *there*
`),In addition to markdownToHtml(inputStr, opts), there are utility functions for use with Mastro:
readMarkdownFile(filePath, opts)readMarkdownFiles(globPattern, opts)serveMarkdownFolder(opts, renderFn)
Use the parse option to supply either a Micromark options object:
markdownToHtml(input, { parse: { allowDangerousHtml: true } });or a custom markdown-to-HTML function:
import markdownIt from "markdown-it";
markdownToHtml(input, { parse: markdownIt.render });The default TypeScript type for the YAML metadata is Record<string, unknown>, but you can override that with e.g. readMarkdownFile<{title: string}>("post.md"). But to actually verify the metadata is correct, you should use a schema. For example using validate.js:
import { object, string } from "./validate.js";
const schema = object({
title: string,
});
const { content, meta } = await markdownToHtml(input, { schema }),To see all functions @mastrojs/markdown exports, see its API docs.
For a tutorial, read the following chapter in the Mastro Guide: A static blog from markdown files