gray-matter
Advanced tools
Comparing version 1.1.0 to 1.1.1
68
index.js
@@ -24,3 +24,3 @@ 'use strict'; | ||
* @param {Object} `options` | ||
* @option {Array} [options] `delims` Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
* @option {Array} [options] `delim` Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
* @option {Function} [options] `parser` Parser function to use. [js-yaml] is the default. | ||
@@ -36,32 +36,29 @@ * @return {Object} Valid JSON | ||
if (str === '') { | ||
return {orig: '', data: {}, content: ''}; | ||
} | ||
var delim = options && options.delim || '---'; | ||
str = stripBom(str); | ||
if (str.charCodeAt(0) === 65279 && str.charCodeAt(1) === 116 && str.charCodeAt(2) === 104) { | ||
str = str.slice(1); | ||
var res = {orig: str, data: {}, content: str}; | ||
if (str === '' || str.indexOf(delim) !== 0) { | ||
return res; | ||
} | ||
var opts = extend({lang: 'yaml', delims: ['---', '---']}, options); | ||
var res = {orig: str, data: {}, content: str}; | ||
var opts = options || {}; | ||
opts.lang = opts.lang || 'yaml'; | ||
var delimLen = delim.length; | ||
var first = str.slice(0, opts.delims[0].length); | ||
if (first !== opts.delims[0]) { | ||
// find the index of the next fence | ||
var end = str.indexOf(delim, delimLen); | ||
if (end === -1) { | ||
return res; | ||
} | ||
var delims = opts.delims; | ||
var d1len = delims[0].length; | ||
var d2len = delims[1].length; | ||
// start the character search after the first fence | ||
var ch = delimLen, l = '', lang = ''; | ||
var len = str.length; | ||
// start the character search after the first fence | ||
var ch = d1len; | ||
var language = '', lang = ''; | ||
// detect the language, if any, after the first fence | ||
while ((language = str.charAt(ch++)) !== '\n') { | ||
lang += language; | ||
while ((l = str.charAt(ch++)) !== '\n') { | ||
lang += l; | ||
if (ch === len) { | ||
if (ch === end) { | ||
throw new Error('[gray-matter]: bad formatting, no newlines detected.'); | ||
@@ -71,13 +68,12 @@ } | ||
// find the index of the next fence | ||
var end = str.indexOf(delims[1], d1len); | ||
// 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; | ||
// get the length of the actual string following the fence | ||
var ll = lang.length; | ||
// format the language to use for parsing | ||
lang = (lang ? lang.trim() : opts.lang).toLowerCase(); | ||
lang = lang.trim(); | ||
lang = (lang ? lang : opts.lang).toLowerCase(); | ||
// if it exists, `data` is a string at this point | ||
var data = str.slice(d1len + ll, end).trim(); | ||
var data = str.slice(delimLen + langLength, end).trim(); | ||
if (data.length > 0) { | ||
@@ -93,3 +89,3 @@ // if data exists, see if we have a matching parser | ||
res.content = str.substr(end + d2len).trim(); | ||
res.content = str.substr(end + delimLen).trim(); | ||
return res; | ||
@@ -166,1 +162,17 @@ } | ||
}; | ||
matter.test = function(str, options) { | ||
var delim = options && options.delim || '---'; | ||
return str.slice(0, delim.length) === delim; | ||
}; | ||
function stripBom(str) { | ||
var hasBom = str.charCodeAt(0) === 65279 | ||
&& str.charCodeAt(1) === 116 | ||
&& str.charCodeAt(2) === 104; | ||
if (hasBom) return str.slice(1); | ||
return str; | ||
} | ||
{ | ||
"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.0", | ||
"version": "1.1.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Jon Schlinkert", |
@@ -47,3 +47,3 @@ # gray-matter [![NPM version](https://badge.fury.io/js/gray-matter.svg)](http://badge.fury.io/js/gray-matter) | ||
* `options` **{Object}** | ||
- `delims` **{Array}**: Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
- `delim` **{Array}**: Custom delimiters formatted as an array. The default is `['---', '---']`. | ||
- `parser` **{Function}**: Parser function to use. [js-yaml] is the default. | ||
@@ -58,3 +58,3 @@ | ||
### [.read](index.js#L122) | ||
### [.read](index.js#L119) | ||
@@ -71,3 +71,3 @@ Read a file and parse front matter. Returns the same object as `matter()`. | ||
### [.stringify](index.js#L153) | ||
### [.stringify](index.js#L150) | ||
@@ -74,0 +74,0 @@ Stringify an object to front-matter-formatted YAML, and concatenate it to the given string. |
17149
321