micromark extension to support frontmatter (YAML, TOML, etc)

Downloads in past


1512.0.03 months ago3 years agoMinified + gzip package size for micromark-extension-frontmatter in KB


!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
micromark extensions to support frontmatter (YAML, TOML, and more).


*   [`frontmatter(options?)`](#frontmatteroptions)
*   [`frontmatterHtml(options?)`](#frontmatterhtmloptions)
*   [`toMatters(options?)`](#tomattersoptions)
*   [`Info`](#info)
*   [`Matter`](#matter)
*   [`Options`](#options)
*   [`Preset`](#preset)

What is this?

This package contains two extensions that add support for frontmatter syntax as often used in markdown to micromarkmicromark.
Frontmatter is a metadata format in front of the content. It’s typically written in YAML and is often used with markdown. Frontmatter does not work everywhere so it makes markdown less portable.
As there is no spec for frontmatter in markdown, these extensions follow how YAML frontmatter works on github.com. It can also parse TOML frontmatter, just like YAML except that it uses a +.

When to use this

You can use these extensions when you are working with micromarkmicromark already.
When you need a syntax tree, you can combine this package with mdast-util-frontmattermdast-util-frontmatter.
All these packages are used remark-frontmatterremark-frontmatter, which focusses on making it easier to transform content by abstracting these internals away.


This package is ESM onlyesm. In Node.js (version 16+), install with npm:
npm install micromark-extension-frontmatter

In Deno with esm.shesmsh:
import {frontmatter, frontmatterHtml} from 'https://esm.sh/micromark-extension-frontmatter@2'

In browsers with esm.shesmsh:
<script type="module">
  import {frontmatter, frontmatterHtml} from 'https://esm.sh/micromark-extension-frontmatter@2?bundle'


Say our module example.js looks as follows:
import {micromark} from 'micromark'
import {frontmatter, frontmatterHtml} from 'micromark-extension-frontmatter'

const output = micromark('---\na: b\n---\n# c', {
  extensions: [frontmatter()],
  htmlExtensions: [frontmatterHtml()]


…now running node example.js yields:


This package exports the identifiers frontmatterapi-frontmatter, frontmatterHtmlapi-frontmatter-html, and toMattersapi-to-matters. There is no default export.
The export map supports the development conditiondevelopment. Run node --conditions development module.js to get instrumented dev code. Without this condition, production code is loaded.


Create an extension for micromarkmicromark to enable frontmatter syntax.
β€” configuration
Extension for micromark that can be passed in extensions, to enable frontmatter syntax (Extensionmicromark-extension).


Create an extension for micromark to support frontmatter when serializing to HTML.
πŸ‘‰ Note: this makes sure nothing is generated in the output HTML for frontmatter.
β€” configuration
Extension for micromark that can be passed in htmlExtensions, to support frontmatter when serializing to HTML (HtmlExtensionmicromark-html-extension).


Simplify options by normalizing them to an array of matters.
β€” configuration
List of matters (Array<Matter>api-matter).


Sequence (TypeScript type).
Depending on how this structure is used, it reflects a marker or a fence.
  • open (string)
β€” opening
  • close (string)
β€” closing


Fields describing a kind of matter (TypeScript type).
πŸ‘‰ Note: using anywhere is a terrible idea. It’s called frontmatter, not matter-in-the-middle or so. This makes your markdown less portable.

πŸ‘‰ Note: marker and fence are mutually exclusive. If marker is set, fence must not be set, and vice versa.
  • type (string)
β€” node type to tokenize as
β€” character repeated 3 times, used as complete fences
β€” complete fences
  • anywhere (boolean, default: false)
β€” whether matter can be found anywhere in the document, normally only
matter at the start of the document is recognized


Configuration (TypeScript type).
type Options = Array<Matter | Preset> | Matter | Preset


Known name of a frontmatter style (TypeScript type).
type Preset = 'toml' | 'yaml'


Here are a couple of example of different matter objects and what frontmatter they match.
To match frontmatter with the same opening and closing fence, namely three of the same markers, use for example {type: 'yaml', marker: '-'}, which matches:


key: value

To match frontmatter with different opening and closing fences, which each use
three different markers, use for example
`{type: 'custom', marker: {open: '<', close: '>'}}`, which matches:


To match frontmatter with the same opening and closing fences, which both use the same custom string, use for example {type: 'custom', fence: '+=+=+=+'}, which matches:

To match frontmatter with different opening and closing fences, which each use different custom strings, use for example {type: 'json', fence: {open: '{', close: '}'}}, which matches:
  "key": "value"


When authoring markdown with frontmatter, it’s recommended to use YAML frontmatter if possible. While YAML has some warts, it works in the most places, so using it guarantees the highest chance of portability.
In certain ecosystems, other flavors are widely used. For example, in the Rust ecosystem, TOML is often used. In such cases, using TOML is an okay choice.
When possible, do not use other types of frontmatter, and do not allow frontmatter anywhere.


Frontmatter does not relate to HTML elements. It is typically stripped, which is what these extensions do.


This package does not relate to CSS.


Frontmatter forms with the following BNF:
frontmatter ::= fence_open *( eol *line ) eol fence_close
fence_open ::= sequence_open *space_or_tab
fence_close ::= sequence_close *space_or_tab
; Note: options can define custom sequences.
sequence_open ::= 3'+' | 3'-'
; Note: options can define custom sequences.
; Restriction: `sequence_close` must correspond to `sequence_open`.
sequence_close ::= 3'+' | 3'-'

; Character groups for informational purposes.
byte ::= 0x00..=0xFFFF
eol ::= '\n' | '\r' | '\r\n'
line ::= byte - eol

Frontmatter can only occur once. It cannot occur in a container. It must have a closing fence. Like flow constructs, it must be followed by an eol (line ending) or eof (end of file).


This package is fully typed with TypeScript. It exports the additional types Infoapi-info, Matterapi-matter, Optionsapi-options, Presetapi-preset.


Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, micromark-extension-frontmatter@^2, compatible with Node.js 16.
This package works with micromark version 3 and later.


This package is safe.


β€” remark plugin using this to support frontmatter
β€” mdast utility to support frontmatter


See contributing.md in micromark/.githubcontributing for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.


MITlicense Β© Titus Wormerauthor