gray-matter
Advanced tools
Comparing version 0.6.2 to 1.0.0
84
.verb.md
@@ -5,7 +5,5 @@ # {%= name %} {%= badge("fury") %} | ||
Used by [assemble](https://github.com/assemble/assemble), [verb](https://github.com/assemble/verb), and many other projects! | ||
## Highlights | ||
* Reliable and battle-tested. | ||
* Reliable and battle-tested with [assemble](https://github.com/assemble/assemble), [verb](https://github.com/assemble/verb), and many other projects! | ||
* Will extract and parse: | ||
@@ -24,5 +22,4 @@ * [YAML](http://github.com/nodeca/js-yaml) | ||
## Install | ||
{%= include("install-npm", {save: true}) %} | ||
{%= include("install-bower") %} | ||
{%= include("install-bower", {save: true}) %} | ||
@@ -33,6 +30,11 @@ ## Usage | ||
var matter = require('gray-matter'); | ||
console.log(matter('---\ntitle: foo\n---\nbar'); | ||
//=> {data: {title: 'foo'}, content: 'bar', orig: '---\ntitle: foo\n---\nbar'} | ||
matter('---\ntitle: Front Matter\n---\nThis is content.'); | ||
``` | ||
## Run tests | ||
```bash | ||
npm test | ||
``` | ||
## API | ||
@@ -43,4 +45,5 @@ {%= comments("index.js") %} | ||
## Options | ||
> All methods will accept an options object to be passed as a second parameter | ||
> All methods accept an options object passed as the last argument | ||
## options.eval | ||
@@ -53,2 +56,3 @@ Type: `Boolean` | ||
## options.lang | ||
@@ -59,4 +63,8 @@ Type: `String` | ||
The parser to use on the extracted front matter. Valid options include: | ||
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` | ||
@@ -69,2 +77,19 @@ * `json` | ||
**Example** | ||
To parse coffee front matter, you would define it as follows: | ||
```js | ||
---coffee | ||
title: 'coffee functions' | ||
user: 'jonschlinkert' | ||
fn: | ||
reverse = (src) -> | ||
src.split('').reverse().join('') | ||
--- | ||
<%= description %> | ||
<%= reverse(user) %> | ||
``` | ||
## options.delims | ||
@@ -75,13 +100,23 @@ Type: `Object` | ||
Open and close delimiters can be passed in as an array of strings. Example: | ||
Open and close delimiters can be passed in as an array of strings. | ||
**Example:** | ||
```js | ||
matter.read('file.md', {delims: ['~~~', '~~~']}); | ||
``` | ||
would parse: | ||
```html | ||
~~~ | ||
title: Home | ||
~~~ | ||
This is the {{title}} page. | ||
``` | ||
## Examples | ||
Let's say our page, `foo.html` contains | ||
## Example | ||
Given we have a page, `abc.html`, containing: | ||
```html | ||
@@ -98,3 +133,3 @@ --- | ||
```js | ||
console.log(matter('foo.html')); | ||
matter('abc.html'); | ||
``` | ||
@@ -113,15 +148,4 @@ returns | ||
``` | ||
and | ||
```js | ||
console.log(matter('foo.html').data); | ||
``` | ||
returns | ||
```json | ||
{"title": "YAML Front matter", "description": "This is a page"} | ||
``` | ||
## Why? | ||
@@ -142,15 +166,3 @@ | ||
## Changelog | ||
**v0.6.0 breaking changes!** | ||
**v0.5.0 breaking changes!** | ||
* YAML is now parsed using the `.safeLoad()` method from [js-yaml](http://github.com/nodeca/js-yaml). | ||
* To parse coffee, CSON or javascript front matter, you must set `options.eval` to true. | ||
* `stringify()` has been renamed to `toJSON()` | ||
* `stringifyYAML()` has been renamed to `toYAML()` | ||
## Authors | ||
@@ -157,0 +169,0 @@ {%= include("author") %} |
{ | ||
"name": "gray-matter", | ||
"version": "0.5.3", | ||
"version": "1.0.0", | ||
"main": [ | ||
"index.js" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Jon Schlinkert", | ||
"homepage": "https://github.com/jonschlinkert" | ||
} | ||
] | ||
} |
81
index.js
@@ -5,6 +5,28 @@ 'use strict'; | ||
var extend = require('extend-shallow'); | ||
var parsers = require('./lib/parsers'); | ||
/** | ||
* Expose `matter()` | ||
* | ||
* @type {Function} | ||
*/ | ||
module.exports = matter; | ||
/** | ||
* Parses a `string` of front-matter with the given `options`, | ||
* and returns an object. | ||
* | ||
* ```js | ||
* matter('---\ntitle: foo\n---\nbar'); | ||
* //=> {data: {title: 'foo'}, content: 'bar', orig: '---\ntitle: foo\n---\nbar'} | ||
* ``` | ||
* | ||
* @param {String} `string` The string to parse. | ||
* @param {Object} `options` | ||
* @option {Array} [options] `delims` Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
* @option {Function} [options] `parser` Parser function to use. [js-yaml] is the default. | ||
* @return {Object} Valid JSON | ||
* @api public | ||
*/ | ||
function matter(str, options) { | ||
@@ -38,12 +60,10 @@ if (typeof str !== 'string') { | ||
o.lang = (o.lang || opts.lang).trim(); | ||
var fn = parsers[o.lang]; | ||
var fn = opts.parser || parsers[o.lang]; | ||
if (fn) { | ||
o.data = fn(str.substr(len1 + ll, to - ll - len2), opts); | ||
} else { | ||
o.data = str.substr(len1 + ll, to - ll - len2); | ||
throw new Error('gray-matter cannot find a parser for: ' + str); | ||
} | ||
if (typeof o.data === 'string') { | ||
throw new Error('gray-matter cannot parse: ' + str); | ||
} | ||
return { | ||
@@ -56,2 +76,24 @@ orig: o.orig, | ||
/** | ||
* Expose `parsers` | ||
* | ||
* @type {Object} | ||
*/ | ||
var parsers = matter.parsers = require('./lib/parsers'); | ||
/** | ||
* Read a file and parse front matter. Returns the same object | ||
* as `matter()`. | ||
* | ||
* ```js | ||
* matter.read('home.md'); | ||
* ``` | ||
* | ||
* @param {String} `fp` file path of the file to read. | ||
* @param {Object} `options` Options to pass to gray-matter. | ||
* @return {Object} | ||
* @api public | ||
*/ | ||
matter.read = function(fp, options) { | ||
@@ -65,7 +107,30 @@ var str = fs.readFileSync(fp, 'utf8'); | ||
/** | ||
* Stringify an object to front-matter-formatted YAML, and | ||
* concatenate it to the given string. | ||
* | ||
* ```js | ||
* matter.stringify('foo bar baz', {title: 'Home'}); | ||
* ``` | ||
* Results in: | ||
* | ||
* ```yaml | ||
* --- | ||
* title: Home | ||
* --- | ||
* foo bar baz | ||
* ``` | ||
* | ||
* @param {String} `str` The content string to append to stringified front-matter. | ||
* @param {Object} `data` Front matter to stringify. | ||
* @param {Object} `options` Options to pass to js-yaml | ||
* @return {String} | ||
* @api public | ||
*/ | ||
matter.stringify = function(str, data, options) { | ||
var yaml = require('js-yaml'); | ||
var YAML = matter.parsers.requires.yaml || (matter.parsers.requires.yaml = require('js-yaml')); | ||
var res = ''; | ||
res += '---\n'; | ||
res += yaml.safeDump(data, options); | ||
res += YAML.safeDump(data, options); | ||
res += '---\n'; | ||
@@ -72,0 +137,0 @@ res += str; |
@@ -15,3 +15,2 @@ /** | ||
var log = require('verbalize'); | ||
var extend = require('extend-shallow'); | ||
@@ -31,3 +30,3 @@ | ||
var requires = {}; | ||
parser.requires = {}; | ||
@@ -47,6 +46,6 @@ | ||
try { | ||
var YAML = requires.yaml || (requires.yaml = require('js-yaml')); | ||
var YAML = parser.requires.yaml || (parser.requires.yaml = require('js-yaml')); | ||
data = YAML.safeLoad(str, options); | ||
} catch (err) { | ||
log.error(' [gray-matter] json:', log.bold(err)); | ||
throw new Error('gray-matter parser [js-yaml]:' + err); | ||
} | ||
@@ -70,3 +69,3 @@ return data; | ||
} catch (err) { | ||
log.error(' [gray-matter] json:', log.bold(err)); | ||
throw new Error('gray-matter cannot parse JSON: ' + str); | ||
} | ||
@@ -113,3 +112,2 @@ return data; | ||
} | ||
var data = {}; | ||
@@ -119,8 +117,7 @@ try { | ||
} catch (err) { | ||
log.error(' [gray-matter] javascript:', log.bold(err)); | ||
throw new Error('gray-matter parser [javascript]:' + err); | ||
} | ||
return data; | ||
} else { | ||
var msg = 'to parse javascript, you must set `options.eval` to `true`'; | ||
log.error(' [gray-matter] javascript:', log.bold(msg)); | ||
throw new Error('gray-matter: to parse javascript set `options.eval` to `true`'); | ||
} | ||
@@ -154,10 +151,9 @@ }; | ||
try { | ||
var coffee = requires.coffee || (requires.coffee = require('coffee-script')); | ||
var coffee = parser.requires.coffee || (parser.requires.coffee = require('coffee-script')); | ||
return coffee['eval'](str, options); | ||
} catch (e) { | ||
log.error(' [gray-matter] coffee-script:', log.bold(e)); | ||
} catch (err) { | ||
throw new Error('gray-matter parser [coffee-script]:' + err); | ||
} | ||
} else { | ||
var msg = 'to parse Coffee-Script, you must set `options.eval` to `true`'; | ||
log.error(' [gray-matter] coffee:', log.bold(msg)); | ||
throw new Error('gray-matter: to parse coffee set `options.eval` to `true`'); | ||
} | ||
@@ -187,7 +183,7 @@ }; | ||
try { | ||
var toml = requires.toml || (requires.toml = require('toml')); | ||
var toml = parser.requires.toml || (parser.requires.toml = require('toml')); | ||
return toml.parse(str.trim()); | ||
} catch (e) { | ||
log.error(' [gray-matter] TOML:', log.bold(e)); | ||
} catch (err) { | ||
throw new Error('gray-matter parser [toml]:' + err); | ||
} | ||
}; |
{ | ||
"name": "gray-matter", | ||
"description": "A simple to use and extend front matter library. Supports parsing and extracting YAML, JSON, TOML or Coffee Front-Matter, with options to set custom delimiters.", | ||
"version": "0.6.2", | ||
"description": "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": "1.0.0", | ||
"author": { | ||
@@ -24,21 +24,25 @@ "name": "Jon Schlinkert", | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha" | ||
"test": "mocha -R spec", | ||
"cover": "istanbul cover node_modules/.bin/_mocha && open coverage/lcov-report/index.html", | ||
"ci": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
}, | ||
"dependencies": { | ||
"coffee-script": "^1.8.0", | ||
"delims": "^0.4.0", | ||
"extend-shallow": "^0.1.1", | ||
"js-yaml": "^3.2.2", | ||
"lodash": "^2.4.1", | ||
"toml": "^2.0.6", | ||
"verbalize": "^0.1.2" | ||
"js-yaml": "^3.2.3" | ||
}, | ||
"devDependencies": { | ||
"benchmarked": "^0.1.3", | ||
"coffee-script": "^1.8.0", | ||
"delims": "^0.4.0", | ||
"glob": "^4.2.1", | ||
"gulp": "^3.8.10", | ||
"gulp-istanbul": "^0.5.0", | ||
"gulp-mocha": "^2.0.0", | ||
"lodash": "^2.4.1", | ||
"mocha": "^1.19.0", | ||
"should": "^4.0.4" | ||
"should": "^4.3.0", | ||
"toml": "^2.0.6" | ||
}, | ||
@@ -60,2 +64,3 @@ "keywords": [ | ||
"generator", | ||
"gh-pages", | ||
"javascript", | ||
@@ -62,0 +67,0 @@ "jekyll", |
137
README.md
# gray-matter [![NPM version](https://badge.fury.io/js/gray-matter.svg)](http://badge.fury.io/js/gray-matter) | ||
> A simple to use and extend front matter library. Supports parsing and extracting YAML, JSON, TOML or Coffee Front-Matter, with options to set custom delimiters. | ||
> 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. | ||
Used by [assemble](https://github.com/assemble/assemble), [verb](https://github.com/assemble/verb), and many other projects! | ||
## Highlights | ||
* Reliable and battle-tested. | ||
* Reliable and battle-tested with [assemble](https://github.com/assemble/assemble), [verb](https://github.com/assemble/verb), and many other projects! | ||
* Will extract and parse: | ||
@@ -23,3 +21,2 @@ * [YAML](http://github.com/nodeca/js-yaml) | ||
## Install | ||
## Install with [npm](npmjs.org) | ||
@@ -33,3 +30,3 @@ | ||
```bash | ||
bower install gray-matter --save-dev | ||
bower install gray-matter --save | ||
``` | ||
@@ -41,13 +38,68 @@ | ||
var matter = require('gray-matter'); | ||
console.log(matter('---\ntitle: foo\n---\nbar'); | ||
//=> {data: {title: 'foo'}, content: 'bar', orig: '---\ntitle: foo\n---\nbar'} | ||
matter('---\ntitle: Front Matter\n---\nThis is content.'); | ||
``` | ||
## Run tests | ||
```bash | ||
npm test | ||
``` | ||
## API | ||
### [matter](index.js#L31) | ||
Parses a `string` of front-matter with the given `options`, and returns an object. | ||
* `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 | ||
```js | ||
matter('---\ntitle: foo\n---\nbar'); | ||
//=> {data: {title: 'foo'}, content: 'bar', orig: '---\ntitle: foo\n---\nbar'} | ||
``` | ||
### [.read](index.js#L96) | ||
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} | ||
```js | ||
matter.read('home.md'); | ||
``` | ||
### [.stringify](index.js#L127) | ||
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} | ||
```js | ||
matter.stringify('foo bar baz', {title: 'Home'}); | ||
``` | ||
Results in: | ||
```yaml | ||
--- | ||
title: Home | ||
--- | ||
foo bar baz | ||
``` | ||
## Options | ||
> All methods will accept an options object to be passed as a second parameter | ||
> All methods accept an options object passed as the last argument | ||
## options.eval | ||
@@ -60,2 +112,3 @@ Type: `Boolean` | ||
## options.lang | ||
@@ -66,4 +119,8 @@ Type: `String` | ||
The parser to use on the extracted front matter. Valid options include: | ||
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` | ||
@@ -76,2 +133,19 @@ * `json` | ||
**Example** | ||
To parse coffee front matter, you would define it as follows: | ||
```js | ||
---coffee | ||
title: 'coffee functions' | ||
user: 'jonschlinkert' | ||
fn: | ||
reverse = (src) -> | ||
src.split('').reverse().join('') | ||
--- | ||
<%= description %> | ||
<%= reverse(user) %> | ||
``` | ||
## options.delims | ||
@@ -82,13 +156,23 @@ Type: `Object` | ||
Open and close delimiters can be passed in as an array of strings. Example: | ||
Open and close delimiters can be passed in as an array of strings. | ||
**Example:** | ||
```js | ||
matter.read('file.md', {delims: ['~~~', '~~~']}); | ||
``` | ||
would parse: | ||
```html | ||
~~~ | ||
title: Home | ||
~~~ | ||
This is the {{title}} page. | ||
``` | ||
## Examples | ||
Let's say our page, `foo.html` contains | ||
## Example | ||
Given we have a page, `abc.html`, containing: | ||
```html | ||
@@ -105,3 +189,3 @@ --- | ||
```js | ||
console.log(matter('foo.html')); | ||
matter('abc.html'); | ||
``` | ||
@@ -120,15 +204,4 @@ returns | ||
``` | ||
and | ||
```js | ||
console.log(matter('foo.html').data); | ||
``` | ||
returns | ||
```json | ||
{"title": "YAML Front matter", "description": "This is a page"} | ||
``` | ||
## Why? | ||
@@ -149,15 +222,3 @@ | ||
## Changelog | ||
**v0.6.0 breaking changes!** | ||
**v0.5.0 breaking changes!** | ||
* YAML is now parsed using the `.safeLoad()` method from [js-yaml](http://github.com/nodeca/js-yaml). | ||
* To parse coffee, CSON or javascript front matter, you must set `options.eval` to true. | ||
* `stringify()` has been renamed to `toJSON()` | ||
* `stringifyYAML()` has been renamed to `toYAML()` | ||
## Authors | ||
@@ -176,3 +237,3 @@ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 04, 2014._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 05, 2014._ | ||
@@ -179,0 +240,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
21231
2
13
322
1
233
1
11
- Removedcoffee-script@^1.8.0
- Removeddelims@^0.4.0
- Removedlodash@^2.4.1
- Removedtoml@^2.0.6
- Removedverbalize@^0.1.2
- Removedansi-styles@1.0.0(transitive)
- Removedarray-flatten@2.1.2(transitive)
- Removedarrayify-compact@0.1.1(transitive)
- Removedchalk@0.4.0(transitive)
- Removedcoffee-script@1.12.7(transitive)
- Removeddelims@0.4.2(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedhas-color@0.1.7(transitive)
- Removedis-plain-object@0.1.0(transitive)
- Removedlodash@2.4.2(transitive)
- Removedmixin-deep@0.1.0(transitive)
- Removedobject-pick@0.1.2(transitive)
- Removedstrip-ansi@0.1.1(transitive)
- Removedtoml@2.3.6(transitive)
- Removedverbalize@0.1.2(transitive)
Updatedjs-yaml@^3.2.3