README.md (6428B)
1 # front-matter 2 [![build][build-img]][build-url] 3 [![coverage][coverage-img]][coverage-url] 4 [![npm][npm-img]][npm-url] 5 [![github][github-img]][github-url] 6 7 [![Sauce Test Status](https://saucelabs.com/browser-matrix/front-matter.svg)](https://saucelabs.com/u/front-matter) 8 9 > Extract meta data (front-matter) from documents. 10 11 This modules does not do any IO (file loading or reading), only extracting and 12 parsing front matter from strings. 13 14 This concept that was originally introduced to me through the [jekyll][jekyll] 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 =". It will also extract YAML between a separator and "...". 15 16 <!-- 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]. --> 17 18 # Install 19 20 With [npm][npm] do: 21 22 npm install front-matter 23 24 # Example 25 26 So you have a file `example.md`: 27 28 ```yaml 29 --- 30 title: Just hack'n 31 description: Nothing to see here 32 --- 33 34 This is some text about some stuff that happened sometime ago 35 ``` 36 37 **NOTE:** As of `front-matter@2.0.0` valid front matter is considered to have 38 the starting separator on the first line. 39 40 Then you can do this: 41 42 ```javascript 43 var fs = require('fs') 44 , fm = require('front-matter') 45 46 fs.readFile('./example.md', 'utf8', function(err, data){ 47 if (err) throw err 48 49 var content = fm(data) 50 51 console.log(content) 52 }) 53 ``` 54 55 And end up with an object like this: 56 57 ```javascript 58 { 59 attributes: { 60 title: 'Just hack\'n', 61 description: 'Nothing to see here' 62 }, 63 body: 'This is some text about some stuff that happened sometime ago', 64 bodyBegin: 6, 65 frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here' 66 } 67 ``` 68 69 # Methods 70 71 ```javascript 72 var fm = require('front-matter') 73 ``` 74 75 ## fm(string, { allowUnsafe: false }) 76 77 Return a `content` object with two properties: 78 79 * `content.attributes` contains the extracted yaml attributes in json form 80 * `content.body` contains the string contents below the yaml separators 81 * `content.bodyBegin` contains the line number the body contents begins at 82 * `content.frontmatter` contains the original yaml string contents 83 84 **NOTE:** By default `fm()` uses `ys-yaml`'s `safeLoad` unless you set 85 `allowUnsafe` in the options object to true. 86 87 # fm.test(string) 88 89 Check if a string contains a front matter header of "---" or "= yaml =". Primarily used internally but is useful outside of the module. 90 91 Returns `true` or `false` 92 93 ```javascript 94 fm.test(string) #=> true || false 95 ``` 96 97 # Contributing 98 99 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. 100 101 ![standard][standard-img] 102 103 All code is linted/formatted using [standard][standard-url] style, any non-conforming code can be automatically formatted using the the fmt make task: `make fmt`. 104 105 ## Maintainers 106 107 - [Adrian Gimenez](https://github.com/axdg) 108 - [Jason Campbell](https://github.com/jxson) - [@jxson](https://twitter.com/jxson) 109 110 ## Contributors 111 112 This module is awesome because of all the folks who submitted pull requests: 113 114 - [Jordan Santell](https://github.com/jsantell) - [@jsantell](https://twitter.com/jsantell) 115 - [Jean-Philippe Monette](https://github.com/jpmonette) - [@jpmonette](https://twitter.com/jpmonette) 116 - [Kai Davenport](https://github.com/binocarlos) 117 - [Marc-André Arseneault](https://github.com/arsnl) - [@im_arsnl](https://twitter.com/im_arsnl) 118 - [Bret Comnes](https://github.com/bcomnes) - http://bret.io 119 - [Shinnosuke Watanabe](https://github.com/shinnn) 120 - [Matt Dickens](https://github.com/mpd106) 121 - [Rod Knowlton](https://github.com/codelahoma) 122 - [Rich DeLauder](https://github.com/FMJaguar) 123 - [Sean Lang](https://github.com/slang800) - http://slang.cx 124 - [Benjamin Tan](https://github.com/d10) - https://d10.github.io/ 125 - [Kenneth Lim](https://github.com/kenlimmj) - https://kenlimmj.com 126 - [Cameron Moy](https://github.com/camoy) 127 - [Fernando Montoya](https://github.com/montogeek) - https://montogeek.com 128 - [Martin Heidegger](https://github.com/martinheidegger) 129 - [Leo Liang](https://github.com/aleung) 130 - [Kasper Pedersen](https://github.com/kasperpedersen) 131 132 # CHANGELOG 133 134 ## 3.0.0 135 * CI only tests Node versions >= v6 (drops v4, and v5) 136 * 137 138 # LICENSE (MIT) 139 140 Copyright (c) Jason Campbell ("Author") 141 142 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: 143 144 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 145 146 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. 147 148 149 [yaml]: http://en.wikipedia.org/wiki/YAML 150 [haiku]: http://haiku.io 151 [npm]: http://npmjs.org 152 [jekyll]: https://github.com/mojombo/jekyll 153 [coverage-img]: https://img.shields.io/coveralls/jxson/front-matter.svg 154 [coverage-url]: https://coveralls.io/r/jxson/front-matter?branch=master 155 [build-img]: https://img.shields.io/travis/jxson/front-matter/master.svg 156 [build-url]: http://travis-ci.org/jxson/front-matter 157 [npm-img]: https://img.shields.io/npm/dm/front-matter.svg 158 [npm-url]: https://npmjs.org/package/standard 159 [github-img]: https://img.shields.io/github/stars/jxson/front-matter.svg?style=social&label=Star 160 [github-url]: https://github.com/jxson/front-matter/ 161 [standard-img]: https://cdn.rawgit.com/feross/standard/master/badge.svg 162 [standard-url]: http://npmjs.com/package/standard