remark plugin to support frontmatter (YAML, TOML, and more).
Contents
What is this?
This package is a unified (remark) plugin to add support for YAML, TOML,
and other frontmatter.
Frontmatter is a metadata format in front of the content.
It’s typically written in YAML and is often used with markdown.
This plugin follow how GitHub handles frontmatter.
GitHub only supports YAML frontmatter, but this plugin also supports different
flavors (such as TOML).
When should I use this?
You can use frontmatter when you want authors, that have some markup
experience, to configure where or how the content is displayed or supply
metadata about content, and know that the markdown is only used in places
that support frontmatter.
A good example use case is markdown being rendered by (static) site generators.
If you just want to turn markdown into HTML (with maybe a few extensions such
as frontmatter), we recommend micromark
with
micromark-extension-frontmatter
instead.
If you don’t use plugins and want to access the syntax tree, you can use
mdast-util-from-markdown
with
mdast-util-frontmatter
.
Install
This package is ESM only.
In Node.js (version 16+), install with npm:
npm install remark-frontmatter
In Deno with esm.sh
:
import remarkFrontmatter from 'https://esm.sh/remark-frontmatter@5'
In browsers with esm.sh
:
<script type="module">
import remarkFrontmatter from 'https://esm.sh/remark-frontmatter@5?bundle'
</script>
Use
Say our document example.md
contains:
+++
layout = "solar-system"
+++
# Jupiter
…and our module example.js
contains:
import remarkFrontmatter from 'remark-frontmatter'
import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
import {unified} from 'unified'
import {read} from 'to-vfile'
const file = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter, ['yaml', 'toml'])
.use(function () {
return function (tree) {
console.dir(tree)
}
})
.process(await read('example.md'))
console.log(String(file))
…then running node example.js
yields:
{
type: 'root',
children: [
{type: 'toml', value: 'layout = "solar-system"', position: [Object]},
{type: 'heading', depth: 1, children: [Array], position: [Object]}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 6, column: 1, offset: 43}
}
}
+++
layout = "solar-system"
+++
# Jupiter
API
This package exports no identifiers.
The default export is remarkFrontmatter
.
Add support for frontmatter.
Parameters
options
(Options
, default: 'yaml'
)
— configuration
Returns
Nothing (undefined
).
Notes
Doesn’t parse the data inside them:
create your own plugin to do that.
Options
Configuration (TypeScript type).
Type
type Options = Array<Matter | Preset> | Matter | Preset
export type Info = {
close: string
open: string
}
type FenceProps = {
fence: Info | string
marker?: never
}
type MarkerProps = {
marker: Info | string
fence?: never
}
type Matter = (MatterProps & FenceProps) | (MatterProps & MarkerProps)
type MatterProps = {
type: string
anywhere?: boolean | null | undefined
}
type Preset = 'toml' | 'yaml'
Examples
Example: different markers and fences
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:
<<<
data
>>>
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:
+=+=+=+
data
+=+=+=+
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"
}
Example: frontmatter as metadata
This plugin handles the syntax of frontmatter in markdown.
It does not parse that frontmatter as say YAML or TOML and expose it
somewhere.
In unified, there is a place for metadata about files:
file.data
.
For frontmatter specifically, it’s customary to expose parsed data at file.data.matter
.
We can make a plugin that does this.
This example uses the utility vfile-matter
, which is specific
to YAML.
To support other data languages, look at this utility for inspiration.
my-unified-plugin-handling-yaml-matter.js
:
import {matter} from 'vfile-matter'
export default function myUnifiedPluginHandlingYamlMatter() {
return function (tree, file) {
matter(file)
}
}
…with an example markdown file example.md
:
---
key: value
---
# Venus
…and using the plugin with an example.js
containing:
import remarkParse from 'remark-parse'
import remarkFrontmatter from 'remark-frontmatter'
import remarkStringify from 'remark-stringify'
import {read} from 'to-vfile'
import {unified} from 'unified'
import myUnifiedPluginHandlingYamlMatter from './my-unified-plugin-handling-yaml-matter.js'
const file = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter)
.use(myUnifiedPluginHandlingYamlMatter)
.process(await read('example.md'))
console.log(file.data.matter)
Example: frontmatter in MDX
MDX has the ability to export data from it, where markdown does not.
When authoring MDX, you can write export
statements and expose arbitrary data
through them.
It is also possible to write frontmatter, and let a plugin turn those into
export statements.
To automatically turn frontmatter into export statements, use
remark-mdx-frontmatter
.
With an example.mdx
as follows:
---
key: value
---
# Mars
This plugin can be used as follows:
import {compile} from '@mdx-js/mdx'
import remarkFrontmatter from 'remark-frontmatter'
import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
import {read, write} from 'to-vfile'
const file = await compile(await read('example.mdx'), {
remarkPlugins: [remarkFrontmatter, [remarkMdxFrontmatter, {name: 'matter'}]]
})
file.path = 'output.js'
await write(file)
const mod = await import('./output.js')
console.log(mod.matter)
Authoring
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.
HTML
Frontmatter does not relate to HTML elements.
It is typically stripped, which is what remark-rehype
does.
CSS
This package does not relate to CSS.
Syntax
See Syntax in
micromark-extension-frontmatter
.
Syntax tree
See Syntax tree in
mdast-util-frontmatter
.
Types
This package is fully typed with TypeScript.
It exports the additional type Options
.
The YAML node type is supported in @types/mdast
by default.
To add other node types, register them by adding them to
FrontmatterContentMap
:
import type {Data, Literal} from 'mdast'
interface Toml extends Literal {
type: 'toml'
data?: TomlData
}
declare module 'mdast' {
interface FrontmatterContentMap {
toml: Toml
}
}
Compatibility
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, remark-frontmatter@^5
,
compatible with Node.js 16.
This plugin works with unified 6+ and remark 13+.
Security
Use of remark-frontmatter
does not involve rehype (hast) or user
content so there are no openings for cross-site scripting (XSS)
attacks.
Related
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, organization, or community you agree to
abide by its terms.
License
MIT © Titus Wormer