Skip to content

Commit

Permalink
Merge 20f3c3b into a956033
Browse files Browse the repository at this point in the history
  • Loading branch information
nswbmw committed Mar 15, 2016
2 parents a956033 + 20f3c3b commit 7a2e704
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ Returns `true` or `false`

fm.test(string) #=> true || false

# fm.parse(alias: fm)
# fm.stringify(obj[, opt])

Stringify a object to yaml. eg:


fm.stringify({
attributes: {
title: 'Just hack\'n',
description: 'Nothing to see here'
},
body: '\nThis is some text about some stuff that happened sometime ago'
})


will output:

---
title: "Just hack'n"
description: Nothing to see here
---

This is some text about some stuff that happened sometime ago


# Contributing

front-matter is an OPEN Source Project so please help out by [reporting bugs](http://github.com/jxson/front-matter/issues) or [forking and opening pull](https://github.com/jxson/front-matter) requests when possible.
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var os = require('os')
var parser = require('js-yaml')
var optionalByteOrderMark = '\\ufeff?'
var pattern = '^(' +
Expand All @@ -14,6 +15,8 @@ var regex = new RegExp(pattern, 'm')

module.exports = extractor
module.exports.test = test
module.exports.parse = extractor
module.exports.stringify = stringify

function extractor (string) {
string = string || ''
Expand Down Expand Up @@ -48,3 +51,20 @@ function test (string) {

return regex.test(string)
}

function stringify (obj, opt) {
obj = obj || {}
opt = opt || {}
var attributes = obj.attributes || {}
var body = obj.body || {}
var scope = opt.scope || '---'

if (Object.keys(attributes).length === 0) {
return body
}

var yaml = parser.dump(attributes)
yaml = scope + os.EOL + yaml + scope + os.EOL + body

return yaml
}
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,31 @@ test('Supports live updating', function (t) {
t.end()
})

test('fm.stringify(obj) - stringify yaml without front-matter', function (t) {
var data = 'no front matter here'
var obj = { attributes: {}, body: data }
t.equal(fm.stringify(obj), 'no front matter here', 'fm.stringify(obj) should equal data')
t.end()
})

test('fm.stringify(obj) - stringify yaml delinetead by `---`', function (t) {
read('dashes-seperator.md', function (err, data) {
t.error(err, 'read(...) should not error')
t.equal(fm.stringify(fm(data)), data, 'fm.stringify(fm(data)) should equal data')

t.end()
})
})

test('fm.stringify(obj) - stringify yaml delinetead by `= yaml =`', function (t) {
read('dashes-seperator.md', function (err, data) {
t.error(err, 'read(...) should not error')
t.ok(fm.stringify(fm(data), { scope: '= yaml =' }).match(/^= yaml =\s/), 'should begain with = yaml =')

t.end()
})
})

function read (relative, callback) {
var directory = path.resolve(__dirname, '../examples')
var resolved = path.join(directory, relative)
Expand Down

0 comments on commit 7a2e704

Please sign in to comment.