yfm
A YAML Front-Matter parsing and extraction Library.
npm i yfm --save
Usage
var yfm = require('yfm');
yfm('yfm.html');
Options
You may pass an options object as a second parameter.
custom delimiters
Type: object
Default: {close: '---', open: '---'}
Open and close delimiters can be a string or an array of strings. If an array of strings is passed for a delimiter then all patterns supplied will be used to check for YAML front matter.
Example:
{
close: ['---', '~~~'],
open: ['...', '---']
}
Checks for this:
---
title: Foo
---
this
~~~
title: Foo
---
this
---
title: Foo
...
and this
~~~
title: Foo
...
Passing multiple delimiters will likely provide unpredictable results, but the option is included for testing purposes.
read
Type: boolean
Default: true
Specify whether or not to read a file from the file system. When set to false
a raw string may be passed to the function. Example:
yfm('---\nTitle: YFM\n---\nContent.', {read: false})
Examples
Let's say our page, foo.html
contains
---
title: YAML Front matter
---
<h1>{{title}}</h1>
then running the following in the command line:
console.log(yfm('foo.html'));
returns
{
"context": {
"title": "YAML Front matter"
},
"content": "<h1>{{title}}</h1>",
"original": "---\ntitle: YAML Front matter\n---\n<h1>{{title}}</h1>"
}
and
console.log(yfm('foo.html').context);
returns
{"title": "YAML Front matter"}
Check for YAML front matter
var hasYFM = function (src, options) {
var obj = yfm(src, options).context;
return _.keys(obj).length > 0;
};
Authors
Jon Schlinkert
Brian Woodward
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by grunt-readme on Friday, January 17, 2014.