gray-matter
Advanced tools
Comparing version 1.0.2 to 1.1.0
82
index.js
@@ -5,7 +5,6 @@ 'use strict'; | ||
var extend = require('extend-shallow'); | ||
var parsers = require('./lib/parsers'); | ||
/** | ||
* Expose `matter()` | ||
* | ||
* @type {Function} | ||
*/ | ||
@@ -37,38 +36,59 @@ | ||
str = str.replace(/^\uFEFF/, '').replace(/\r/g, ''); | ||
var o = {lang: '', data: {}, content: '', orig: str}; | ||
var opts = extend({lang: 'yaml'}, options); | ||
var delims = opts.delims || ['---', '---']; | ||
if (str === '') { | ||
return {orig: '', data: {}, content: ''}; | ||
} | ||
var i = str.indexOf(delims[0]); | ||
if (i !== 0) { | ||
o.content = str; | ||
delete o.lang; | ||
return o; | ||
if (str.charCodeAt(0) === 65279 && str.charCodeAt(1) === 116 && str.charCodeAt(2) === 104) { | ||
str = str.slice(1); | ||
} | ||
var len1 = delims[0].length; | ||
var len2 = delims[1].length; | ||
var ch = len1; | ||
var lang; | ||
var opts = extend({lang: 'yaml', delims: ['---', '---']}, options); | ||
var res = {orig: str, data: {}, content: str}; | ||
while ((lang = str.charAt(ch++)) !== '\n') { | ||
o.lang += lang; | ||
var first = str.slice(0, opts.delims[0].length); | ||
if (first !== opts.delims[0]) { | ||
return res; | ||
} | ||
var ll = o.lang.length; | ||
var to = str.indexOf(delims[1], len1); | ||
o.lang = (o.lang || opts.lang).trim(); | ||
var fn = opts.parser || parsers[o.lang]; | ||
if (fn) { | ||
o.data = fn(str.substr(len1 + ll, to - ll - len2), opts); | ||
} else { | ||
throw new Error('gray-matter cannot find a parser for: ' + str); | ||
var delims = opts.delims; | ||
var d1len = delims[0].length; | ||
var d2len = delims[1].length; | ||
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; | ||
if (ch === len) { | ||
throw new Error('[gray-matter]: bad formatting, no newlines detected.'); | ||
} | ||
} | ||
return { | ||
orig: o.orig, | ||
data: o.data, | ||
content: str.substr(to + len2).trim() | ||
}; | ||
// find the index of the next fence | ||
var end = str.indexOf(delims[1], d1len); | ||
// 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(); | ||
// if it exists, `data` is a string at this point | ||
var data = str.slice(d1len + ll, end).trim(); | ||
if (data.length > 0) { | ||
// if data exists, see if we have a matching parser | ||
var fn = opts.parser || parsers[lang]; | ||
if (typeof fn === 'function') { | ||
res.data = fn(data, opts); | ||
} else { | ||
throw new Error('gray-matter cannot find a parser for: ' + str); | ||
} | ||
} | ||
res.content = str.substr(end + d2len).trim(); | ||
return res; | ||
} | ||
@@ -82,3 +102,3 @@ | ||
var parsers = matter.parsers = require('./lib/parsers'); | ||
matter.parsers = parsers; | ||
@@ -85,0 +105,0 @@ /** |
/** | ||
* gray-matter <https://github.com/assemble/gray-matter> | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* Copyright (c) 2014-2015, Jon Schlinkert. | ||
* Licensed under the MIT license. | ||
@@ -16,6 +16,6 @@ */ | ||
var extend = require('extend-shallow'); | ||
var chalk = require('chalk'); | ||
/** | ||
* Expose `parser` | ||
* Expose `parser` module | ||
*/ | ||
@@ -25,3 +25,2 @@ | ||
/** | ||
@@ -33,3 +32,2 @@ * Requires cache. | ||
/** | ||
@@ -45,10 +43,13 @@ * Parse YAML front matter | ||
parser.yaml = function(str, options) { | ||
var data = {}; | ||
var opts = extend({strict: false, saftLoad: false}, options); | ||
try { | ||
var YAML = parser.requires.yaml || (parser.requires.yaml = require('js-yaml')); | ||
data = YAML.safeLoad(str, options); | ||
return opts.safeLoad ? YAML.safeLoad(str, options) : YAML.load(str, options); | ||
} catch (err) { | ||
throw new Error('gray-matter parser [js-yaml]:' + err); | ||
if (opts.strict) { | ||
throw new Error(msg('js-yaml', err)); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
return data; | ||
}; | ||
@@ -65,10 +66,13 @@ | ||
parser.json = function(str) { | ||
var data = {}; | ||
parser.json = function(str, options) { | ||
var opts = extend({strict: false}, options); | ||
try { | ||
data = JSON.parse(str); | ||
return JSON.parse(str); | ||
} catch (err) { | ||
throw new Error('gray-matter cannot parse JSON: ' + str); | ||
if (opts.strict) { | ||
throw new Error(msg('JSON', err)); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
return data; | ||
}; | ||
@@ -87,3 +91,3 @@ | ||
* | ||
* ```coffee | ||
* ```markdown | ||
* ---js | ||
@@ -107,18 +111,21 @@ * title: 'autodetect-javascript', | ||
parser.javascript = function(str, options) { | ||
var opts = extend({wrapped: true, eval: false}, options); | ||
var opts = extend({wrapped: true, eval: false, strict: false}, options); | ||
if (opts.eval) { | ||
var fn = str; | ||
if (opts.wrapped) { | ||
fn = 'function data() {return { ' + str + '} } data();'; | ||
str = 'function data() {return {' + str + '}; } data();'; | ||
} | ||
var data = {}; | ||
try { | ||
data = eval(fn); | ||
return eval(str); | ||
} catch (err) { | ||
throw new Error('gray-matter parser [javascript]:' + err); | ||
throw new Error(msg('javascript', err)); | ||
} | ||
return data; | ||
return {}; | ||
} else { | ||
throw new Error('gray-matter: to parse javascript set `options.eval` to `true`'); | ||
// if `eval` isn't set | ||
if (opts.strict) { | ||
throw new Error(evalError('javascript')); | ||
} else { | ||
console.error(evalError('javascript', true)); | ||
} | ||
} | ||
@@ -148,4 +155,3 @@ }; | ||
parser.coffee = function(str, options) { | ||
var opts = extend({eval: false}, options); | ||
var opts = extend({eval: false, strict: false}, options); | ||
if (opts.eval) { | ||
@@ -156,10 +162,15 @@ try { | ||
} catch (err) { | ||
throw new Error('gray-matter parser [coffee-script]:' + err); | ||
throw new Error(msg('coffee-script', err)); | ||
} | ||
} else { | ||
throw new Error('gray-matter: to parse coffee set `options.eval` to `true`'); | ||
// if `eval` isn't set | ||
if (opts.strict) { | ||
throw new Error(evalError('coffee')); | ||
} else { | ||
console.error(evalError('coffee', true)); | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -173,3 +184,2 @@ * Alias for `parse.coffee()`. | ||
/** | ||
@@ -184,9 +194,26 @@ * Parse TOML front matter. | ||
parser.toml = function(str) { | ||
parser.toml = function(str, opts) { | ||
try { | ||
var toml = parser.requires.toml || (parser.requires.toml = require('toml')); | ||
return toml.parse(str.trim()); | ||
return toml.parse(str); | ||
} catch (err) { | ||
throw new Error('gray-matter parser [toml]:' + err); | ||
if (opts.strict) { | ||
throw new Error(msg('TOML', err)); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
}; | ||
/** | ||
* Normalize error messages | ||
*/ | ||
function msg(lang, err) { | ||
return 'gray-matter parser [' + lang + ']: ' + err; | ||
} | ||
function evalError(lang, color) { | ||
var msg = '[gray-matter]: to parse ' + lang + ' set `options.eval` to `true`'; | ||
return color ? chalk.red(msg) : msg; | ||
} |
{ | ||
"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.0.2", | ||
"version": "1.1.0", | ||
"author": { | ||
@@ -16,8 +16,6 @@ "name": "Jon Schlinkert", | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/assemble/gray-matter/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/assemble/gray-matter/blob/master/LICENSE-MIT" | ||
}, | ||
"main": "index.js", | ||
@@ -30,5 +28,8 @@ "engines": { | ||
"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" | ||
"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" | ||
}, | ||
"dependencies": { | ||
"chalk": "^0.5.1", | ||
"extend-shallow": "^0.1.1", | ||
@@ -40,3 +41,5 @@ "js-yaml": "^3.2.3" | ||
"coffee-script": "^1.8.0", | ||
"delims": "^0.4.0", | ||
"delimiter-regex": "^1.1.0", | ||
"for-own": "^0.1.2", | ||
"front-matter": "^0.2.1", | ||
"glob": "^4.2.1", | ||
@@ -47,4 +50,6 @@ "gulp": "^3.8.10", | ||
"lodash": "^2.4.1", | ||
"minimist": "^1.1.0", | ||
"mocha": "^1.19.0", | ||
"should": "^4.3.0", | ||
"sort-object": "^0.3.2", | ||
"toml": "^2.0.6" | ||
@@ -51,0 +56,0 @@ }, |
@@ -41,3 +41,3 @@ # gray-matter [![NPM version](https://badge.fury.io/js/gray-matter.svg)](http://badge.fury.io/js/gray-matter) | ||
## API | ||
### [matter](index.js#L31) | ||
### [matter](index.js#L30) | ||
@@ -58,3 +58,3 @@ Parses a `string` of front-matter with the given `options`, and returns an object. | ||
### [.read](index.js#L102) | ||
### [.read](index.js#L122) | ||
@@ -71,3 +71,3 @@ Read a file and parse front matter. Returns the same object as `matter()`. | ||
### [.stringify](index.js#L133) | ||
### [.stringify](index.js#L153) | ||
@@ -227,3 +227,3 @@ Stringify an object to front-matter-formatted YAML, and concatenate it to the given string. | ||
## License | ||
Copyright (c) 2014 Jon Schlinkert | ||
Copyright (c) 2015 Jon Schlinkert | ||
Released under the MIT license | ||
@@ -233,3 +233,3 @@ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 10, 2014. To update, run `npm i -g verb && verb`._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on January 15, 2015._ | ||
@@ -236,0 +236,0 @@ |
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
312
16938
3
15
5
+ Addedchalk@^0.5.1
+ Addedansi-regex@0.2.1(transitive)
+ Addedansi-styles@1.1.0(transitive)
+ Addedchalk@0.5.1(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@0.1.0(transitive)
+ Addedstrip-ansi@0.3.0(transitive)
+ Addedsupports-color@0.2.0(transitive)