What is front-matter?
The front-matter npm package is used to parse YAML or JSON front matter from strings, typically used in markdown files. This is useful for extracting metadata from content files.
What are front-matter's main functionalities?
Parsing Front Matter
This feature allows you to parse a string containing front matter and extract the metadata and content separately.
const frontMatter = require('front-matter');
const content = '---\ntitle: Hello World\ndate: 2023-10-01\n---\nThis is the content of the file.';
const parsed = frontMatter(content);
console.log(parsed.attributes); // { title: 'Hello World', date: '2023-10-01' }
console.log(parsed.body); // 'This is the content of the file.'
Custom Delimiters
This feature allows you to specify custom delimiters for the front matter, which is useful if your front matter is not delimited by the default '---'.
const frontMatter = require('front-matter');
const content = '+++\ntitle: Hello World\ndate: 2023-10-01\n+++\nThis is the content of the file.';
const options = { delimiters: '+++' };
const parsed = frontMatter(content, options);
console.log(parsed.attributes); // { title: 'Hello World', date: '2023-10-01' }
console.log(parsed.body); // 'This is the content of the file.'
Other packages similar to front-matter
gray-matter
gray-matter is a popular package for parsing front matter from strings. It supports YAML, JSON, and TOML front matter, and offers more features like custom engines and stringifying parsed data back to front matter format. It is more feature-rich compared to front-matter.
node-front-matter
http://en.wikipedia.org/wiki/YAML
Extract YAML front matter from strings.
So you have this file example.md
:
---
title:
description:
---
This is some content
Then you can do this:
var frontmatter = require('front-matter')
, extract = frontmatter(data)
And end up with this:
console.log(extract)
{ attributes: { title: 'example'
, description: 'example description'
}
, body: '\nThis is some content\n'
}