gray-matter
Advanced tools
Comparing version 3.1.1 to 4.0.0
# Release history | ||
## Unreleased | ||
## 4.0.0 - 2018-04-01 | ||
### Breaking changes | ||
- Now requires node v4 or higher. | ||
## 3.0.0 - 2017-06-30 | ||
### Breaking changes | ||
* `toml`, `coffee` and `cson` are no longer supported by default. Please see [`options.engines`](README.md#optionsengines) and the [examples](./examples) to learn how to add engines. | ||
- `toml`, `coffee` and `cson` are no longer supported by default. Please see [`options.engines`](README.md#optionsengines) and the [examples](./examples) to learn how to add engines. | ||
### Added | ||
* Support for [excerpts](README.md#optionsexcerpt). | ||
* The returned object now has non-enumerable `matter` and `stringify` properties. | ||
- Support for [excerpts](README.md#optionsexcerpt). | ||
- The returned object now has non-enumerable `matter` and `stringify` properties. | ||
### Changed | ||
* Refactored engines (parsers), so that it's easier to add parsers and stringifiers. | ||
* `options.parsers` was renamed to [`options.engines`](README.md#optionsengines) | ||
- Refactored engines (parsers), so that it's easier to add parsers and stringifiers. | ||
- `options.parsers` was renamed to [`options.engines`](README.md#optionsengines) |
@@ -1,15 +0,114 @@ | ||
declare function matter(str: string, options?: matter.GrayMatterOption): any | ||
/** | ||
* 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). | ||
* | ||
* ```js | ||
* var matter = require('gray-matter'); | ||
* console.log(matter('---\ntitle: Home\n---\nOther stuff')); | ||
* //=> { data: { title: 'Home'}, content: 'Other stuff' } | ||
* ``` | ||
* @param {Object|String} `input` String, or object with `content` string | ||
* @param {Object} `options` | ||
* @return {Object} | ||
* @api public | ||
*/ | ||
declare function matter< | ||
I extends matter.Input, | ||
O extends matter.GrayMatterOption<I, O> | ||
>(input: I | { content: I }, options?: O): matter.GrayMatterFile<I> | ||
declare namespace matter { | ||
interface GrayMatterOption { | ||
parser?: () => void; | ||
eval?: boolean; | ||
lang?: string; | ||
delims?: string | string[]; | ||
type Input = string | Buffer | ||
interface GrayMatterOption< | ||
I extends Input, | ||
O extends GrayMatterOption<I, O> | ||
> { | ||
parser?: () => void | ||
eval?: boolean | ||
excerpt?: boolean | ((input: I, options: O) => string) | ||
excerpt_separator?: string | ||
engines?: { | ||
[index: string]: | ||
| ((string) => object) | ||
| { parse: (string) => object; stringify?: (object) => string } | ||
} | ||
language?: string | ||
delimiters?: string | [string, string] | ||
} | ||
export function read(fp: string, options?: GrayMatterOption): any; | ||
export function stringify(str: string, data: object, options?: GrayMatterOption): string; | ||
export function test(str: string, options?: GrayMatterOption): string; | ||
interface GrayMatterFile<I extends Input> { | ||
data: object | ||
content: string | ||
excrept?: string | ||
orig: Buffer | I | ||
language: string | ||
matter: string | ||
stringify(lang: string): string | ||
} | ||
/** | ||
* 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. | ||
* | ||
* ```js | ||
* console.log(matter.stringify('foo bar baz', {title: 'Home'})); | ||
* // results in: | ||
* // --- | ||
* // title: Home | ||
* // --- | ||
* // foo bar baz | ||
* ``` | ||
* @param {String|Object} `file` The content string to append to stringified front-matter, or a file object with `file.content` string. | ||
* @param {Object} `data` Front matter to stringify. | ||
* @param {Object} `options` [Options](#options) to pass to gray-matter and [js-yaml]. | ||
* @return {String} Returns a string created by wrapping stringified yaml with delimiters, and appending that to the given string. | ||
*/ | ||
export function stringify<O extends GrayMatterOption<string, O>>( | ||
file: string | { content: string }, | ||
data: object, | ||
options?: GrayMatterOption<string, O> | ||
): string | ||
/** | ||
* Synchronously read a file from the file system and parse | ||
* front matter. Returns the same object as the [main function](#matter). | ||
* | ||
* ```js | ||
* var file = matter.read('./content/blog-post.md'); | ||
* ``` | ||
* @param {String} `filepath` file path of the file to read. | ||
* @param {Object} `options` [Options](#options) to pass to gray-matter. | ||
* @return {Object} Returns [an object](#returned-object) with `data` and `content` | ||
*/ | ||
export function read<O extends GrayMatterOption<string, O>>( | ||
fp: string, | ||
options?: GrayMatterOption<string, O> | ||
): matter.GrayMatterFile<string> | ||
/** | ||
* Returns true if the given `string` has front matter. | ||
* @param {String} `string` | ||
* @param {Object} `options` | ||
* @return {Boolean} True if front matter exists. | ||
*/ | ||
export function test<O extends matter.GrayMatterOption<string, O>>( | ||
str: string, | ||
options?: GrayMatterOption<string, O> | ||
): boolean | ||
/** | ||
* Detect the language to use, if one is defined after the | ||
* first front-matter delimiter. | ||
* @param {String} `string` | ||
* @param {Object} `options` | ||
* @return {Object} Object with `raw` (actual language string), and `name`, the language with whitespace trimmed | ||
*/ | ||
export function language<O extends matter.GrayMatterOption<string, O>>( | ||
str: string, | ||
options?: GrayMatterOption<string, O> | ||
): { name: string; raw: string } | ||
} | ||
export = matter |
95
index.js
'use strict'; | ||
var fs = require('fs'); | ||
var extend = require('extend-shallow'); | ||
var parse = require('./lib/parse'); | ||
var defaults = require('./lib/defaults'); | ||
var stringify = require('./lib/stringify'); | ||
var excerpt = require('./lib/excerpt'); | ||
var engines = require('./lib/engines'); | ||
var toFile = require('./lib/to-file'); | ||
var utils = require('./lib/utils'); | ||
var cache = {}; | ||
const fs = require('fs'); | ||
const sections = require('section-matter'); | ||
const defaults = require('./lib/defaults'); | ||
const stringify = require('./lib/stringify'); | ||
const excerpt = require('./lib/excerpt'); | ||
const engines = require('./lib/engines'); | ||
const toFile = require('./lib/to-file'); | ||
const parse = require('./lib/parse'); | ||
const utils = require('./lib/utils'); | ||
@@ -20,3 +19,3 @@ /** | ||
* ```js | ||
* var matter = require('gray-matter'); | ||
* const matter = require('gray-matter'); | ||
* console.log(matter('---\ntitle: Home\n---\nOther stuff')); | ||
@@ -32,15 +31,20 @@ * //=> { data: { title: 'Home'}, content: 'Other stuff' } | ||
function matter(input, options) { | ||
var file = {data: {}, content: input, excerpt: '', orig: input}; | ||
if (input === '') return file; | ||
if (input === '') { | ||
return { data: {}, content: input, excerpt: '', orig: input }; | ||
} | ||
file = toFile(input); | ||
var cached = cache[file.content]; | ||
let file = toFile(input); | ||
const cached = matter.cache[file.content]; | ||
if (!options) { | ||
if (cached) { | ||
file = extend({}, cached); | ||
file = Object.assign({}, cached); | ||
file.orig = cached.orig; | ||
return file; | ||
} | ||
cache[file.content] = file; | ||
// only cache if there are no options passed. if we cache when options | ||
// are passed, we would need to also cache options values, which would | ||
// negate any performance benefits of caching | ||
matter.cache[file.content] = file; | ||
} | ||
@@ -51,7 +55,11 @@ | ||
/** | ||
* Parse front matter | ||
*/ | ||
function parseMatter(file, options) { | ||
var opts = defaults(options); | ||
var open = opts.delimiters[0]; | ||
var close = '\n' + opts.delimiters[1]; | ||
var str = file.content; | ||
const opts = defaults(options); | ||
const open = opts.delimiters[0]; | ||
const close = '\n' + opts.delimiters[1]; | ||
let str = file.content; | ||
@@ -63,3 +71,3 @@ if (opts.language) { | ||
// get the length of the opening delimiter | ||
var openLen = open.length; | ||
const openLen = open.length; | ||
if (!utils.startsWith(str, open, openLen)) { | ||
@@ -79,6 +87,6 @@ excerpt(file, opts); | ||
str = str.slice(openLen); | ||
var len = str.length; | ||
const len = str.length; | ||
// use the language defined after first delimiter, if it exists | ||
var language = matter.language(str, opts); | ||
const language = matter.language(str, opts); | ||
if (language.name) { | ||
@@ -90,3 +98,3 @@ file.language = language.name; | ||
// get the index of the closing delimiter | ||
var closeIndex = str.indexOf(close); | ||
let closeIndex = str.indexOf(close); | ||
if (closeIndex === -1) { | ||
@@ -99,5 +107,13 @@ closeIndex = len; | ||
// create file.data by parsing the raw file.matter block | ||
file.data = parse(file.language, file.matter, opts); | ||
const block = file.matter.replace(/^\s*#[^\n]+/gm, '').trim(); | ||
if (block === '') { | ||
file.isEmpty = true; | ||
file.empty = file.content; | ||
file.data = {}; | ||
} else { | ||
// create file.data by parsing the raw file.matter block | ||
file.data = parse(file.language, file.matter, opts); | ||
} | ||
// update file.content | ||
@@ -117,2 +133,6 @@ if (closeIndex === len) { | ||
excerpt(file, opts); | ||
if (opts.sections === true || typeof opts.section === 'function') { | ||
sections(file, opts.section); | ||
} | ||
return file; | ||
@@ -149,5 +169,3 @@ } | ||
matter.stringify = function(file, data, options) { | ||
if (typeof file === 'string') { | ||
file = matter(file, options); | ||
} | ||
if (typeof file === 'string') file = matter(file, options); | ||
return stringify(file, data, options); | ||
@@ -161,3 +179,3 @@ }; | ||
* ```js | ||
* var file = matter.read('./content/blog-post.md'); | ||
* const file = matter.read('./content/blog-post.md'); | ||
* ``` | ||
@@ -171,4 +189,4 @@ * @param {String} `filepath` file path of the file to read. | ||
matter.read = function(filepath, options) { | ||
var str = fs.readFileSync(filepath, 'utf8'); | ||
var file = matter(str, options); | ||
const str = fs.readFileSync(filepath, 'utf8'); | ||
const file = matter(str, options); | ||
file.path = filepath; | ||
@@ -187,4 +205,3 @@ return file; | ||
matter.test = function(str, options) { | ||
var opts = defaults(options); | ||
return utils.startsWith(str, opts.delimiters[0]); | ||
return utils.startsWith(str, defaults(options).delimiters[0]); | ||
}; | ||
@@ -201,4 +218,4 @@ | ||
matter.language = function(str, options) { | ||
var opts = defaults(options); | ||
var open = opts.delimiters[0]; | ||
const opts = defaults(options); | ||
const open = opts.delimiters[0]; | ||
@@ -209,3 +226,3 @@ if (matter.test(str)) { | ||
var language = str.slice(0, str.search(/\r?\n/)); | ||
const language = str.slice(0, str.search(/\r?\n/)); | ||
return { | ||
@@ -221,2 +238,4 @@ raw: language, | ||
matter.cache = {}; | ||
matter.clearCache = () => (matter.cache = {}); | ||
module.exports = matter; |
'use strict'; | ||
var extend = require('extend-shallow'); | ||
var engines = require('./engines'); | ||
var utils = require('./utils'); | ||
const engines = require('./engines'); | ||
const utils = require('./utils'); | ||
module.exports = function(options) { | ||
var opts = extend({}, options); | ||
const opts = Object.assign({}, options); | ||
@@ -17,4 +16,4 @@ // ensure that delimiters are an array | ||
opts.language = (opts.language || opts.lang || 'yaml').toLowerCase(); | ||
opts.engines = extend({}, engines, opts.parsers, opts.engines); | ||
opts.engines = Object.assign({}, engines, opts.parsers, opts.engines); | ||
return opts; | ||
}; |
'use strict'; | ||
module.exports = function(name, options) { | ||
var engine = options.engines[name] || options.engines[aliase(name)]; | ||
let engine = options.engines[name] || options.engines[aliase(name)]; | ||
if (typeof engine === 'undefined') { | ||
@@ -6,0 +6,0 @@ throw new Error('gray-matter engine "' + name + '" is not registered'); |
'use strict'; | ||
var extend = require('extend-shallow'); | ||
var yaml = require('js-yaml'); | ||
const yaml = require('js-yaml'); | ||
@@ -10,3 +9,3 @@ /** | ||
var engines = exports = module.exports; | ||
const engines = exports = module.exports; | ||
@@ -29,3 +28,3 @@ /** | ||
stringify: function(obj, options) { | ||
var opts = extend({replacer: null, space: 2}, options); | ||
const opts = Object.assign({replacer: null, space: 2}, options); | ||
return JSON.stringify(obj, opts.replacer, opts.space); | ||
@@ -32,0 +31,0 @@ } |
'use strict'; | ||
var defaults = require('./defaults'); | ||
const defaults = require('./defaults'); | ||
module.exports = function(file, options) { | ||
var opts = defaults(options); | ||
const opts = defaults(options); | ||
@@ -16,3 +16,3 @@ if (file.data == null) { | ||
var sep = file.data.excerpt_separator || opts.excerpt_separator; | ||
const sep = file.data.excerpt_separator || opts.excerpt_separator; | ||
if (sep == null && (opts.excerpt === false || opts.excerpt == null)) { | ||
@@ -22,9 +22,8 @@ return file; | ||
var delimiter = sep || opts.delimiters[0]; | ||
if (typeof opts.excerpt === 'string') { | ||
delimiter = opts.excerpt; | ||
} | ||
const delimiter = typeof opts.excerpt === 'string' | ||
? opts.excerpt | ||
: (sep || opts.delimiters[0]); | ||
// if enabled, get the excerpt defined after front-matter | ||
var idx = file.content.indexOf(delimiter); | ||
const idx = file.content.indexOf(delimiter); | ||
if (idx !== -1) { | ||
@@ -31,0 +30,0 @@ file.excerpt = file.content.slice(0, idx); |
'use strict'; | ||
var getEngine = require('./engine'); | ||
var defaults = require('./defaults'); | ||
const getEngine = require('./engine'); | ||
const defaults = require('./defaults'); | ||
module.exports = function(language, str, options) { | ||
var opts = defaults(options); | ||
var engine = getEngine(language, opts); | ||
const opts = defaults(options); | ||
const engine = getEngine(language, opts); | ||
if (typeof engine.parse !== 'function') { | ||
@@ -10,0 +10,0 @@ throw new TypeError('expected "' + language + '.parse" to be a function'); |
'use strict'; | ||
var extend = require('extend-shallow'); | ||
var typeOf = require('kind-of'); | ||
var getEngine = require('./engine'); | ||
var defaults = require('./defaults'); | ||
const typeOf = require('kind-of'); | ||
const getEngine = require('./engine'); | ||
const defaults = require('./defaults'); | ||
@@ -23,13 +22,11 @@ module.exports = function(file, data, options) { | ||
var str = file.content; | ||
var opts = defaults(options); | ||
const str = file.content; | ||
const opts = defaults(options); | ||
if (data == null) { | ||
if (!opts.data) { | ||
return file; | ||
} | ||
if (!opts.data) return file; | ||
data = opts.data; | ||
} | ||
var language = file.language || opts.language; | ||
var engine = getEngine(language, opts); | ||
const language = file.language || opts.language; | ||
const engine = getEngine(language, opts); | ||
if (typeof engine.stringify !== 'function') { | ||
@@ -39,7 +36,7 @@ throw new TypeError('expected "' + language + '.stringify" to be a function'); | ||
data = extend({}, file.data, data); | ||
var open = opts.delimiters[0]; | ||
var close = opts.delimiters[1]; | ||
var matter = engine.stringify(data, options).trim(); | ||
var buf = ''; | ||
data = Object.assign({}, file.data, data); | ||
const open = opts.delimiters[0]; | ||
const close = opts.delimiters[1]; | ||
const matter = engine.stringify(data, options).trim(); | ||
let buf = ''; | ||
@@ -46,0 +43,0 @@ if (matter !== '{}') { |
'use strict'; | ||
var typeOf = require('kind-of'); | ||
var stringify = require('./stringify'); | ||
var utils = require('./utils'); | ||
const typeOf = require('kind-of'); | ||
const stringify = require('./stringify'); | ||
const utils = require('./utils'); | ||
@@ -21,43 +21,24 @@ /** | ||
if (file.content == null) { | ||
// if file was passed as an object, ensure that | ||
// "file.content" is set | ||
if (file.contents && file.content == null) { | ||
file.content = file.contents; | ||
} | ||
var orig = utils.toBuffer(file.content); | ||
Object.defineProperty(file, 'orig', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: orig | ||
}); | ||
Object.defineProperty(file, 'matter', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: file.matter || '' | ||
}); | ||
Object.defineProperty(file, 'language', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: file.language || '' | ||
}); | ||
Object.defineProperty(file, 'stringify', { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: function(data, options) { | ||
if (options && options.language) { | ||
file.language = options.language; | ||
} | ||
return stringify(file, data, options); | ||
// set non-enumerable properties on the file object | ||
utils.define(file, 'orig', utils.toBuffer(file.content)); | ||
utils.define(file, 'language', file.language || ''); | ||
utils.define(file, 'matter', file.matter || ''); | ||
utils.define(file, 'stringify', function(data, options) { | ||
if (options && options.language) { | ||
file.language = options.language; | ||
} | ||
return stringify(file, data, options); | ||
}); | ||
// strip BOM and ensure that "file.content" is a string | ||
file.content = utils.toString(file.content); | ||
file.isEmpty = false; | ||
file.excerpt = ''; | ||
return file; | ||
}; |
'use strict'; | ||
var stripBom = require('strip-bom-string'); | ||
exports.typeOf = require('kind-of'); | ||
const stripBom = require('strip-bom-string'); | ||
const typeOf = require('kind-of'); | ||
exports.define = function(obj, key, val) { | ||
Reflect.defineProperty(obj, key, { | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
value: val | ||
}); | ||
}; | ||
/** | ||
@@ -10,5 +19,3 @@ * Returns true if `val` is a buffer | ||
exports.isBuffer = function(val) { | ||
return exports.typeOf(val) === 'buffer'; | ||
}; | ||
exports.isBuffer = val => typeOf(val) === 'buffer'; | ||
@@ -19,5 +26,3 @@ /** | ||
exports.isObject = function(val) { | ||
return exports.typeOf(val) === 'object'; | ||
}; | ||
exports.isObject = val => typeOf(val) === 'object'; | ||
@@ -29,6 +34,3 @@ /** | ||
exports.toBuffer = function(input) { | ||
if (typeof input === 'string') { | ||
return new Buffer(input); | ||
} | ||
return input; | ||
return typeof input === 'string' ? Buffer.from(input) : input; | ||
}; | ||
@@ -41,5 +43,3 @@ | ||
exports.toString = function(input) { | ||
if (exports.isBuffer(input)) { | ||
return stripBom(String(input)); | ||
} | ||
if (exports.isBuffer(input)) return stripBom(String(input)); | ||
if (typeof input !== 'string') { | ||
@@ -46,0 +46,0 @@ throw new TypeError('expected input to be a string or buffer'); |
{ | ||
"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.1.1", | ||
"version": "4.0.0", | ||
"homepage": "https://github.com/jonschlinkert/gray-matter", | ||
@@ -31,3 +31,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=6.0" | ||
}, | ||
@@ -37,17 +37,15 @@ "scripts": { | ||
}, | ||
"browser": { | ||
"fs": false | ||
}, | ||
"dependencies": { | ||
"extend-shallow": "^2.0.1", | ||
"js-yaml": "^3.10.0", | ||
"kind-of": "^5.0.2", | ||
"js-yaml": "^3.11.0", | ||
"kind-of": "^6.0.2", | ||
"section-matter": "^1.0.0", | ||
"strip-bom-string": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ansi-magenta": "^0.1.1", | ||
"ansi-green": "^0.1.1", | ||
"benchmarked": "^2.0.0", | ||
"coffee-script": "^1.12.7", | ||
"coffeescript": "^2.2.3", | ||
"delimiter-regex": "^2.0.0", | ||
"front-matter": "^2.2.0", | ||
"extend-shallow": "^3.0.2", | ||
"front-matter": "^2.3.0", | ||
"gulp-format-md": "^1.0.0", | ||
@@ -92,3 +90,11 @@ "minimist": "^1.2.0", | ||
], | ||
"browser": { | ||
"fs": false | ||
}, | ||
"typings": "gray-matter.d.ts", | ||
"eslintConfig": { | ||
"rules": { | ||
"no-console": 0 | ||
} | ||
}, | ||
"verb": { | ||
@@ -95,0 +101,0 @@ "toc": false, |
100
README.md
@@ -27,5 +27,5 @@ # 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) | ||
```js | ||
var fs = require('fs'); | ||
var matter = require('gray-matter'); | ||
var str = fs.readFileSync('example.html', 'utf8'); | ||
const fs = require('fs'); | ||
const matter = require('gray-matter'); | ||
const str = fs.readFileSync('example.html', 'utf8'); | ||
console.log(matter(str)); | ||
@@ -89,6 +89,6 @@ ``` | ||
* Allow custom delimiters, when it's necessary for avoiding delimiter collision. | ||
* Should return an object with at least these three properties (for debugging): | ||
* Should return an object with at least these three properties: | ||
- `data`: the parsed YAML front matter, as a JSON object | ||
- `content`: the contents as a string, without the front matter | ||
- `orig`: the "original" content | ||
- `orig`: the "original" content (for debugging) | ||
@@ -102,3 +102,3 @@ </details> | ||
```js | ||
var matter = require('gray-matter'); | ||
const matter = require('gray-matter'); | ||
``` | ||
@@ -144,2 +144,3 @@ | ||
* `file.excerpt` **{String}**: an excerpt, if [defined on the options](#optionsexcerpt) | ||
* `file.empty` **{String}**: when the front-matter is "empty" (either all whitespace, nothing at all, or just comments and no data), the original string is set on this property. See [#65](https://github.com/jonschlinkert/gray-matter/issues/65) for details regarding use case. | ||
@@ -153,3 +154,3 @@ **Non-enumerable** | ||
* `file.matter` **{String}**: the _raw_, un-parsed front-matter string | ||
* `file.stringify` **{Function}**: [stringify](#stringify) the file by converting `file.data` to a string in the given language, wrapping it in delimiters and appending it to `file.content`. | ||
* `file.stringify` **{Function}**: [stringify](#stringify) the file by converting `file.data` to a string in the given language, wrapping it in delimiters and prepending it to `file.content`. | ||
@@ -176,2 +177,4 @@ ## Run the examples | ||
**Links to examples** | ||
* [coffee](examples/coffee.js) | ||
@@ -184,2 +187,5 @@ * [excerpt-separator](examples/excerpt-separator.js) | ||
* [json](examples/json.js) | ||
* [restore-empty](examples/restore-empty.js) | ||
* [sections-excerpt](examples/sections-excerpt.js) | ||
* [sections](examples/sections.js) | ||
* [toml](examples/toml.js) | ||
@@ -191,3 +197,3 @@ * [yaml-stringify](examples/yaml-stringify.js) | ||
### [matter](index.js#L30) | ||
### [matter](index.js#L29) | ||
@@ -205,3 +211,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). | ||
```js | ||
var matter = require('gray-matter'); | ||
const matter = require('gray-matter'); | ||
console.log(matter('---\ntitle: Home\n---\nOther stuff')); | ||
@@ -211,3 +217,3 @@ //=> { data: { title: 'Home'}, content: 'Other stuff' } | ||
### [.stringify](index.js#L140) | ||
### [.stringify](index.js#L160) | ||
@@ -234,3 +240,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#L160) | ||
### [.read](index.js#L178) | ||
@@ -248,6 +254,6 @@ Synchronously read a file from the file system and parse front matter. Returns the same object as the [main function](#matter). | ||
```js | ||
var file = matter.read('./content/blog-post.md'); | ||
const file = matter.read('./content/blog-post.md'); | ||
``` | ||
### [.test](index.js#L175) | ||
### [.test](index.js#L193) | ||
@@ -266,3 +272,3 @@ Returns true if the given `string` has front matter. | ||
**Type**: `Object` | ||
**Type**: `Boolean|Function` | ||
@@ -273,7 +279,9 @@ **Default**: `undefined` | ||
If set to `excerpt: true`, it will look for the frontmatter delimiter, `---` by default and grab everything leading up to it. | ||
**Example** | ||
```js | ||
var str = '--\ntitle: Home\n---\nAn excerpt\n---\nOther stuff'; | ||
console.log(matter(str, {excerpt: true})); | ||
const str = '---\nfoo: bar\n---\nThis is an excerpt.\n---\nThis is content'; | ||
const file = matter(str, { excerpt: true }); | ||
``` | ||
@@ -285,8 +293,40 @@ | ||
{ | ||
data: { title: 'Home'}, | ||
excerpt: '\nAn excerpt', | ||
content: '\nAn excerpt\n---\nOther stuff' | ||
content: 'This is an excerpt.\n---\nThis is content', | ||
data: { foo: 'bar' }, | ||
excerpt: 'This is an excerpt.\n' | ||
} | ||
``` | ||
You can also set `excerpt` to a function. This function uses the 'file' and 'options' that were initially passed to gray-matter as parameters, so you can control how the excerpt is extracted from the content. | ||
**Example** | ||
```js | ||
// returns the first 4 lines of the contents | ||
function firstFourLines(file, options) { | ||
file.excerpt = file.content.split('\n').slice(0, 4).join(' '); | ||
} | ||
const file = matter([ | ||
'---', | ||
'foo: bar', | ||
'---', | ||
'Only this', | ||
'will be', | ||
'in the', | ||
'excerpt', | ||
'but not this...' | ||
].join('\n'), {excerpt: firstFourLines}); | ||
``` | ||
Results in: | ||
```js | ||
{ | ||
content: 'Only this\nwill be\nin the\nexcerpt\nbut not this...', | ||
data: { foo: 'bar' }, | ||
excerpt: 'Only this will be in the excerpt' | ||
} | ||
``` | ||
### options.excerpt_separator | ||
@@ -342,3 +382,3 @@ | ||
```js | ||
var toml = require('toml'); | ||
const toml = require('toml'); | ||
@@ -349,3 +389,3 @@ /** | ||
var file = matter(str, { | ||
const file = matter(str, { | ||
engines: { | ||
@@ -360,3 +400,3 @@ toml: toml.parse.bind(toml), | ||
var file = matter(str, { | ||
const file = matter(str, { | ||
engines: { | ||
@@ -512,6 +552,8 @@ toml: { | ||
| --- | --- | | ||
| 163 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 174 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 7 | [RobLoach](https://github.com/RobLoach) | | ||
| 5 | [heymind](https://github.com/heymind) | | ||
| 2 | [doowb](https://github.com/doowb) | | ||
| 4 | [doowb](https://github.com/doowb) | | ||
| 3 | [aljopro](https://github.com/aljopro) | | ||
| 2 | [reccanti](https://github.com/reccanti) | | ||
| 2 | [onokumus](https://github.com/onokumus) | | ||
@@ -522,2 +564,3 @@ | 2 | [moozzyk](https://github.com/moozzyk) | | ||
| 1 | [ianstormtaylor](https://github.com/ianstormtaylor) | | ||
| 1 | [qm3ster](https://github.com/qm3ster) | | ||
| 1 | [zachwhaley](https://github.com/zachwhaley) | | ||
@@ -529,8 +572,9 @@ | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) | ||
* [GitHub Profile](https://github.com/jonschlinkert) | ||
* [Twitter Profile](https://twitter.com/jonschlinkert) | ||
### License | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
@@ -540,2 +584,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on October 19, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 01, 2018._ |
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
38284
552
563
12
+ Addedsection-matter@^1.0.0
+ Addedkind-of@6.0.3(transitive)
+ Addedsection-matter@1.0.0(transitive)
- Removedextend-shallow@^2.0.1
- Removedkind-of@5.1.0(transitive)
Updatedjs-yaml@^3.11.0
Updatedkind-of@^6.0.2