unified

Project: syntax-tree/mdast-util-frontmatter

Package: mdast-util-frontmatter@0.2.0

  1. Dependencies: 1·Dependents: 0
  2. mdast extension to parse and serialize frontmatter (YAML, TOML, etc)
  1. markdown 133
  2. util 118
  3. utility 114
  4. unist 110
  5. mdast 81
  6. mdast-util 24
  7. gfm 17
  8. markup 12
  9. frontmatter 5
  10. yaml 5
  11. toml 3

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(tree, {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