Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gulp-gray-matter

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-gray-matter - npm Package Compare versions

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"
}
}
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 &copy; 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 &copy; 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc