Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
micromark-extension-frontmatter
Advanced tools
micromark extension to support frontmatter (YAML, TOML, etc)
The micromark-extension-frontmatter package is an extension for micromark, a Markdown parser, that allows for the parsing of frontmatter. Frontmatter is metadata at the beginning of a Markdown file, typically used for configuration or data storage purposes.
Parsing YAML Frontmatter
This feature allows you to parse YAML frontmatter in a Markdown file. The code sample demonstrates how to use the micromark-extension-frontmatter to parse a Markdown string containing YAML frontmatter and convert it to HTML.
const micromark = require('micromark');
const { frontmatter } = require('micromark-extension-frontmatter');
const markdown = `---
title: Example Title
author: John Doe
---
# Hello World
`;
const html = micromark(markdown, {
extensions: [frontmatter()]
});
console.log(html);
Parsing TOML Frontmatter
This feature allows you to parse TOML frontmatter in a Markdown file. The code sample demonstrates how to use the micromark-extension-frontmatter to parse a Markdown string containing TOML frontmatter and convert it to HTML.
const micromark = require('micromark');
const { frontmatter } = require('micromark-extension-frontmatter');
const markdown = `+++
title = "Example Title"
author = "John Doe"
+++
# Hello World
`;
const html = micromark(markdown, {
extensions: [frontmatter('toml')]
});
console.log(html);
Parsing JSON Frontmatter
This feature allows you to parse JSON frontmatter in a Markdown file. The code sample demonstrates how to use the micromark-extension-frontmatter to parse a Markdown string containing JSON frontmatter and convert it to HTML.
const micromark = require('micromark');
const { frontmatter } = require('micromark-extension-frontmatter');
const markdown = `{
"title": "Example Title",
"author": "John Doe"
}
# Hello World
`;
const html = micromark(markdown, {
extensions: [frontmatter('json')]
});
console.log(html);
gray-matter is a popular package for parsing frontmatter from strings or files. It supports YAML, JSON, and TOML frontmatter. Unlike micromark-extension-frontmatter, which is an extension for micromark, gray-matter is a standalone package that can be used independently of any Markdown parser.
front-matter is another package for parsing frontmatter from strings. It supports YAML frontmatter and is simpler and more lightweight compared to gray-matter. It does not support TOML or JSON frontmatter, making it less versatile than micromark-extension-frontmatter.
markdown-it-front-matter is a plugin for the markdown-it Markdown parser that allows for the extraction of frontmatter. It primarily supports YAML frontmatter. This package is similar to micromark-extension-frontmatter in that it is an extension for a specific Markdown parser.
micromark extensions to support frontmatter (YAML, TOML, and more).
This package contains two extensions that add support for frontmatter syntax
as often used in markdown to micromark
.
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 +
.
You can use these extensions when you are working with micromark
already.
When you need a syntax tree, you can combine this package with
mdast-util-frontmatter
.
All these packages are used remark-frontmatter
, which
focusses on making it easier to transform content by abstracting these
internals away.
This package is ESM only. In Node.js (version 14.14+), install with npm:
npm install micromark-extension-frontmatter
In Deno with esm.sh
:
import {frontmatter, frontmatterHtml} from 'https://esm.sh/micromark-extension-frontmatter@1'
In browsers with esm.sh
:
<script type="module">
import {frontmatter, frontmatterHtml} from 'https://esm.sh/micromark-extension-frontmatter@1?bundle'
</script>
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()]
})
console.log(output)
…now running node example.js
yields:
<h1>c</h1>
This package exports the identifiers frontmatter
and
frontmatterHtml
.
There is no default export.
The export map supports the development
condition.
Run node --conditions development module.js
to get instrumented dev code.
Without this condition, production code is loaded.
frontmatter(options?)
Create an extension for micromark
to enable frontmatter syntax.
options
(Options
, default: ['yaml']
)
— configurationExtension for micromark
that can be passed in extensions
, to enable
frontmatter syntax (Extension
).
frontmatterHtml(options?)
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.
options
(Options
, default: ['yaml']
)
— configurationExtension for micromark
that can be passed in htmlExtensions
, to support
frontmatter when serializing to HTML
(HtmlExtension
).
Info
Sequence (TypeScript type).
Depending on how this structure is used, it reflects a marker or a fence.
open
(string
)
— openingclose
(string
)
— closingMatter
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
andfence
are mutually exclusive. Ifmarker
is set,fence
must not be set, and vice versa.
type
(string
)
— node type to tokenize asmarker
(string
or Info
)
— character repeated 3 times, used as complete fencesfence
(string
or Info
)
— complete fencesanywhere
(boolean
, default: false
)
— whether matter can be found anywhere in the document, normally only
matter at the start of the document is recognizedOptions
Configuration (TypeScript type).
type Options = Matter | Preset | Array<Matter | Preset>
Preset
Known name of a frontmatter style (TypeScript type).
'yaml'
— Matter
defined as {type: 'yaml', marker: '-'}
'toml'
— Matter
defined as {type: 'toml', marker: '+'}
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:
<<<
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"
}
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 Info
, Matter
,
Options
, Preset
.
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+. Our projects sometimes work with older versions, but this is not guaranteed.
These extensions work with micromark
version 3+.
This package is safe.
remark-frontmatter
— remark plugin using this to support frontmattermdast-util-frontmatter
— mdast utility to support frontmatterSee contributing.md
in micromark/.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.
FAQs
micromark extension to support frontmatter (YAML, TOML, etc)
The npm package micromark-extension-frontmatter receives a total of 897,660 weekly downloads. As such, micromark-extension-frontmatter popularity was classified as popular.
We found that micromark-extension-frontmatter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.