Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content
Official v5.0.0-beta.84 MIT kubb >=5.0.0 node >=22

@kubb/parser-md

Emits `.md` and `.markdown` files from the Kubb AST, joining source blocks as plain markdown and prepending YAML frontmatter from a file's meta.

markdownfrontmatterparserdocsyaml
Downloads
10.3k / mo
Stars
1.8k
Bundle size
22.4 kB
Updated
2d ago

@kubb/parser-md lets Kubb emit .md and .markdown files. Any plugin that writes a markdown source has its output serialized for you.

The parser joins a file's source blocks with blank lines to form the body. When file.meta.frontmatter is set, it renders those keys as a YAML frontmatter block and prepends it, so you do not add a yaml dependency yourself.

TIP

parserMd ships with Kubb and runs by default next to parserTs and parserTsx. Add it to parsers yourself only when you set a custom list, since a custom parsers array replaces the whole default set. Files whose extension has no registered parser are written by joining their sources verbatim.

Installation

shell
bun add -d @kubb/parser-md@beta
shell
pnpm add -D @kubb/parser-md@beta
shell
npm install --save-dev @kubb/parser-md@beta
shell
yarn add -D @kubb/parser-md@beta

Frontmatter

@kubb/parser-md takes no options of its own. To add a YAML frontmatter block to a generated page, set frontmatter on a file's meta inside a plugin. The parser renders those keys and prepends them to the output. Any serializable object works.

Type: Record<string, unknown> | null
Plugin that sets frontmatter
typescript
ast.factory.createFile({
  baseName: 'README.md',
  path: `${config.output.path}/README.md`,
  meta: {
    frontmatter: { title: 'API Reference', layout: 'doc' },
  },
  sources: [...],
})

The parser turns that meta into:

markdown
---
title: API Reference
layout: doc
---

You can also call parserMd.print directly to build a frontmatter envelope. It accepts objects and markdown strings and joins them with blank lines, so parserMd.print({ title: 'Pets', layout: 'doc' }) returns ---\ntitle: Pets\nlayout: doc\n---.

Example

typescript
import { defineConfig } from 'kubb'
import { adapterOas } from '@kubb/adapter-oas'
import { parserMd } from '@kubb/parser-md'

export default defineConfig({
  input: { path: './petStore.yaml' },
  output: { path: './src/gen' },
  adapter: adapterOas(),
  parsers: [parserMd],
  plugins: [],
})
typescript
import {  } from 'kubb'
import {  } from '@kubb/adapter-oas'
import {  } from '@kubb/parser-md'
import { ,  } from '@kubb/parser-ts'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen' },
  : (),
  : [, , ],
  : [],
})

See also