gray-matter
Advanced tools
Comparing version 0.2.5 to 0.2.6
{ | ||
"name": "gray-matter", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"main": [ | ||
@@ -5,0 +5,0 @@ "index.js" |
36
index.js
@@ -20,7 +20,7 @@ /** | ||
// Parse the given string | ||
function matter(src, options) { | ||
function matter(str, options) { | ||
var opts = _.extend({delims: ['---','---'], lang: 'yaml'}, options); | ||
var metadata = {}; | ||
var content = src; | ||
var content = str; | ||
var delimiters = delims(opts.delims).evaluate; | ||
@@ -50,3 +50,3 @@ | ||
content: content, | ||
original: src | ||
original: str | ||
}; | ||
@@ -61,17 +61,35 @@ }; | ||
// Does YAML front matter exist? | ||
matter.exists = function(src, options) { | ||
var obj = matter.read(src, options).context; | ||
matter.exists = function(str, options) { | ||
var obj = matter(str, options).context; | ||
return _.keys(obj).length > 0; | ||
}; | ||
// Extend and stringify YAML. | ||
matter.extend = function(str, obj) { | ||
if(matter.exists(str)) { | ||
var context = _.extend({}, matter(str).context, obj); | ||
var yaml = matter.stringifyYAML(context); | ||
return '---\n' + yaml + '---'; | ||
} else { | ||
return ''; | ||
} | ||
}; | ||
// Extend YAML, then put the file back together | ||
matter.reconstruct = function(str, obj) { | ||
var front = matter.extend(str, obj); | ||
var content = matter(str).content; | ||
return front + content; | ||
}; | ||
// Stringify to jSON | ||
matter.stringifyJSON = function(src, options) { | ||
return matter(src, options).context; | ||
matter.stringify = function(str, options) { | ||
return matter(str, options).context; | ||
}; | ||
// Stringify to YAML | ||
matter.stringifyYAML = function(src) { | ||
return YAML.dump(src); | ||
matter.stringifyYAML = function(obj) { | ||
return YAML.dump(obj); | ||
}; | ||
module.exports = matter; |
{ | ||
"name": "gray-matter", | ||
"description": "A simple to use YAML, JSON or Coffee Front-Matter parsing and extraction library, with options to set custom delimiters.", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Jon Schlinkert", |
124
README.md
@@ -9,6 +9,38 @@ # gray-matter [![NPM version](https://badge.fury.io/js/gray-matter.png)](http://badge.fury.io/js/gray-matter) | ||
## Quickstart | ||
Install with [npm](npmjs.org) | ||
```bash | ||
npm i gray-matter --save | ||
``` | ||
Install with [bower](https://github.com/bower/bower) | ||
```bash | ||
bower install gray-matter --save | ||
``` | ||
<!-- toc --> | ||
* [Quickstart](#quickstart) | ||
* [Usage](#usage) | ||
* [Methods](#methods) | ||
* [matter](#matter) | ||
* [matter.read](#matterread) | ||
* [matter.exists](#matterexists) | ||
* [matter.extend](#matterextend) | ||
* [matter.recontruct](#matterrecontruct) | ||
* [matter.stringify](#matterstringify) | ||
* [matter.stringifyYAML](#matterstringifyyaml) | ||
* [Options](#options) | ||
* [lang](#lang) | ||
* [delims](#delims) | ||
* [autodetect](#autodetect) | ||
* [Examples](#examples) | ||
* [matter](#matter) | ||
* [matter.extend](#matterextend) | ||
* [Authors](#authors) | ||
* [License](#license) | ||
<!-- toc stop --> | ||
## Usage | ||
@@ -28,14 +60,12 @@ | ||
```js | ||
matter('---\nTitle: This is matter\n---\n<p>This is content.<p>'); | ||
matter(str); | ||
``` | ||
results in: | ||
results in something like: | ||
```json | ||
{ | ||
"context": { | ||
"title": "This is matter" | ||
}, | ||
"content": "<p>This is content.<p>", | ||
"original": "---\nTitle: This is matter\n---\n<p>This is content.<p>" | ||
"context": {"foo": "bar"}, | ||
"content": "baz", | ||
"original": "---\nfoo: bar\n---\nbaz" | ||
} | ||
@@ -46,3 +76,3 @@ ``` | ||
To read a file from the file system before parsing, use `matter.read`: | ||
Read a file from the file system before parsing. | ||
@@ -52,13 +82,54 @@ ```js | ||
``` | ||
Returns: | ||
```json | ||
{ | ||
"context": {"foo": "bar"}, | ||
"content": "baz", | ||
"original": "---\nfoo: bar\n---\nbaz" | ||
} | ||
``` | ||
### matter.exists | ||
To check for YAML front matter, returning `true` or `false` if it exists, use `matter.exists`: | ||
Returns `true` or `false` if front matter exists: | ||
```js | ||
matter.exists('file.md'); | ||
matter.exists(str); | ||
``` | ||
### matter.extend | ||
Extend and stringify **YAML** front matter. Takes an object as the second parameter, and returns either the extended, stringified object (YAML), or if no front matter is found an empty string is returned. | ||
```js | ||
matter.extend(str, obj); | ||
``` | ||
### matter.recontruct | ||
A convenience wrapper around the `matter` and `matter.extend`. Extends YAML front matter, then re-assembles front matter with the content of the file. | ||
```js | ||
matter.recontruct(str, obj); | ||
``` | ||
### matter.stringify | ||
A convenience wrapper around the `matter(str).context` method. | ||
```js | ||
matter.stringify(str); | ||
``` | ||
### matter.stringifyYAML | ||
Stringify parsed front matter back to YAML. | ||
```js | ||
matter.stringifyYAML(str); | ||
``` | ||
## Options | ||
@@ -119,2 +190,4 @@ | ||
### matter | ||
Let's say our page, `foo.html` contains | ||
@@ -159,3 +232,34 @@ | ||
### matter.extend | ||
Given this page: | ||
```html | ||
--- | ||
title: Gray Matter | ||
--- | ||
Hooray! | ||
``` | ||
and this config: | ||
```js | ||
var file = require('fs').readFileSync('file.md', 'utf8'); | ||
var obj = { | ||
description: 'A simple to use front matter lib'; | ||
}; | ||
matter.extend(file, obj); | ||
``` | ||
the result would be: | ||
```html | ||
--- | ||
title: Gray Matter | ||
description: A simple to use front matter lib | ||
--- | ||
Hooray! | ||
``` | ||
## Authors | ||
@@ -162,0 +266,0 @@ |
@@ -280,3 +280,4 @@ /** | ||
it('should return true or false if YAML front matter exists.', function (done) { | ||
var actual = matter.exists('./test/fixtures/alpha.hbs'); | ||
var fixture = file.readFileSync('./test/fixtures/alpha.hbs'); | ||
var actual = matter.exists(fixture); | ||
expect(actual).to.equal(true); | ||
@@ -283,0 +284,0 @@ done(); |
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
51136
559
283