gray-matter
Advanced tools
Comparing version 3.0.8 to 3.1.0
43
index.js
'use strict'; | ||
var fs = require('fs'); | ||
var extend = require('extend-shallow'); | ||
var parse = require('./lib/parse'); | ||
@@ -30,17 +31,25 @@ var defaults = require('./lib/defaults'); | ||
function matter(input, options) { | ||
var file = toFile(input); | ||
var str = file.content; | ||
var file = {data: {}, content: input, excerpt: '', orig: input}; | ||
if (input === '') return file; | ||
if (str === '') return file; | ||
if (typeof options === 'undefined') { | ||
if (cache[str]) { | ||
return cache[str]; | ||
file = toFile(input); | ||
var cached = cache[file.content]; | ||
if (!options) { | ||
if (cached) { | ||
file = extend({}, cached); | ||
file.orig = cached.orig; | ||
return file; | ||
} | ||
cache[str] = file; | ||
cache[file.content] = file; | ||
} | ||
// support "options.delims" for backward compatibility | ||
return parseMatter(file, options); | ||
} | ||
function parseMatter(file, options) { | ||
var opts = defaults(options); | ||
var open = opts.delimiters[0]; | ||
var close = '\n' + opts.delimiters[1]; | ||
var str = file.content; | ||
@@ -51,2 +60,3 @@ if (opts.language) { | ||
// get the length of the opening delimiter | ||
var openLen = open.length; | ||
@@ -58,7 +68,10 @@ if (!utils.startsWith(str, open, openLen)) { | ||
var nextChar = str.charAt(openLen); | ||
if (nextChar === open.slice(-1)) { | ||
// if the next character after the opening delimiter is | ||
// a character from the delimiter, then it's not a front- | ||
// matter delimiter | ||
if (str.charAt(openLen) === open.slice(-1)) { | ||
return file; | ||
} | ||
// strip the opening delimiter | ||
str = str.slice(openLen); | ||
@@ -87,12 +100,12 @@ var len = str.length; | ||
// update file.content | ||
if (closeIndex !== len) { | ||
if (closeIndex === len) { | ||
file.content = ''; | ||
} else { | ||
file.content = str.slice(closeIndex + close.length); | ||
if (file.content.charAt(0) === '\r') { | ||
if (file.content[0] === '\r') { | ||
file.content = file.content.slice(1); | ||
} | ||
if (file.content.charAt(0) === '\n') { | ||
if (file.content[0] === '\n') { | ||
file.content = file.content.slice(1); | ||
} | ||
} else { | ||
file.content = ''; | ||
} | ||
@@ -99,0 +112,0 @@ |
'use strict'; | ||
module.exports = function(name, options) { | ||
var engine = options.engines[name] || options.engines[aliases(name)]; | ||
var engine = options.engines[name] || options.engines[aliase(name)]; | ||
if (typeof engine === 'undefined') { | ||
@@ -14,3 +14,3 @@ throw new Error('gray-matter engine "' + name + '" is not registered'); | ||
function aliases(name) { | ||
function aliase(name) { | ||
switch (name.toLowerCase()) { | ||
@@ -17,0 +17,0 @@ case 'js': |
@@ -7,4 +7,7 @@ 'use strict'; | ||
var opts = defaults(options); | ||
file.data = file.data || {}; | ||
if (file.data == null) { | ||
file.data = {}; | ||
} | ||
if (typeof opts.excerpt === 'function') { | ||
@@ -15,3 +18,3 @@ return opts.excerpt(file, opts); | ||
var sep = file.data.excerpt_separator || opts.excerpt_separator; | ||
if (!sep && (opts.excerpt === false || opts.excerpt == null)) { | ||
if (sep == null && (opts.excerpt === false || opts.excerpt == null)) { | ||
return file; | ||
@@ -18,0 +21,0 @@ } |
@@ -26,7 +26,6 @@ 'use strict'; | ||
if (data == null) { | ||
if (opts.data) { | ||
data = opts.data; | ||
} else { | ||
if (!opts.data) { | ||
return file; | ||
} | ||
data = opts.data; | ||
} | ||
@@ -43,9 +42,7 @@ | ||
var close = opts.delimiters[1]; | ||
var matter = newline(engine.stringify(data, options)); | ||
var matter = engine.stringify(data, options).trim(); | ||
var buf = ''; | ||
if (matter.trim() !== '{}') { | ||
buf += newline(open); | ||
buf += matter; | ||
buf += newline(close); | ||
if (matter !== '{}') { | ||
buf = newline(open) + newline(matter) + newline(close); | ||
} | ||
@@ -55,9 +52,7 @@ | ||
if (str.indexOf(file.excerpt.trim()) === -1) { | ||
buf += newline(file.excerpt); | ||
buf += newline(close); | ||
buf += newline(file.excerpt) + newline(close); | ||
} | ||
} | ||
buf += newline(str); | ||
return buf; | ||
return buf + newline(str); | ||
}; | ||
@@ -64,0 +59,0 @@ |
'use strict'; | ||
var typeOf = require('kind-of'); | ||
var stringify = require('./stringify'); | ||
@@ -12,7 +13,10 @@ var utils = require('./utils'); | ||
module.exports = function(file) { | ||
var isObject = utils.isObject(file); | ||
if (!isObject) { | ||
if (typeOf(file) !== 'object') { | ||
file = { content: file }; | ||
} | ||
if (typeOf(file.data) !== 'object') { | ||
file.data = {}; | ||
} | ||
if (file.content == null) { | ||
@@ -58,7 +62,3 @@ file.content = file.contents; | ||
file.excerpt = ''; | ||
if (!utils.isObject(file.data)) { | ||
file.data = {}; | ||
} | ||
return file; | ||
}; |
'use strict'; | ||
var stripBom = require('strip-bom-string'); | ||
var utils = module.exports = exports; | ||
utils.typeOf = require('kind-of'); | ||
exports.typeOf = require('kind-of'); | ||
@@ -11,4 +10,4 @@ /** | ||
utils.isBuffer = function(val) { | ||
return utils.typeOf(val) === 'buffer'; | ||
exports.isBuffer = function(val) { | ||
return exports.typeOf(val) === 'buffer'; | ||
}; | ||
@@ -20,4 +19,4 @@ | ||
utils.isObject = function(val) { | ||
return utils.typeOf(val) === 'object'; | ||
exports.isObject = function(val) { | ||
return exports.typeOf(val) === 'object'; | ||
}; | ||
@@ -29,3 +28,3 @@ | ||
utils.toBuffer = function(input) { | ||
exports.toBuffer = function(input) { | ||
if (typeof input === 'string') { | ||
@@ -41,4 +40,4 @@ return new Buffer(input); | ||
utils.toString = function(input) { | ||
if (utils.isBuffer(input)) { | ||
exports.toString = function(input) { | ||
if (exports.isBuffer(input)) { | ||
return stripBom(String(input)); | ||
@@ -56,3 +55,3 @@ } | ||
utils.arrayify = function(val) { | ||
exports.arrayify = function(val) { | ||
return val ? (Array.isArray(val) ? val : [val]) : []; | ||
@@ -65,5 +64,5 @@ }; | ||
utils.startsWith = function(str, substr, len) { | ||
exports.startsWith = function(str, substr, len) { | ||
if (typeof len !== 'number') len = substr.length; | ||
return str.slice(0, len) === substr; | ||
}; |
{ | ||
"name": "gray-matter", | ||
"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. Used by metalsmith, assemble, verb and many other projects.", | ||
"version": "3.0.8", | ||
"version": "3.1.0", | ||
"homepage": "https://github.com/jonschlinkert/gray-matter", | ||
@@ -46,4 +46,2 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"devDependencies": { | ||
"ansi-bold": "^0.1.1", | ||
"ansi-gray": "^0.1.1", | ||
"ansi-magenta": "^0.1.1", | ||
@@ -53,11 +51,9 @@ "benchmarked": "^2.0.0", | ||
"delimiter-regex": "^2.0.0", | ||
"for-own": "^1.0.0", | ||
"front-matter": "^2.2.0", | ||
"gulp-format-md": "^1.0.0", | ||
"matched": "^1.0.2", | ||
"minimist": "^1.2.0", | ||
"mocha": "^4.0.0", | ||
"sort-object": "^3.0.2", | ||
"toml": "^2.3.3", | ||
"vinyl": "^2.1.0" | ||
"vinyl": "^2.1.0", | ||
"write": "^1.0.3" | ||
}, | ||
@@ -64,0 +60,0 @@ "keywords": [ |
@@ -5,3 +5,3 @@ # gray-matter [![NPM version](https://img.shields.io/npm/v/gray-matter.svg?style=flat)](https://www.npmjs.com/package/gray-matter) [![NPM monthly downloads](https://img.shields.io/npm/dm/gray-matter.svg?style=flat)](https://npmjs.org/package/gray-matter) [![NPM total downloads](https://img.shields.io/npm/dt/gray-matter.svg?style=flat)](https://npmjs.org/package/gray-matter) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/gray-matter.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/gray-matter) | ||
Follow this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), for updates on this project and others. | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
@@ -185,3 +185,3 @@ ## Install | ||
### [matter](index.js#L29) | ||
### [matter](index.js#L30) | ||
@@ -204,3 +204,3 @@ Takes a string or object with `content` property, extracts and parses front-matter from the string, then returns an object with `data`, `content` and other [useful properties](#returned-object). | ||
### [.stringify](index.js#L127) | ||
### [.stringify](index.js#L140) | ||
@@ -227,3 +227,3 @@ Stringify an object to YAML or the specified language, and append it to the given string. By default, only YAML and JSON can be stringified. See the [engines](#engines) section to learn how to stringify other languages. | ||
### [.read](index.js#L147) | ||
### [.read](index.js#L160) | ||
@@ -244,3 +244,3 @@ Synchronously read a file from the file system and parse front matter. Returns the same object as the [main function](#matter). | ||
### [.test](index.js#L162) | ||
### [.test](index.js#L175) | ||
@@ -456,2 +456,33 @@ Returns true if the given `string` has front matter. | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
</details> | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
</details> | ||
### Related projects | ||
@@ -465,6 +496,2 @@ | ||
### Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
### Contributors | ||
@@ -474,3 +501,3 @@ | ||
| --- | --- | | ||
| 158 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 163 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 7 | [RobLoach](https://github.com/RobLoach) | | ||
@@ -486,20 +513,2 @@ | 5 | [heymind](https://github.com/heymind) | | ||
### Building docs | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
### Running tests | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
### Author | ||
@@ -519,2 +528,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on October 05, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on October 19, 2017._ |
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
32788
11
465
519