front-matter
Advanced tools
Comparing version 0.0.3 to 0.1.0
42
index.js
@@ -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" | ||
} | ||
} |
@@ -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 |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
8358
10
0
112
69
2
+ Addedyaml-js@0.0.5
+ Addedyaml-js@0.0.5(transitive)
- Removedyamlparser@0.0.2
- Removedyamlparser@0.0.2(transitive)