跳转至

mdast-util-frontmatter

Build Coverage Downloads Size Sponsors Backers Chat

Extension for mdast-util-from-markdown and/or mdast-util-to-markdown to support frontmatter in mdast. When parsing (from-markdown), must be combined with micromark-extension-frontmatter.

You probably shouldn’t use this package directly, but instead use remark-frontmatter with remark.

Install

npm:

npm install mdast-util-frontmatter

Use

Say we have the following file, example.md:

+++
title = "New Website"
+++

# Other markdown

And our script, example.js, looks as follows:

var fs = require('fs')
var fromMarkdown = require('mdast-util-from-markdown')
var toMarkdown = require('mdast-util-to-markdown')
var syntax = require('micromark-extension-frontmatter')
var frontmatter = require('mdast-util-frontmatter')

var doc = fs.readFileSync('example.md')

var tree = fromMarkdown(doc, {
  extensions: [syntax(['yaml', 'toml'])],
  mdastExtensions: [frontmatter.fromMarkdown(['yaml', 'toml'])]
})

console.log(tree)

var out = toMarkdown({extensions: [frontmatter.toMarkdown(['yaml', 'toml'])]})

console.log(out)

Now, running node example yields:

{
  type: 'root',
  children: [
    {type: 'toml', value: 'title = "New Website"'},
    {
      type: 'heading',
      depth: 1,
      children: [{type: 'text', value: 'Other markdown'}]
    }
  ]
}
+++
title = "New Website"
+++

# Other markdown

API

frontmatter.fromMarkdown([options])

frontmatter.toMarkdown([options])

Note: the separate extensions are also available at mdast-util-frontmatter/from-markdown and mdast-util-frontmatter/to-markdown.

Support frontmatter (YAML, TOML, and more). These functions can be called with options and return extensions, respectively for mdast-util-from-markdown and mdast-util-to-markdown.

Options are the same as micromark-extension-frontmatter.

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer