gulp-gray-matter
Advanced tools
Comparing version 2.2.2 to 3.0.0
118
index.js
'use strict'; | ||
var es = require('event-stream'); | ||
var matter = require('gray-matter'); | ||
var grayMatter = require('gray-matter'), | ||
gUtil = require('gulp-util'), | ||
merge = require('merge'), | ||
objectPath = require('object-path'), | ||
through = require('through2'); | ||
var genError = function (message) { | ||
return new Error('gulp-gray-matter: ' + message); | ||
}; | ||
module.exports = gulpGrayMatter; | ||
var extract = function (file, callback) { | ||
if (file.isBuffer()) { | ||
if (this.handler || this.name) { | ||
var data = matter(String(file.contents), this.options); | ||
if (this.handler) { | ||
file.contents = new Buffer(this.handler.call(matter, data) || ''); | ||
} | ||
if (this.name) { | ||
file[this.name] = data.data; // may have been modified by handler | ||
} | ||
/** | ||
* gray-matter gulp plugin | ||
* @param {object} options custom options | ||
* @return {object} gulp stream handler | ||
*/ | ||
function gulpGrayMatter(options) { | ||
options = setOptions(options); | ||
return through.obj(transformChunk); | ||
/** | ||
* transform a file | ||
* @param {object} chunk file object | ||
* @param {string} enc file encoding | ||
* @param {Function} done callback | ||
* @return {undefined} | ||
*/ | ||
function transformChunk(chunk, enc, done) { | ||
if (chunk.isNull()) return done(null, chunk); | ||
if (chunk.isStream()) return this.emit('error', new gUtil.PluginError('gulp-gray-matter', 'Streaming not supported')); | ||
try { | ||
extractMatter(chunk); | ||
} catch (err) { | ||
return this.emit('error', err); | ||
} | ||
return callback(null, file); | ||
done(null, chunk); | ||
} | ||
if (file.isStream()) { | ||
// TODO? | ||
return callback(genError('Cannot get the front-matter in a stream. (unsupported)'), file); | ||
/** | ||
* extract matter data from file and optionally remove matter header | ||
* @param {object} chunk file object | ||
* @return {undefined} | ||
*/ | ||
function extractMatter(chunk) { | ||
var matter = grayMatter(String(chunk.contents), options.grayMatter), | ||
data = objectPath.get(chunk, options.property); | ||
data = options.setData(typeof data === 'object' ? data : {}, matter.data); | ||
objectPath.set(chunk, options.property, data); | ||
if (options.remove) { | ||
chunk.contents = new Buffer( | ||
options.trim ? String(matter.content).trim() : matter.content | ||
); | ||
} | ||
} | ||
callback(null, file); | ||
}; | ||
/** | ||
* @param {function(this: gray-matter, object): string} handler (optional) | ||
* @param {object} options options for gray-matter (optional) | ||
* @param {string} name propery name for adding frontmatter object to file (optional) | ||
*/ | ||
module.exports = function (handler, options, name) { | ||
if (handler !== null && !(handler instanceof Function)) { | ||
name = options; | ||
options = handler; | ||
handler = null; | ||
/** | ||
* sets new data values | ||
* @param {object} oldData old data | ||
* @param {object} newData new data | ||
* @return {undefined} | ||
*/ | ||
function setData(oldData, newData) { | ||
return merge.recursive(oldData, newData); | ||
} | ||
if (options !== null && typeof options !== 'object') { | ||
name = options; | ||
options = null; | ||
/** | ||
* [setOptions description] | ||
* @param {object} opts custom options | ||
* @return {object} options object | ||
*/ | ||
function setOptions(opts) { | ||
opts = typeof opts === 'object' ? opts : {}; | ||
return { | ||
property: typeof opts.property === 'string' ? opts.property : 'data', | ||
remove: typeof opts.remove === 'boolean' ? opts.remove : true, | ||
trim: typeof opts.trim === 'boolean' ? opts.trim : true, | ||
setData: typeof opts.setData === 'function' ? opts.setData : setData, | ||
grayMatter: { | ||
delims: opts.delims || '---', | ||
eval: typeof opts.eval === 'boolean' ? opts.eval : true, | ||
lang: opts.lang || 'yaml', | ||
parser: opts.parser || undefined | ||
} | ||
}; | ||
} | ||
return es.map(extract.bind({ | ||
name: name, | ||
options: options, | ||
handler: handler | ||
})); | ||
}; | ||
} |
{ | ||
"name": "gulp-gray-matter", | ||
"version": "2.2.2", | ||
"description": "Extract gray-matter header from files.", | ||
"version": "3.0.0", | ||
"description": "A gulp plugin for extracting data header from file contents using gray-matter.", | ||
"author": "Simon Lepel <simbo@simbo.de> (http://simbo.ninja)", | ||
"main": "index.js", | ||
"keywords": [ | ||
"frontmatter", | ||
"front-matter", | ||
"gray-matter", | ||
"gulpplugin" | ||
"gulpplugin", | ||
"gulp", | ||
"data" | ||
], | ||
"scripts": { | ||
"test": "mocha -R spec", | ||
"lint": "eslint ./index.js", | ||
"cover": "istanbul cover ./node_modules/.bin/_mocha -- -R spec", | ||
"watch": "mocha -R spec -w", | ||
"codecov": "codecov" | ||
}, | ||
@@ -27,3 +35,2 @@ "repository": { | ||
"files": [ | ||
"Changes.md", | ||
"index.js", | ||
@@ -34,6 +41,17 @@ "LICENSE", | ||
"dependencies": { | ||
"event-stream": "~3.1.0", | ||
"gray-matter": "~1.2.0" | ||
"gray-matter": "^2.0.2", | ||
"gulp-util": "^3.0.7", | ||
"merge": "^1.2.0", | ||
"object-path": "^0.9.2", | ||
"through2": "^2.0.0" | ||
}, | ||
"devDependencies": {} | ||
"devDependencies": { | ||
"codecov": "^1.0.1", | ||
"eslint": "^1.10.3", | ||
"eslint-config-simbo": "^0.2.0", | ||
"gulp": "^3.9.0", | ||
"istanbul": "^0.4.2", | ||
"mocha": "^2.4.5", | ||
"stream-assert": "^2.0.3" | ||
} | ||
} |
164
README.md
gulp-gray-matter | ||
================ | ||
> A simple gulp plugin for [gray-matter](https://github.com/assemble/gray-matter). | ||
> A *gulp* plugin for extracting data header from file contents using *gray-matter*. | ||
[![Build Status](https://travis-ci.org/jakwings/gulp-gray-matter.svg)](https://travis-ci.org/jakwings/gulp-gray-matter) | ||
[![NPM version](https://badge.fury.io/js/gulp-gray-matter.svg)](http://badge.fury.io/js/gulp-gray-matter) | ||
> *"See the [benchmarks](https://www.npmjs.com/package/gray-matter#benchmarks). gray-matter is 20-30x faster than front-matter."* | ||
> ([@jonschlinkert](https://www.npmjs.com/~jonschlinkert)) | ||
**maintainer change. original author: [@jakwings](https://www.npmjs.com/~jakwings)** | ||
[![npm Package Version](https://img.shields.io/npm/v/gulp-gray-matter.svg?style=flat-square)](https://www.npmjs.com/package/gulp-gray-matter) | ||
[![MIT License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://simbo.mit-license.org) | ||
Usage | ||
===== | ||
[![Travis Build Status](https://img.shields.io/travis/simbo/gulp-gray-matter/master.svg?style=flat-square)](https://travis-ci.org/simbo/gulp-gray-matter) | ||
[![Codecov Test Coverage](https://img.shields.io/codecov/c/github/simbo/gulp-gray-matter.svg?style=flat-square)](https://codecov.io/github/simbo/gulp-gray-matter) | ||
``` javascript | ||
/** | ||
* @param {function(this: gray-matter, object): string} handler (optional) | ||
* @param {object} options options for gray-matter (optional) | ||
* @param {string} name propery name for adding frontmatter object to file (optional) | ||
*/ | ||
var matter = require('gulp-gray-matter'); | ||
[![Dependencies Status](https://img.shields.io/david/simbo/gulp-gray-matter.svg?style=flat-square)](https://david-dm.org/simbo/gulp-gray-matter) | ||
[![devDependencies Status](https://img.shields.io/david/dev/simbo/gulp-gray-matter.svg?style=flat-square)](https://david-dm.org/simbo/gulp-gray-matter#info=devDependencies) | ||
var noop = function (data) { | ||
return data.orig; // Since gray-matter v0.5.0, "original" property is renamed "orig" | ||
}; | ||
--- | ||
/** | ||
* @param this gray-matter | ||
* @param {object} data object return from `gray-matter(content)` | ||
* @return {string} | ||
*/ | ||
var handler = function (data) { | ||
var matter = this; // not gulp plugin | ||
return data.content; | ||
}; | ||
<!-- MarkdownTOC --> | ||
... | ||
gulp.src('*.md') | ||
.pipe(matter(handler, {delims: ['@@@', '@@@']})) | ||
.pipe(gulp.dest('build')); | ||
... | ||
- [About](#about) | ||
- [Setup and usage](#setup-and-usage) | ||
- [Options](#options) | ||
- [property](#property) | ||
- [remove](#remove) | ||
- [trim](#trim) | ||
- [setData](#setdata) | ||
- [License](#license) | ||
... | ||
gulp.src('*.md') | ||
.pipe(matter(handler, null, 'context')) | ||
.pipe(custom()) // file.contents = Handlerbars.compile(file.contents)(file.context) | ||
.pipe(gulp.dest('build')); | ||
... | ||
<!-- /MarkdownTOC --> | ||
--- | ||
## About | ||
*gulp-gray-matter* is a plugin for [gulp](http://gulpjs.com/), to extract data | ||
headers from file contents using [gray-matter](https://www.npmjs.com/package/gray-matter). | ||
The extracted data is set as a property of the file object for further processing. | ||
You can customize the property name and also use nested properties (via | ||
[object-path](https://www.npmjs.com/package/object-path)). | ||
If the file object already has data attached on the defined property, existing | ||
data will be merged recursively with extracted data (using | ||
[merge](https://www.npmjs.com/package/object-path)). You can define a custom | ||
function for setting data to change this behavior. | ||
There are further [custom options](#options) and of course you can also use all | ||
[gray-matter options](https://www.npmjs.com/package/gray-matter#options). | ||
## Setup and usage | ||
Install `gulp-gray-matter` using `npm`: | ||
```sh | ||
npm i gulp-gray-matter | ||
``` | ||
License | ||
======= | ||
In your `gulpfile.js`: | ||
Copyright © 2014 Jak Wings. Released under the MIT license | ||
```js | ||
var gulp = require('gulp'), | ||
gulpGrayMatter = require('gulp-gray-matter'); | ||
gulp.task('default', function() { | ||
return gulp.src('./src/**.*') | ||
.pipe(gulpGrayMatter({ /* options */ })) | ||
// … | ||
.pipe(gulp.dest('./dest')); | ||
}); | ||
``` | ||
A common workflow, after extracting front matter, could be using a template | ||
rendering plugin like [gulp-jade](https://www.npmjs.com/package/gulp-jade). | ||
## Options | ||
Beside its own options, *gulp-gray-matter* also supports all [gray-matter options](https://www.npmjs.com/package/gray-matter#options): | ||
`delims`, `eval`, `lang` and `parser` | ||
### property | ||
*String* | ||
Default: `'data'` | ||
file object property for setting data. can also be a nested property name like | ||
`foo.bar.baz`. | ||
### remove | ||
*Boolean* | ||
Default: `true` | ||
Whether data header should be removed from file contents or not. | ||
### trim | ||
*Boolean* | ||
Default: `true` | ||
Whether file contents should be trimmed after removing file header or not. | ||
(has no effect if `options.remove` is `false`.) | ||
### setData | ||
*Function* | ||
Default: | ||
```js | ||
function setData(oldData, newData) { | ||
return require('merge').recursive(oldData, newData); | ||
} | ||
``` | ||
If there is already data attached to the file object on the property defined | ||
with `options.property`, existing data will be recursively merged with extracted | ||
data. Set your own function to change this behavior. | ||
## License | ||
[MIT © 2016 Simon Lepel](http://simbo.mit-license.org/) | ||
Before version 2.2.2, [@jakwings](https://www.npmjs.com/~jakwings) was the | ||
original author and owner of the npm package *gulp-gray-matter*. |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8926
78
140
0
5
7
4
+ Addedgulp-util@^3.0.7
+ Addedmerge@^1.2.0
+ Addedobject-path@^0.9.2
+ Addedthrough2@^2.0.0
+ Addedansi-gray@0.1.1(transitive)
+ Addedansi-red@0.1.1(transitive)
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedansi-wrap@0.1.0(transitive)
+ Addedarray-differ@1.0.0(transitive)
+ Addedarray-uniq@1.0.3(transitive)
+ Addedbeeper@1.1.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedclone@1.0.4(transitive)
+ Addedclone-stats@0.0.1(transitive)
+ Addedcoffee-script@1.12.7(transitive)
+ Addedcolor-support@1.1.3(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddateformat@2.2.0(transitive)
+ Addedduplexer2@0.0.2(transitive)
+ Addedextend-shallow@2.0.1(transitive)
+ Addedfancy-log@1.3.3(transitive)
+ Addedglogg@1.0.2(transitive)
+ Addedgray-matter@2.1.1(transitive)
+ Addedgulp-util@3.0.8(transitive)
+ Addedgulplog@1.0.0(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhas-gulplog@0.1.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedisarray@0.0.11.0.0(transitive)
+ Addedlodash._basecopy@3.0.1(transitive)
+ Addedlodash._basetostring@3.0.1(transitive)
+ Addedlodash._basevalues@3.0.0(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash._reescape@3.0.0(transitive)
+ Addedlodash._reevaluate@3.0.0(transitive)
+ Addedlodash._reinterpolate@3.0.0(transitive)
+ Addedlodash._root@3.0.1(transitive)
+ Addedlodash.escape@3.2.0(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.keys@3.1.2(transitive)
+ Addedlodash.restparam@3.6.1(transitive)
+ Addedlodash.template@3.6.2(transitive)
+ Addedlodash.templatesettings@3.1.1(transitive)
+ Addedmerge@1.2.1(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmultipipe@0.1.2(transitive)
+ Addedobject-assign@3.0.0(transitive)
+ Addedobject-path@0.9.2(transitive)
+ Addedparse-node-version@1.0.1(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@1.1.142.3.8(transitive)
+ Addedreplace-ext@0.0.1(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsparkles@1.0.1(transitive)
+ Addedstring_decoder@0.10.311.1.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedtime-stamp@1.1.0(transitive)
+ Addedtoml@2.3.6(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedvinyl@0.5.3(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedevent-stream@~3.1.0
- Removedansi-regex@0.2.1(transitive)
- Removedansi-styles@1.1.0(transitive)
- Removedarray-slice@0.2.3(transitive)
- Removedchalk@0.5.1(transitive)
- Removedduplexer@0.1.2(transitive)
- Removedevent-stream@3.1.7(transitive)
- Removedextend-shallow@0.2.0(transitive)
- Removedfrom@0.1.7(transitive)
- Removedgray-matter@1.2.6(transitive)
- Removedhas-ansi@0.1.0(transitive)
- Removedmap-stream@0.1.0(transitive)
- Removedpause-stream@0.0.11(transitive)
- Removedsplit@0.2.10(transitive)
- Removedstream-combiner@0.0.4(transitive)
- Removedstrip-ansi@0.3.0(transitive)
- Removedsupports-color@0.2.0(transitive)
- Removedthrough@2.3.8(transitive)
Updatedgray-matter@^2.0.2