gray-matter
Advanced tools
Comparing version 1.1.2 to 1.2.0
100
index.js
@@ -24,3 +24,3 @@ 'use strict'; | ||
* @param {Object} `options` | ||
* @option {Array} [options] `delim` Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
* @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. | ||
@@ -36,47 +36,46 @@ * @return {Object} Valid JSON | ||
var delim = options && options.delim || '---'; | ||
str = stripBom(str); | ||
// default results to build up | ||
var res = {orig: str, data: {}, content: str}; | ||
if (str === '' || str.indexOf(delim) !== 0) { | ||
if (str === '') { | ||
return res; | ||
} | ||
var opts = options || {}; | ||
opts.lang = opts.lang || 'yaml'; | ||
var delimLen = delim.length; | ||
// delimiters | ||
var delims = arrayify((options && options.delims) || '---'); | ||
var a = delims[0]; | ||
// find the index of the next fence | ||
var end = str.indexOf(delim, delimLen); | ||
if (end === -1) { | ||
// strip byte order marks | ||
str = stripBom(str); | ||
// if the first delim isn't the first thing, return | ||
if (!isFirst(str, a)) { | ||
return res; | ||
} | ||
// start the character search after the first fence | ||
var ch = delimLen, l = '', lang = ''; | ||
var len = str.length; | ||
var b = delims[1] || delims[0]; | ||
var alen = a.length; | ||
// detect the language, if any, after the first fence | ||
while ((l = str.charAt(ch++)) !== '\n') { | ||
lang += l; | ||
if (ch === end) { | ||
throw new Error('[gray-matter]: bad formatting, no newlines detected.'); | ||
} | ||
// find the index of the next delimiter before | ||
// going any further. If not found, return. | ||
var end = str.indexOf(b, alen + 1); | ||
if (end === -1) { | ||
return res; | ||
} | ||
// store the length of the actual string following the fence, | ||
// since we need this to continue on to the data block | ||
var langLength = lang.length; | ||
// detect a language, if defined | ||
var lang = str.slice(alen, str.indexOf('\n')); | ||
// measure the lang before trimming whitespace | ||
var start = alen + lang.length; | ||
// format the language to use for parsing | ||
lang = lang.trim(); | ||
lang = (lang ? lang : opts.lang).toLowerCase(); | ||
var opts = options || {}; | ||
opts.lang = opts.lang || 'yaml'; | ||
lang = (lang && lang.trim()) || opts.lang; | ||
// if it exists, `data` is a string at this point | ||
var data = str.slice(delimLen + langLength, end).trim(); | ||
if (data.length > 0) { | ||
// get the front matter (data) string | ||
var data = str.slice(start, end).trim(); | ||
if (data) { | ||
// if data exists, see if we have a matching parser | ||
var fn = opts.parser || parsers[lang]; | ||
if (typeof fn === 'function') { | ||
// run the parser on the data string | ||
res.data = fn(data, opts); | ||
@@ -88,3 +87,3 @@ } else { | ||
res.content = str.substr(end + delimLen).trim(); | ||
res.content = str.substr(end + b.length); | ||
return res; | ||
@@ -153,8 +152,8 @@ } | ||
matter.stringify = function(str, data, options) { | ||
var delims = arrayify(options && options.delims || '---'); | ||
var res = ''; | ||
res += '---\n'; | ||
res += delims[0] + '\n'; | ||
res += YAML.safeDump(data, options); | ||
res += '---\n'; | ||
res += str; | ||
res += '\n'; | ||
res += (delims[1] || delims[0]) + '\n'; | ||
res += str + '\n'; | ||
return res; | ||
@@ -172,7 +171,16 @@ }; | ||
matter.test = function(str, options) { | ||
var delim = options && options.delim || '---'; | ||
return str.slice(0, delim.length) === delim; | ||
var delims = arrayify(options && options.delims || '---'); | ||
return isFirst(str, delims[0]); | ||
}; | ||
/** | ||
* Return true if the given `ch` the first | ||
* thing in the string. | ||
*/ | ||
function isFirst(str, ch) { | ||
return str.substr(0, ch.length) === ch; | ||
} | ||
/** | ||
* Utility to strip byte order marks | ||
@@ -182,9 +190,15 @@ */ | ||
function stripBom(str) { | ||
var hasBom = str.charCodeAt(0) === 65279 | ||
&& str.charCodeAt(1) === 116 | ||
&& str.charCodeAt(2) === 104; | ||
return str.charAt(0) === '\uFEFF' | ||
? str.slice(1) | ||
: str; | ||
} | ||
if (hasBom) return str.slice(1); | ||
return str; | ||
/** | ||
* Typecast `val` to an array. | ||
*/ | ||
function arrayify(val) { | ||
return !Array.isArray(val) | ||
? [val] | ||
: val; | ||
} | ||
{ | ||
"name": "gray-matter", | ||
"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.1.2", | ||
"description": "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.", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -25,12 +25,8 @@ "name": "Jon Schlinkert", | ||
"scripts": { | ||
"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", | ||
"docs": "update && license && npmignore && deps && verb", | ||
"all": "npm run test && npm run docs" | ||
"test": "mocha -R spec" | ||
}, | ||
"dependencies": { | ||
"chalk": "^0.5.1", | ||
"extend-shallow": "^0.1.1", | ||
"js-yaml": "^3.2.3" | ||
"extend-shallow": "^0.2.0", | ||
"js-yaml": "^3.2.5" | ||
}, | ||
@@ -37,0 +33,0 @@ "devDependencies": { |
# gray-matter [![NPM version](https://badge.fury.io/js/gray-matter.svg)](http://badge.fury.io/js/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. | ||
> 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. | ||
@@ -20,5 +20,2 @@ See the [benchmarks](#benchmarks). gray-matter is 15-30x faster than [front-matter](). | ||
#### Example TOC | ||
<!-- toc --> | ||
## Install with [npm](npmjs.org) | ||
@@ -35,2 +32,3 @@ | ||
## Usage | ||
@@ -43,3 +41,18 @@ | ||
Returns: | ||
```js | ||
{ | ||
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 | ||
@@ -52,3 +65,3 @@ ### [matter](index.js#L30) | ||
* `options` **{Object}** | ||
- `delim` **{Array}**: Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
- `delims` **{Array}**: Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
- `parser` **{Function}**: Parser function to use. [js-yaml] is the default. | ||
@@ -63,3 +76,3 @@ | ||
### [.read](index.js#L118) | ||
### [.read](index.js#L117) | ||
@@ -76,3 +89,3 @@ Read a file and parse front matter. Returns the same object as `matter()`. | ||
### [.stringify](index.js#L149) | ||
### [.stringify](index.js#L148) | ||
@@ -103,3 +116,3 @@ Stringify an object to front-matter-formatted YAML, and concatenate it to the given string. | ||
> All methods accept an options object passed as the last argument | ||
> All methods exposed on the API accept an options object passed as the last argument | ||
@@ -111,5 +124,7 @@ ## options.eval | ||
Evaluate coffee-script, CSON or JavaScript in front-matter. If you aren't aware of the dangers, google is your friend. | ||
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 | ||
@@ -160,7 +175,11 @@ Type: `String` | ||
```js | ||
matter.read('file.md', {delim: '~~~'}); | ||
// format delims as a string | ||
matter.read('file.md', {delims: '~~~'}); | ||
// or an array (open/close) | ||
matter.read('file.md', {delims: ['~~~', '~~~']}); | ||
``` | ||
would parse: | ||
```html | ||
<pre> | ||
~~~ | ||
@@ -170,6 +189,6 @@ title: Home | ||
This is the {{title}} page. | ||
``` | ||
</pre> | ||
## Example | ||
## Example usage | ||
@@ -221,20 +240,20 @@ Given we have a page, `abc.html`, containing: | ||
#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) | ||
front-matter.js x 433 ops/sec ±1.21% (91 runs sampled) | ||
gray-matter.js x 9,491 ops/sec ±1.07% (92 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) | ||
front-matter.js x 5,744,976 ops/sec ±0.76% (99 runs sampled) | ||
gray-matter.js x 18,048,669 ops/sec ±0.84% (93 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) | ||
front-matter.js x 10,739 ops/sec ±2.65% (84 runs sampled) | ||
gray-matter.js x 201,322 ops/sec ±0.71% (93 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) | ||
front-matter.js x 13,097 ops/sec ±3.00% (82 runs sampled) | ||
gray-matter.js x 198,441 ops/sec ±0.49% (101 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) | ||
front-matter.js x 5,420,088 ops/sec ±0.79% (96 runs sampled) | ||
gray-matter.js x 9,559,091 ops/sec ±1.33% (92 runs sampled) | ||
``` | ||
@@ -270,4 +289,4 @@ | ||
+ [github/assemble](https://github.com/assemble) | ||
+ [twitter/assemble](http://twitter.com/assemble) | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
@@ -280,3 +299,3 @@ ## License | ||
_This file was generated by [verb](https://github.com/assemble/verb) on January 16, 2015._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on February 06, 2015._ | ||
@@ -283,0 +302,0 @@ |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
86236
79
1817
294
26
2
+ Addedextend-shallow@0.2.0(transitive)
- Removedextend-shallow@0.1.1(transitive)
Updatedextend-shallow@^0.2.0
Updatedjs-yaml@^3.2.5