Skip to content

mastrojs/markdown

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@mastrojs/markdown

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).

Install

Deno

deno add jsr:@mastrojs/markdown

Node.js

pnpm add jsr:@mastrojs/markdown

Bun

bunx jsr add @mastrojs/markdown

Usage

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:

Options

Parse

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 });

Schema

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 }),

Docs

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

About

Generate HTML from markdown.

Resources

License

Stars

Watchers

Forks

Contributors