Socket
Socket
Sign inDemoInstall

gray-matter

Package Overview
Dependencies
13
Maintainers
2
Versions
55
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    gray-matter

Front-matter parsing done right. Fast, reliable and easy to use. Parses YAML front matter by default, but also has support for YAML, JSON, TOML or Coffee Front-Matter, with options to set custom delimiters.


Version published
Weekly downloads
1.4M
decreased by-4.16%
Maintainers
2
Install size
802 kB
Created
Weekly downloads
 

Package description

What is gray-matter?

The gray-matter npm package is used to parse front-matter from strings or files. Front-matter is metadata stored at the top of a file, typically in YAML or JSON format, which is often used in static site generators and other content management systems.

What are gray-matter's main functionalities?

Parsing Front-Matter

This feature allows you to parse front-matter from a string. The front-matter is typically written in YAML or JSON format and is separated from the content by triple dashes (---). The parsed object contains the metadata and the content separately.

const matter = require('gray-matter');
const file = '---\ntitle: Hello World\ndate: 2023-10-01\n---\nThis is the content of the file.';
const parsed = matter(file);
console.log(parsed);

Stringify Front-Matter

This feature allows you to convert a content string and a metadata object back into a string with front-matter. This is useful for generating files with front-matter programmatically.

const matter = require('gray-matter');
const data = { title: 'Hello World', date: '2023-10-01' };
const content = 'This is the content of the file.';
const stringified = matter.stringify(content, data);
console.log(stringified);

Custom Delimiters

This feature allows you to specify custom delimiters for the front-matter. By default, gray-matter uses triple dashes (---), but you can change this to any string you prefer.

const matter = require('gray-matter');
const file = '+++\ntitle = "Hello World"\ndate = "2023-10-01"\n+++\nThis is the content of the file.';
const parsed = matter(file, { delimiters: '+++' });
console.log(parsed);

Other packages similar to gray-matter

Readme

Source

gray-matter NPM version

Front-matter parsing done right. Fast, reliable and easy to use. Parses YAML front matter by default, but also has support for YAML, JSON, TOML or Coffee Front-Matter, with options to set custom delimiters.

See the benchmarks. gray-matter is 15-30x faster than front-matter.

Highlights

  • Reliable and battle-tested with assemble, verb, and many other projects!
  • Will extract and parse:
  • Easy to add additional parsers! pull requests welcome!
Example TOC

Install with npm

npm i gray-matter --save

Install with bower

bower install gray-matter --save

Usage

var matter = require('gray-matter');
matter('---\ntitle: Front Matter\n---\nThis is content.');

API

matter

Parses a string of front-matter with the given options, and returns an object.

  • string {String}: The string to parse.
  • options {Object}
    • delim {Array}: Custom delimiters formatted as an array. The default is ['---', '---'].
    • parser {Function}: Parser function to use. js-yaml is the default.
  • returns {Object}: Valid JSON
matter('---\ntitle: foo\n---\nbar');
//=> {data: {title: 'foo'}, content: 'bar', orig: '---\ntitle: foo\n---\nbar'}

.read

Read a file and parse front matter. Returns the same object as matter().

  • fp {String}: file path of the file to read.
  • options {Object}: Options to pass to gray-matter.
  • returns: {Object}
matter.read('home.md');

.stringify

Stringify an object to front-matter-formatted YAML, and concatenate it to the given string.

  • str {String}: The content string to append to stringified front-matter.
  • data {Object}: Front matter to stringify.
  • options {Object}: Options to pass to js-yaml
  • returns: {String}
matter.stringify('foo bar baz', {title: 'Home'});

Results in:

---
title: Home
---
foo bar baz

Options

All methods accept an options object passed as the last argument

options.eval

Type: Boolean

Default: false

Evaluate coffee-script, CSON or JavaScript in front-matter. If you aren't aware of the dangers, google is your friend.

options.lang

Type: String

Default: yaml

The parser to use on the extracted front matter.

YAML is parsed by default, and the languages listed below are parsed automatically if the language is specified after the first delimiter (e.g. ---).

Valid languages are:

  • yaml
  • json
  • coffee
  • cson
  • toml
  • js|javascript

Example

To parse coffee front matter, you would define it as follows:

---coffee
title: 'coffee functions'
user: 'jonschlinkert'
fn:
  reverse = (src) ->
    src.split('').reverse().join('')
---

<%= description %>
<%= reverse(user) %>

options.delims

Type: String

Default: ---

Open and close delimiters can be passed in as an array of strings.

Example:

matter.read('file.md', {delim: '~~~'});

would parse:

~~~
title: Home
~~~
This is the {{title}} page.

Example

Given we have a page, abc.html, containing:

---
title: YAML Front matter
description: This is a page
---
<h1>{{title}}</h1>

then running the following in the command line:

matter('abc.html');

returns

{
  "data": {
    "title": "YAML Front matter",
    "description": "This is a page"
  },
  "content": "<h1>{{title}}</h1>",
  "original": "---\ntitle: YAML Front matter\n---\n<h1>{{title}}</h1>"
}

Benchmarks

Blog

The following benchmarks reflect the processing time for all markdown posts in the bootstrap-blog.

front-matter.js x 209 ops/sec ±2.75% (78 runs sampled)
gray-matter.js x 3,792 ops/sec ±0.81% (93 runs sampled)

Misc

gray-matter is 12-20x faster than [front-matter] when content or front matter actually exist.

#1: complex.js
  front-matter.js x 425 ops/sec ±1.52% (90 runs sampled)
  gray-matter.js x 6,629 ops/sec ±1.30% (91 runs sampled)

#2: empty.js
  front-matter.js x 5,224,394 ops/sec ±0.96% (97 runs sampled)
  gray-matter.js x 10,491,590 ops/sec ±0.66% (98 runs sampled)

#3: matter.js
  front-matter.js x 9,065 ops/sec ±3.33% (80 runs sampled)
  gray-matter.js x 186,766 ops/sec ±0.99% (95 runs sampled)

#4: no-content.js
  front-matter.js x 10,037 ops/sec ±3.17% (84 runs sampled)
  gray-matter.js x 163,841 ops/sec ±0.91% (95 runs sampled)

#5: no-matter.js
  front-matter.js x 5,112,736 ops/sec ±0.89% (96 runs sampled)
  gray-matter.js x 8,239,841 ops/sec ±1.39% (92 runs sampled)

Why?

Why another YAML Front Matter library?

Because other libraries we tried failed to meet our requirements with Assemble. Some most of the libraries met most of the requirements, but none had all of them. Here are the most important:

  • Be usable, if not simple
  • Allow custom delimiters
  • Use a dependable and well-supported library for parsing YAML and other languages
  • Don't fail when no content exists
  • Don't fail when no front matter exists
  • Have no problem reading YAML files directly
  • Have no problem with complex content, including fenced code blocks that contain examples of YAML front matter. Other parsers fail on this.
  • Should return an object that contains the parsed YAML front matter and content, as well as the "original" content.

Run tests

npm test

Authors

Jon Schlinkert

License

Copyright (c) 2015 Jon Schlinkert
Released under the MIT license


This file was generated by verb on January 16, 2015.

Keywords

FAQs

Last updated on 16 Jan 2015

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc