|
| 1 | +# vfile-frontmatter |
| 2 | + |
| 3 | +[![Build][build-badge]][build] |
| 4 | +[![Coverage][coverage-badge]][coverage] |
| 5 | +[![Downloads][downloads-badge]][downloads] |
| 6 | +[![Size][size-badge]][size] |
| 7 | +[![Sponsors][sponsors-badge]][collective] |
| 8 | +[![Backers][backers-badge]][collective] |
| 9 | +[![Chat][chat-badge]][chat] |
| 10 | + |
| 11 | +Parse the YAML frontmatter in a [`vfile`][vfile]. |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +[npm][]: |
| 16 | + |
| 17 | +```sh |
| 18 | +npm install vfile-frontmatter |
| 19 | +``` |
| 20 | + |
| 21 | +## Use |
| 22 | + |
| 23 | +Say we have a file, `example.html`: |
| 24 | + |
| 25 | +```html |
| 26 | +--- |
| 27 | +title: Hello, world! |
| 28 | +--- |
| 29 | +<p>Some more text</p> |
| 30 | +``` |
| 31 | + |
| 32 | +And our script, `example.js`, looks like so: |
| 33 | + |
| 34 | +```js |
| 35 | +var vfile = require('to-vfile') |
| 36 | +var frontmatter = require('vfile-frontmatter') |
| 37 | + |
| 38 | +var file = vfile.readSync('example.html') |
| 39 | + |
| 40 | +frontmatter(file, {strip: true}) |
| 41 | + |
| 42 | +console.log(file.data) |
| 43 | +console.log(String(file)) |
| 44 | +``` |
| 45 | + |
| 46 | +Now, running our script (`node example`) yields: |
| 47 | + |
| 48 | +```js |
| 49 | +{ frontmatter: { title: 'Hello, world!' } } |
| 50 | +``` |
| 51 | + |
| 52 | +```html |
| 53 | +<p>Some more text</p> |
| 54 | +``` |
| 55 | + |
| 56 | +## API |
| 57 | + |
| 58 | +### `frontmatter(file[, options])` |
| 59 | + |
| 60 | +Parse the YAML frontmatter in a [`vfile`][vfile], and add it as |
| 61 | +`file.data.frontmatter`. |
| 62 | + |
| 63 | +If no frontmatter is found in the file, nothing happens, except that |
| 64 | +`file.data.frontmatter` is set to an empty object (`{}`). |
| 65 | + |
| 66 | +###### Parameters |
| 67 | + |
| 68 | +* `file` ([`VFile`][vfile]) |
| 69 | + — Virtual file |
| 70 | +* `options.strip` (`boolean`, default: `false`) |
| 71 | + — Remove the YAML frontmatter from the file |
| 72 | + |
| 73 | +###### Returns |
| 74 | + |
| 75 | +The given `file`. |
| 76 | + |
| 77 | +## Contribute |
| 78 | + |
| 79 | +See [`contributing.md`][contributing] in [`vfile/.github`][health] for ways to |
| 80 | +get started. |
| 81 | +See [`support.md`][support] for ways to get help. |
| 82 | + |
| 83 | +This project has a [Code of Conduct][coc]. |
| 84 | +By interacting with this repository, organisation, or community you agree to |
| 85 | +abide by its terms. |
| 86 | + |
| 87 | +## License |
| 88 | + |
| 89 | +[MIT][license] © [Titus Wormer][author] |
| 90 | + |
| 91 | +<!-- Definitions --> |
| 92 | + |
| 93 | +[build-badge]: https://img.shields.io/travis/vfile/vfile-frontmatter.svg |
| 94 | + |
| 95 | +[build]: https://travis-ci.org/vfile/vfile-frontmatter |
| 96 | + |
| 97 | +[coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile-frontmatter.svg |
| 98 | + |
| 99 | +[coverage]: https://codecov.io/github/vfile/vfile-frontmatter |
| 100 | + |
| 101 | +[downloads-badge]: https://img.shields.io/npm/dm/vfile-frontmatter.svg |
| 102 | + |
| 103 | +[downloads]: https://www.npmjs.com/package/vfile-frontmatter |
| 104 | + |
| 105 | +[size-badge]: https://img.shields.io/bundlephobia/minzip/vfile-frontmatter.svg |
| 106 | + |
| 107 | +[size]: https://bundlephobia.com/result?p=vfile-frontmatter |
| 108 | + |
| 109 | +[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg |
| 110 | + |
| 111 | +[backers-badge]: https://opencollective.com/unified/backers/badge.svg |
| 112 | + |
| 113 | +[collective]: https://opencollective.com/unified |
| 114 | + |
| 115 | +[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg |
| 116 | + |
| 117 | +[chat]: https://spectrum.chat/unified/vfile |
| 118 | + |
| 119 | +[npm]: https://docs.npmjs.com/cli/install |
| 120 | + |
| 121 | +[contributing]: https://github.com/vfile/.github/blob/master/contributing.md |
| 122 | + |
| 123 | +[support]: https://github.com/vfile/.github/blob/master/support.md |
| 124 | + |
| 125 | +[health]: https://github.com/vfile/.github |
| 126 | + |
| 127 | +[coc]: https://github.com/vfile/.github/blob/master/code-of-conduct.md |
| 128 | + |
| 129 | +[license]: license |
| 130 | + |
| 131 | +[author]: https://wooorm.com |
| 132 | + |
| 133 | +[vfile]: https://github.com/vfile/vfile |
0 commit comments