unified

Project:remarkjs/remark-frontmatter

Package:remark-frontmatter@1.3.2

  1. Dependencies:2·Dependents:110
  2. remark plugin to support frontmatter (yaml, toml, and more)
  1. remark 188
  2. unified 132
  3. markdown 96
  4. plugin 88
  5. mdast 69
  6. frontmatter 3
  7. yaml 3

remark-frontmatter

Build Coverage Downloads Size Sponsors Backers Chat

remark plugin to support frontmatter (YAML, TOML, and more).

Install

npm:

npm install remark-frontmatter

Use

Say we have the following file, example.md:

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

# Other markdown

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

var vfile = require('to-vfile')
var report = require('vfile-reporter')
var unified = require('unified')
var parse = require('remark-parse')
var stringify = require('remark-stringify')
var frontmatter = require('remark-frontmatter')

unified()
  .use(parse)
  .use(stringify)
  .use(frontmatter, ['yaml', 'toml'])
  .use(logger)
  .process(vfile.readSync('example.md'), function(err, file) {
    console.log(String(file))
    console.error(report(err || file))
  })

function logger() {
  return console.dir
}

Now, running node example yields:

{ type: 'root',
  children:
   [ { type: 'toml',
       value: 'title = "New Website"',
       position: [Object] },
     { type: 'heading',
       depth: 1,
       children: [Array],
       position: [Object] } ],
  position: [Object] }
example.md: no issues found
+++
title = "New Website"
+++

# Other markdown

API

remark().use(frontmatter[, options])

Support frontmatter (YAML, TOML, and more). Adds tokenizers if the processor is configured with remark-parse, and visitors if configured with remark-stringify.

If you are parsing from a different syntax, or compiling to a different syntax (such as, remark-man) your custom nodes may not be supported.

options

One preset or Matter, or an array of them, defining all the supported frontmatters (default: 'yaml').

preset

Either 'yaml' or 'toml':

Matter

An object with a type and either a marker or a fence:

Example

For {type: 'yaml', marker: '-'}:

---
key: value
---

Yields:

{
  "type": "yaml",
  "value": "key: value"
}
Example

For {type: 'custom', marker: {open: '<', close: '>'}}:

<<<
data
>>>

Yields:

{
  "type": "custom",
  "value": "data"
}
Example

For {type: 'custom', fence: '+=+=+=+'}:

+=+=+=+
data
+=+=+=+

Yields:

{
  "type": "custom",
  "value": "dats"
}
Example

For {type: 'json', fence: {open: '{', close: '}'}}:

{
  "key": "value"
}

Yields:

{
  "type": "json",
  "value": "\"key\": \"value\""
}

Contribute

See contributing.md in remarkjs/.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, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer