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

gray-matter

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gray-matter - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

lib/utils.js

27

index.js

@@ -10,3 +10,3 @@ /**

// node_modules
var delim = require('delims');
var delims = require('delims');
var file = require('fs-utils');

@@ -16,18 +16,29 @@ var _ = require('lodash');

// Local libs
var parse = require('./lib/parsers');
var parsers = require('./lib/parsers');
var utils = require('./lib/utils');
// Parse the given string
var matter = function (src, options) {
var opts = _.extend({delims: ['---','---'], format: 'yaml'}, options)
function matter(src, options) {
var opts = _.extend({delims: ['---','---'], lang: 'yaml'}, options);
var metadata = {};
var content = src;
var delimiters = delim(opts.delims).evaluate;
var delimiters = delims(opts.delims).evaluate;
// If true, will attempt to detect and register
// the correct parser based on the returned string
if(opts.autodetect) {
opts.lang = utils.detectLang(opts.delims[0], content);
content = content.replace(opts.lang, '');
}
// File object
var fileObject = content.match(delimiters);
if (fileObject && fileObject.length === 3) {
metadata = parse[opts.format](fileObject[1]);
try {
metadata = parsers[opts.lang](fileObject[1]);
} catch(e) {
e.origin = __filename;
console.warn('Front-matter language not detected by gray-matter', e);
}
content = fileObject[2];

@@ -34,0 +45,0 @@ }

@@ -22,3 +22,2 @@ /**

parse.json = function(src) {

@@ -28,3 +27,2 @@ return JSON.parse(src);

parse.coffee = function(src, options) {

@@ -31,0 +29,0 @@ options = options || {};

{
"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.1.0",
"version": "0.2.0",
"author": {

@@ -40,2 +40,5 @@ "name": "Jon Schlinkert",

"matter",
"parse",
"parser",
"yfm",
"JSON",

@@ -42,0 +45,0 @@ "coffee",

@@ -64,3 +64,3 @@ # gray-matter [![NPM version](https://badge.fury.io/js/gray-matter.png)](http://badge.fury.io/js/gray-matter)

#### format
#### lang
Type: `String`

@@ -72,6 +72,5 @@

#### delims
Type: `Object`
#### delimiters
Type: `object`
Default: `{delims: ['---', '---']}`

@@ -95,6 +94,23 @@

```
_Note that passing multiple delimiters will yield unpredictable results, so it is recommended that you use this option only for testing purposes._
_However, passing multiple delimiters will yield unpredictable results, so it is recommended that you use this option only for testing purposes._
#### autodetect
Type: `Boolean`
Default: `undefined`
Attempts to automatically register a language that is specified after the first code boundary (delimiter).
Usage Example:
```coffee
--- coffee
user = 'jonschlinkert'
reverse = (src) ->
src.split('').reverse().join('')
---
{%= user %}
{%= reverse(user) %}
```
## Examples

@@ -101,0 +117,0 @@

@@ -15,3 +15,5 @@ /**

var file = require('fs-utils');
var _ = require('lodash');
// Local libs

@@ -22,3 +24,3 @@ var matter = require('../');

var complexExpected = {
context: {"foo": 'bar', "version": 2},
context: {foo: 'bar', version: 2},
content: '\n\n<span class="alert alert-info">This is an alert</span>\n',

@@ -33,11 +35,11 @@ original: '---\nfoo: bar\nversion: 2\n---\n\n<span class="alert alert-info">This is an alert</span>\n'

var empty = {
"context": {},
"content": "",
"original": ""
context: {},
content: "",
original: ""
};
var contentOnly = {
"context": {},
"content": "# This file doesn't have matter!",
"original": "# This file doesn't have matter!"
context: {},
content: "# This file doesn't have matter!",
original: "# This file doesn't have matter!"
};

@@ -93,8 +95,8 @@

var expected = {
"context": {
"title": "JSON",
context: {
title: "JSON",
"description": "Front Matter"
},
"content": "\n\n# This page has JSON front matter!",
"original": "---\n{\n \"title\": \"JSON\",\n \"description\": \"Front Matter\"\n}\n---\n\n# This page has JSON front matter!"
content: "\n\n# This page has JSON front matter!",
original: "---\n{\n \"title\": \"JSON\",\n \"description\": \"Front Matter\"\n}\n---\n\n# This page has JSON front matter!"
};

@@ -108,11 +110,11 @@ expect(actual).to.deep.equal(expected);

it('should parse coffee front matter.', function(done) {
var actual = matter.read('./test/fixtures/coffee.md', {format: 'coffee'});
var actual = matter.read('./test/fixtures/coffee.md', {lang: 'coffee'});
var expected = {
"context": {
"categories": "front matter coffee coffescript",
context: {
categories: "front matter coffee coffescript",
"title": "Coffee",
"description": "Front matter",
},
"content": "\n\n# This page has coffee front matter!",
"original": "---\ntitle: 'Coffee'\ndescription: '''\n Front matter\n '''\ncategories: '''\n front matter coffee coffescript\n '''\n---\n\n# This page has coffee front matter!"
content: "\n\n# This page has coffee front matter!",
original: "---\ntitle: 'Coffee'\ndescription: '''\n Front matter\n '''\ncategories: '''\n front matter coffee coffescript\n '''\n---\n\n# This page has coffee front matter!"
}

@@ -122,4 +124,22 @@ expect(actual).to.deep.equal(expected);

});
it('should evaluate functions in coffee front matter.', function(done) {
var actual = matter.read('./test/fixtures/coffee-fn.md', {lang: 'coffee'});
expect(typeof actual.context).to.equal('function');
done();
});
});
describe('Autodetect language:', function() {
it('should autodetect front matter language, and use the correct parser.', function(done) {
var actual = matter.read('./test/fixtures/autodetect.md', {autodetect: true});
var expected = {
context: 'jonschlinkert',
content: "\nContent",
original: "--- coffee\nuser = 'jonschlinkert'\n---\nContent"
}
expect(actual).to.deep.equal(expected);
done();
});
});
describe('Read empty files:', function() {

@@ -126,0 +146,0 @@ it('should return an object, even if the file is empty.', function(done) {

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