gray-matter
A simple-to-use Front-Matter parsing and extraction Library.
- Use custom delimiters
- Will extract and parse YAML, JSON, or CoffeeScript.
- Easy to add additional parsers!
Quickstart
Install with npm
npm i gray-matter --save
Install with bower
bower install gray-matter --save
Usage
var matter = require('gray-matter');
matter(String, Object);
Methods
matter
By default the matter()
method expects a string. So this:
matter(str);
results in something like:
{
"context": {"foo": "bar"},
"content": "baz",
"original": "---\nfoo: bar\n---\nbaz"
}
matter.read
Read a file from the file system before parsing.
matter.read('file.md');
Returns:
{
"context": {"foo": "bar"},
"content": "baz",
"original": "---\nfoo: bar\n---\nbaz"
}
matter.exists
Returns true
or false
if front matter exists:
matter.exists(str);
matter.extend
Extend and stringify YAML front matter. Takes an object as the second parameter, and returns either the extended, stringified object (YAML), or if no front matter is found an empty string is returned.
matter.extend(str, obj);
matter.recontruct
A convenience wrapper around the matter
and matter.extend
. Extends YAML front matter, then re-assembles front matter with the content of the file.
matter.recontruct(str, obj);
matter.stringify
A convenience wrapper around the matter(str).context
method.
matter.stringify(str);
matter.stringifyYAML
Stringify parsed front matter back to YAML.
matter.stringifyYAML(str);
Options
All methods will accept an options object to be passed as a second paramer
lang
Type: String
Default: yaml
The parser to use on the extracted front matter. Valid options are, yaml
, coffee
and json
.
delims
Type: Object
Default: {delims: ['---', '---']}
Open and close delimiters can be passed in as an array of strings. Example:
matter.read('file.md', {delims: ['~~~', '~~~']});
You may also pass an array of arrays, allowing multiple alternate delimiters to be used. Example:
{
delims: [
['---', '~~~'], ['---', '~~~']
]
}
Note that passing multiple delimiters will yield unpredictable results, so it is recommended that you use this option only for testing purposes.
autodetect
Type: Boolean
Default: undefined
Attempts to automatically register a language that is specified after the first code boundary (delimiter).
Usage Example:
--- coffee
user = 'jonschlinkert'
reverse = (src) ->
src.split('').reverse().join('')
---
{%= user %}
{%= reverse(user) %}
Examples
matter
Let's say our page, foo.html
contains
---
title: YAML Front matter
description: This is a page
---
<h1>{{title}}</h1>
then running the following in the command line:
console.log(matter('foo.html'));
returns
{
"context": {
"title": "YAML Front matter",
"description": "This is a page"
},
"content": "<h1>{{title}}</h1>",
"original": "---\ntitle: YAML Front matter\n---\n<h1>{{title}}</h1>"
}
and
console.log(matter('foo.html').context);
returns
{"title": "YAML Front matter", "description": "This is a page"}
matter.extend
Given this page:
---
title: Gray Matter
---
Hooray!
and this config:
var file = require('fs').readFileSync('file.md', 'utf8');
var obj = {
description: 'A simple to use front matter lib';
};
matter.extend(file, obj);
the result would be:
---
title: Gray Matter
description: A simple to use front matter lib
---
Hooray!
Authors
Jon Schlinkert
Brian Woodward
License
Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors.
Released under the MIT license
This file was generated by grunt-readme on Monday, January 27, 2014.