npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gray-matter-webpack-build

v2.1.1

Published

Parse front-matter from a string or file. 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. Used by metalsmith, assemble, verb and

Downloads

1

Readme

  • This project is forked from https://github.com/jonschlinkert/gray-matter.
  • CoffeeScript support removed version for Webpack build compatibility.
  • Forked gray-matter version is 2.1.1

gray-matter NPM version NPM monthly downloads NPM total downloads Linux Build Status

Parse front-matter from a string or file. 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. Used by metalsmith, assemble, verb and many other projects.

Install

Install with npm:

$ npm install --save gray-matter

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

Highlights

  • Reliable and battle-tested by metalsmith, assemble, verb, and many other projects!
  • Extracts and parses:
  • Easy to add additional parsers! pull requests welcome!

Usage

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

Returns:

{ 
  orig: '---\ntitle: Front Matter\n---\nThis is content.',
  data: { title: 'Front Matter' },
  content: '\nThis is content.' 
}

That's it! Just pass a string and gray-matter returns an object.


API

matter

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

Example

matter('---\ntitle: foo\n---\nbar');
//=> {data: {title: 'foo'}, content: 'bar', orig: '---\ntitle: foo\n---\nbar'}

Params

  • string {String}: The string to parse.
  • options {Object}
  • delims {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

.read

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

Example

matter.read('home.md');

Params

  • fp {String}: file path of the file to read.
  • options {Object}: Options to pass to gray-matter.
  • returns {Object}

.stringify

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

Results in:

Examples

matter.stringify('foo bar baz', {title: 'Home'});
---
title: Home
---
foo bar baz

Params

  • 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}

Options

All methods exposed on the API accept an options object passed as the last argument

options.parser

Type: Function

Default: undefined

Pass a custom parser on the options. This is useful if you need to, for example, define custom schemas for [js-yaml].

Example

matter(str, {
  parser: require('js-yaml').safeLoad
});

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.

However, if you are aware and you only use front-matter on, say, blog posts for a static site... this feature can be pretty useful.

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:

// format delims as a string
matter.read('file.md', {delims: '~~~'});
// or an array (open/close)
matter.read('file.md', {delims: ['~~~', '~~~']});

would parse:

Example usage

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

---
title: YAML Front matter
description: This is a page
---
<h1></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></h1>",
  "original": "---\ntitle: YAML Front matter\n---\n<h1></h1>"
}

Benchmarks

Benchmarks for building the bootstrap-blog

gray-matter would process all markdown posts in the bootstrap-blog 20 times before the front-matter library finished processing it once.

front-matter.js x 271 ops/sec ±2.68% (80 runs sampled)
gray-matter.js x 4,294 ops/sec ±0.86% (91 runs sampled)

Misc

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

#1: complex
  front-matter x 338 ops/sec ±1.60% (85 runs sampled)
  gray-matter x 10,608 ops/sec ±1.97% (86 runs sampled)

#2: empty
  front-matter x 5,755,004 ops/sec ±0.88% (94 runs sampled)
  gray-matter x 15,157,998 ops/sec ±0.81% (95 runs sampled)

#3: matter
  front-matter x 10,256 ops/sec ±2.18% (92 runs sampled)
  gray-matter x 202,026 ops/sec ±0.71% (93 runs sampled)

#4: no-content
  front-matter x 10,136 ops/sec ±2.00% (91 runs sampled)
  gray-matter x 206,548 ops/sec ±1.16% (94 runs sampled)

#5: no-matter
  front-matter x 3,540,817 ops/sec ±0.68% (95 runs sampled)
  gray-matter x 7,959,809 ops/sec ±0.73% (91 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 non-front-matter fenced code blocks that contain examples of YAML front matter. Other parsers fail on this.
  • Should return an object with three properties:
    • data: the parsed YAML front matter, as a JSON object
    • content: the contents as a string, without the front matter
    • orig: the "original" content

About

Related projects

  • assemble: Get the rocks out of your socks! Assemble makes you fast at creating web projects… more | homepage
  • metalsmith: An extremely simple, pluggable static site generator. | homepage
  • verb: Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

| Commits | Contributor | | --- | --- | | 121 | jonschlinkert | | 7 | RobLoach | | 2 | doowb | | 2 | moozzyk | | 1 | Ajedi32 | | 1 | ianstormtaylor |

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb-generate-readme, v0.2.0, on October 25, 2016.