🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

front-matter

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

front-matter - npm Package Compare versions

Comparing version
0.0.3
to
0.1.0
+4
.travis.yml
language: node_js
node_js:
- 0.8
- 0.6
---
title: Three dashes marks the spot
tags:
- yaml
- toes
- dashes
expaned-description: with some --- crazy stuff in it
---
don't break
---
ALso this shouldn't be a problem
---
title: Complex yaml example
description: You can use the front-matter module to convert this
tags: [example, yaml, node]
folded-text: |
There once was a man from Darjeeling
Who got on a bus bound for Ealing
It said on the door
"Please don't spit on the floor"
So he carefully spat on the ceiling
wrapped-text: >
Wrapped text
will be folded
into a single
paragraph
Blank lines denote
paragraph breaks
---
Some crazy stuff going on up there ^^
= yaml =
title: I couldn't think of a better name
description: Just an example of using `= yaml =`
= yaml =
Plays nice with markdown syntax highlighting
# The MIT License (MIT)
Copyright (c) Jason Campbell ("Author")
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var fm = require('../')
, fs = require('fs')
, path = require('path')
, chai = require('chai')
, assert = chai.assert
chai.use(fmAsserts)
describe('fm(string)', function(){
it('parses yaml delinetead by `---`', function(done){
read('dashes-seperator.md', function(err, data){
if (err) return done(err)
var content = fm(data)
, body = '\ndon\'t break\n\n'
+ '---\n\n'
+ 'ALso this shouldn\'t be a problem\n'
assert.validExtraction(content)
assert.hasBody(content, body)
assert.propertyVal(content.attributes
, 'title'
, 'Three dashes marks the spot')
assert.property(content.attributes, 'tags')
assert.lengthOf(content.attributes.tags, 3)
done()
})
})
it('parses yaml delinetead by `= yaml =`', function(done){
read('yaml-seperator.md', function(err, data){
if (err) return done(err)
var content = fm(data)
, body = '\nPlays nice with markdown syntax highlighting\n'
assert.validExtraction(content)
assert.hasBody(content, body)
assert.propertyVal(content.attributes
, 'title'
, 'I couldn\'t think of a better name')
assert.propertyVal(content.attributes
, 'description'
, 'Just an example of using `= yaml =`')
done()
})
})
it('works on strings missing front-matter', function(){
var body = 'No front matter here'
, content = fm(body)
assert.validExtraction(content)
assert.hasBody(content, body)
assert.lengthOf(Object.keys(content.attributes), 0)
})
it('parses wrapped text in yaml prperly', function(done){
read('wrapped-text.md', function(err, data){
if (err) return done(err)
var content = fm(data)
, body = '\nSome crazy stuff going on up there ^^\n'
, foldedText = 'There once was a man from Darjeeling\n'
+ 'Who got on a bus bound for Ealing\n'
+ ' It said on the door\n'
+ ' "Please don\'t spit on the floor"\n'
+ 'So he carefully spat on the ceiling\n'
assert.validExtraction(content)
assert.hasBody(content, body)
assert.propertyVal(content.attributes
, 'folded-text'
, foldedText)
done()
})
})
})
function read(file, callback){
var dir = path.resolve(__dirname, '../examples')
, file = path.join(dir, file)
fs.readFile(file, 'utf8', callback)
}
function fmAsserts(chai, utils){
var Assertion = chai.Assertion
, assert = chai.assert
Assertion.includeStack = true
Assertion.addMethod('validExtraction', function(extraction, message){
assert.isObject(content)
assert.property(extraction, 'attributes')
assert.isObject(extraction.attributes)
assert.property(extraction, 'body')
assert.isString(extraction.body)
})
assert.validExtraction = function(extraction, message){
new Assertion(extraction, message).is['validExtraction']
}
assert.hasBody = function(extraction, body, message){
assert.property(extraction, 'body')
assert.isString(extraction.body)
assert.propertyVal(extraction, 'body', body)
}
}
+21
-21

@@ -1,26 +0,26 @@

var yaml = require('yamlparser')
;
var parser = require('yaml-js')
module.exports = function(data){
// http://stackoverflow.com/q/1068308
var data = data || ''
, regex = /^(\s*---([\s\S]+)---\s*)/gi
, match = regex.exec(data)
, attributes
, body
module.exports = function(string){
var body = string
, attributes = {}
, match = matcher(string, '= yaml =') || matcher(string, '---')
if (match && match.length > 0){
var yamlString = match[2].replace(/^\s+|\s+$/g, '')
attributes = yaml.eval(yamlString)
body = data.replace(match[0], '')
} else {
attributes = {};
body = data;
if (match){
attributes = parser.load(match[2].replace(/^\s+|\s+$/g, ''))
body = string.replace(match[0], '')
}
return {
attributes: attributes,
body: body
};
return { attributes: attributes, body: body }
}
function matcher(string, seperator){
var seperator = seperator || '---'
, pattern = '^('
+ seperator
+ '$([\\s\\S]*?)'
+ seperator+'$\\n)'
, regex = new RegExp(pattern, 'm')
, match = regex.exec(string)
if (match && match.length > 0) return match
}

@@ -5,4 +5,7 @@ {

"description": "Extract YAML front matter from strings",
"version": "0.0.3",
"keywords": [ "yaml", "front matter", "meta data"],
"license": "MIT",
"version": "0.1.0",
"homepage": "https://github.com/jxson/front-matter",
"bugs": "https://github.com/jxson/front-matter/issues",
"repository": {

@@ -14,3 +17,3 @@ "type": "git",

"scripts": {
"test": "blah"
"test": "mocha tests/ --reporter tap"
},

@@ -21,6 +24,8 @@ "engines": {

"dependencies": {
"yamlparser": "0.0.2"
"yaml-js": "0.0.5"
},
"devDependencies": {},
"optionalDependencies": {}
"devDependencies": {
"chai": "1.4.2",
"mocha": "1.7.4"
}
}
+55
-17

@@ -1,31 +0,69 @@

node-front-matter
=================
[![build status](https://secure.travis-ci.org/jxson/front-matter.png)](http://travis-ci.org/jxson/front-matter)
http://en.wikipedia.org/wiki/YAML
# front-matter
Extract YAML front matter from strings.
Extract [YAML][yaml] front matter from strings.
So you have this file `example.md`:
This modules does not do any IO (file loading or reading), only extracting yaml front matter from strings.
This concept that was originally introduced to me through the [jeykll][jeykll] blogging system and is pretty useful where you want to be able to easily add metadata to content without the need for a database. YAML is extracted from the the top of a file between matching separators of "---" or "= yaml =".
<!-- This is part of a long running project I have been working on where I am splitting out internals of [haiku][haiku] into to separate, more useful and shareable modules. If your in need of a static site generator [check it out][haiku]. -->
# Example
So you have a file `example.md`:
---
title:
description:
title: Just hack'n
description: Nothing to see here
---
This is some content
This is some text about some stuff that happened sometime ago
Then you can do this:
var frontmatter = require('front-matter')
, extract = frontmatter(data)
var fs = require('fs')
, fm = require('front-matter')
fs.readFile('./example.md', 'utf8', function(err, data){
if (err) throw err
And end up with this:
var content = fm(data)
console.log(extract)
console.log(content)
})
{ attributes: { title: 'example'
, description: 'example description'
}
, body: '\nThis is some content\n'
}
And end up with an object like this:
{ attributes: { title: 'Just hack\'n'
, description: 'Nothing to see here'
}
, body: 'This is some content'
}
# Methods
var fm = require('front-matter')
## fm(string)
Return a `content` object with two properties:
* `content.attributes` contains the extracted yaml attributes in json form
* `content.body` contains the string contents below the yaml separators
## Install
With [npm][npm] do:
npm install front-matter
# License
MIT
[yaml]: http://en.wikipedia.org/wiki/YAML
[haiku]: http://haiku.io
[npm]: http://npmjs.org
[jeykll]: https://github.com/mojombo/jekyll
---
title: A basic example
description: You can use the front-matter module to convert this
tags: [example, yaml, node]
foldedText |
There once was a man from Darjeeling
Who got on a bus bound for Ealing
It said on the door
"Please don't spit on the floor"
So he carefully spat on the ceiling
wrappedText >
Wrapped text
will be folded
into a single
paragraph
Blank lines denote
paragraph breaks
---
Using the front-matter module you can access YAML attributes defined at the top of a string the way this file shows, YAML attributes must be defined at the top and between a set of three dashes "---".
When you read this file and run it's contents through `front-matter` you will receive an object with two keys, `attributes` and `body`. The `attributes` key will contain the YAML attributes and the `body` will contain the remaining file contents.
This module is intentionally small and leaves out things like reading the file, this allows you the flexibility to use your preferred file reading method and get really creative with mixing in formats and templating languages. For example these three paragraphs will be the `body` and can be treated as a Mustache template which could be populated from the YAML attributes and then parsed through Markdown.
{{#tags}}
* {{.}}
{{/tags}}
---
title: Some Title
tags:
- paper
- rock
- scissors
id: 1001
nested:
attr1: [a, b, c]
attr2: [1, 2, 3]
attr3: [g, h, i]
---
Super Large Text
---
*Mega Man X*, known in Japan as Rockman X (ロックマンX?), is a video game developed by Capcom for the Super Nintendo Entertainment System (SNES). It is the first Mega Man game for the 16-bit console and the first game in the Mega Man X series, a spin-off of the original Mega Man series that began on the SNES's predecessor, the Nintendo Entertainment System (NES). Mega Man X was first published in Japan on December 12, 1993 and was released in both North America and Europe the following year. Taking place a century after the original Mega Man series, Mega Man X is set in a futuristic world populated by both humans and "Reploids", robots capable of thinking, feeling, and growing like their human creators. Because of these complex attributes, many Reploids are prone to destructive, criminal activity and are thereafter referred to as "Mavericks". The plot of the game follows the protagonist Mega Man X, an android member of a military task force called the "Maverick Hunters". With the help of his partner Zero, X must thwart the plans of Sigma, a powerful Maverick leader wishing to bring about human extinction.
Some bullet points
---
* Paper
* Rock
* Scissors
var
frontmatter = require('../index'),
fs = require('fs');
module.exports = {
'parse files' : function ( test ) {
test.expect( 6 );
fs.readFile( 'tests/data/test.md', 'utf-8', function ( err, data ) {
data = frontmatter(data);
test.ok( data.body, 'Body should be returned' );
test.ok( data.attributes.title === 'Some Title' );
test.ok( data.attributes.tags[2] === 'scissors' );
test.ok( data.attributes.id === 1001 );
test.ok( data.attributes.nested.attr1[2] === 'c' );
test.ok( data.attributes.nested.attr3[2] === 'i' );
test.done();
});
},
'parse invalid data' : function ( test ) {
test.expect( 4 );
var
data1 = frontmatter(''),
data2 = frontmatter('invalid');
test.ok( data1.attributes, 'should not have data' );
test.ok( data1.body === '', 'body should be empty' );
test.ok( data2.attributes, 'should not have data' );
test.ok( data2.body === 'invalid', 'should have body if no FM found' );
test.done();
}
};
#!/usr/bin/env node
require('nodeunit').reporters.default.run([ 'tests/front-matter-tests.js' ]);