UNPKG

4.9 kBMarkdownView Raw
1
2# front-matter
3
4[![NPM](https://nodei.co/npm/front-matter.png)](https://nodei.co/npm/front-matter/)
5
6## Extract [YAML][yaml] front matter from strings.
7
8[![build status](https://secure.travis-ci.org/jxson/front-matter.png)](http://travis-ci.org/jxson/front-matter) [![Coverage Status](https://coveralls.io/repos/jxson/front-matter/badge.svg?branch=master)](https://coveralls.io/r/jxson/front-matter?branch=master) [![Dependency Status](https://david-dm.org/jxson/front-matter.png)](https://david-dm.org/jxson/front-matter)
9
10This modules does not do any IO (file loading or reading), only extracting yaml front matter from strings.
11
12This 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 meta-data to content without the need for a database. YAML is extracted from the the top of a file between matching separators of "---" or "= yaml =".
13
14<!-- 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]. -->
15
16# Install
17
18With [npm][npm] do:
19
20 npm install front-matter
21
22# Example
23
24So you have a file `example.md`:
25
26 ---
27 title: Just hack'n
28 description: Nothing to see here
29 ---
30
31 This is some text about some stuff that happened sometime ago
32
33**NOTE:** As of `front-matter@2.0.0` valid front matter is considered to have
34the starting separator on the first line.
35
36Then you can do this:
37
38 var fs = require('fs')
39 , fm = require('front-matter')
40
41 fs.readFile('./example.md', 'utf8', function(err, data){
42 if (err) throw err
43
44 var content = fm(data)
45
46 console.log(content)
47 })
48
49And end up with an object like this:
50
51 { attributes: { title: 'Just hack\'n'
52 , description: 'Nothing to see here'
53 }
54 , body: 'This is some text about some stuff that happened sometime ago'
55 }
56
57# Methods
58
59 var fm = require('front-matter')
60
61## fm(string)
62
63Return a `content` object with two properties:
64
65* `content.attributes` contains the extracted yaml attributes in json form
66* `content.body` contains the string contents below the yaml separators
67
68# fm.test(string)
69
70Check if a string contains a front matter header of "---" or "= yaml =". Primarily used internally but is useful outside of the module.
71
72Returns `true` or `false`
73
74 fm.test(string) #=> true || false
75
76# Contributing
77
78front-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.
79
80### Contributors
81
82This module is awesome because of all the folks who helped out:
83
84- [Jason Campbell](https://github.com/jxson) - [@jxson](https://twitter.com/jxson)
85- [Jordan Santell](https://github.com/jsantell) - [@jsantell](https://twitter.com/jsantell)
86- [Jean-Philippe Monette](https://github.com/jpmonette) - [@jpmonette](https://twitter.com/jpmonette)
87- [Kai Davenport](https://github.com/binocarlos)
88- [Marc-André Arseneault](https://github.com/arsnl) - [@im_arsnl](https://twitter.com/im_arsnl)
89- [Bret Comnes](https://github.com/bcomnes) - http://bret.io
90- [Shinnosuke Watanabe](https://github.com/shinnn)
91- [Matt Dickens](https://github.com/mpd106)
92- [Rod Knowlton](https://github.com/codelahoma)
93- [Rich DeLauder](https://github.com/FMJaguar)
94- [Sean Lang](https://github.com/slang800) - http://slang.cx
95- [Benjamin Tan](https://github.com/d10) - https://d10.github.io/
96- [Kenneth Lim](https://github.com/kenlimmj) - https://kenlimmj.com
97
98# LICENSE (MIT)
99
100Copyright (c) Jason Campbell ("Author")
101
102Permission 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:
103
104The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
105
106THE 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.
107
108
109[yaml]: http://en.wikipedia.org/wiki/YAML
110[haiku]: http://haiku.io
111[npm]: http://npmjs.org
112[jeykll]: https://github.com/mojombo/jekyll