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.
remark-frontmatter
Advanced tools
The remark-frontmatter package is a plugin for the remark processor that allows you to parse and manipulate frontmatter in Markdown files. Frontmatter is typically used to include metadata at the beginning of a Markdown document, often in YAML or TOML format.
Parsing YAML frontmatter
This feature allows you to parse YAML frontmatter in a Markdown file. The code sample demonstrates how to use the remark-frontmatter plugin to parse YAML frontmatter and process the Markdown content.
const remark = require('remark');
const frontmatter = require('remark-frontmatter');
const markdown = `---
title: Example Title
date: 2023-10-01
---
# Hello World
`;
remark()
.use(frontmatter, ['yaml'])
.process(markdown, function (err, file) {
if (err) throw err;
console.log(String(file));
});
Parsing TOML frontmatter
This feature allows you to parse TOML frontmatter in a Markdown file. The code sample demonstrates how to use the remark-frontmatter plugin to parse TOML frontmatter and process the Markdown content.
const remark = require('remark');
const frontmatter = require('remark-frontmatter');
const markdown = `+++
title = "Example Title"
date = "2023-10-01"
+++
# Hello World
`;
remark()
.use(frontmatter, ['toml'])
.process(markdown, function (err, file) {
if (err) throw err;
console.log(String(file));
});
Parsing multiple types of frontmatter
This feature allows you to parse multiple types of frontmatter (e.g., YAML and TOML) in a Markdown file. The code sample demonstrates how to use the remark-frontmatter plugin to parse both YAML and TOML frontmatter and process the Markdown content.
const remark = require('remark');
const frontmatter = require('remark-frontmatter');
const markdown = `---
title: Example Title
date: 2023-10-01
---
# Hello World
`;
remark()
.use(frontmatter, ['yaml', 'toml'])
.process(markdown, function (err, file) {
if (err) throw err;
console.log(String(file));
});
gray-matter is a popular package for parsing frontmatter from strings or files. It supports YAML, TOML, and JSON frontmatter. Compared to remark-frontmatter, gray-matter is more versatile as it can be used outside of the remark ecosystem and provides more features for handling frontmatter data.
front-matter is a simple package for parsing YAML frontmatter from strings. It is lightweight and easy to use but does not support TOML or JSON frontmatter. Compared to remark-frontmatter, it is more limited in scope but can be a good choice for projects that only need to handle YAML frontmatter.
markdown-it-front-matter is a plugin for the markdown-it parser that allows you to extract frontmatter from Markdown files. It supports YAML frontmatter and integrates well with the markdown-it ecosystem. Compared to remark-frontmatter, it is designed specifically for use with markdown-it rather than remark.
remark plugin to support frontmatter (YAML, TOML, and more).
This plugin is affected by the new parser in remark
(micromark
,
see remarkjs/remark#536
).
Use version 2 while you’re still on remark 12.
Use version 3 for remark 13+.
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install remark-frontmatter
Say we have the following file, example.md
:
+++
title = "New Website"
+++
# Other markdown
And our module, example.js
, looks as follows:
import {readSync} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkFrontmatter from 'remark-frontmatter'
import remarkStringify from 'remark-stringify'
const file = readSync('example.md')
unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter, ['yaml', 'toml'])
.use(() => (tree) => {
console.dir(tree)
})
.process(file)
.then((file) => {
console.error(reporter(file))
console.log(String(file))
})
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: {
start: {line: 1, column: 1, offset: 0},
end: {line: 6, column: 1, offset: 48}
}
}
example.md: no issues found
+++
title = "New Website"
+++
# Other markdown
This package exports no identifiers.
The default export is remarkFrontmatter
.
unified().use(remarkFrontmatter[, options])
Configures remark so that it can parse and serialize frontmatter (YAML, TOML, and more).
options
See micromark-extension-frontmatter
for a description of options
.
Use of remark-frontmatter
does not involve rehype
(hast) or user content so there are no openings for
cross-site scripting (XSS) attacks.
remark-gfm
— GitHub Flavored Markdownremark-footnotes
— Footnotesremark-math
— Mathremark-github
— Auto-link references like in GitHub issues, PRs, and commentsremark-yaml-config
— Configure remark from YAML configurationSee 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.
FAQs
remark plugin to support frontmatter (yaml, toml, and more)
The npm package remark-frontmatter receives a total of 1,019,556 weekly downloads. As such, remark-frontmatter popularity was classified as popular.
We found that remark-frontmatter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.