Skip to content

Commit 01838d4

Browse files
committed
Add improved docs
1 parent 1bd1da2 commit 01838d4

File tree

1 file changed

+114
-22
lines changed

1 file changed

+114
-22
lines changed

readme.md

Lines changed: 114 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,68 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
Parse the YAML front matter in a [`vfile`][vfile].
11+
[vfile][] utility parse YAML front matter.
1212

13-
## Install
13+
## Contents
14+
15+
* [What is this?](#what-is-this)
16+
* [When should I use this?](#when-should-i-use-this)
17+
* [Install](#install)
18+
* [Use](#use)
19+
* [API](#api)
20+
* [`matter(file[, options])`](#matterfile-options)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Contribute](#contribute)
24+
* [License](#license)
25+
26+
## What is this?
27+
28+
This package parses YAML frontmatter, when found in a file, and exposes it as
29+
`file.data.matter`.
30+
It can optionally strip the frontmatter, which is useful for languages that do
31+
not understand frontmatter, but stripping can make it harder to deal with
32+
languages that *do* understand it, such as markdown, because it messes up
33+
positional info of warnings and errors.
1434

15-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
35+
## When should I use this?
36+
37+
Frontmatter is a metadata format in front of content.
38+
It’s typically written in YAML and is often used with markdown.
39+
This mechanism works well when you want authors, that have some markup
40+
experience, to configure where or how the content is displayed or supply
41+
metadata about content.
42+
43+
When using vfiles with markdown, you are likely also using [remark][], in which
44+
case you should use [`remark-frontmatter`][remark-frontmatter], instead of
45+
stripping frontmatter.
46+
47+
## Install
1748

18-
[npm][]:
49+
This package is [ESM only][esm].
50+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
1951

2052
```sh
2153
npm install vfile-matter
2254
```
2355

56+
In Deno with [`esm.sh`][esmsh]:
57+
58+
```js
59+
import {matter} from 'https://esm.sh/vfile-matter@3'
60+
```
61+
62+
In browsers with [`esm.sh`][esmsh]:
63+
64+
```html
65+
<script type="module">
66+
import {matter} from 'https://esm.sh/vfile-matter@3?bundle'
67+
</script>
68+
```
69+
2470
## Use
2571

26-
Say we have a file, `example.html`:
72+
Say our document `example.html` contains:
2773

2874
```html
2975
---
@@ -32,21 +78,21 @@ title: Hello, world!
3278
<p>Some more text</p>
3379
```
3480

35-
And our script, `example.js`, looks like so:
81+
…and our module `example.js` looks as follows:
3682

3783
```js
38-
import {toVFile as vfile} from 'to-vfile'
84+
import {read} from 'to-vfile'
3985
import {matter} from 'vfile-matter'
4086

41-
var file = vfile.readSync('example.html')
87+
const file = await read('example.html')
4288

4389
matter(file, {strip: true})
4490

4591
console.log(file.data)
4692
console.log(String(file))
4793
```
4894

49-
Now, running our script (`node example`) yields:
95+
…now running `node example.js` yields:
5096

5197
```js
5298
{matter: {title: 'Hello, world!'}}
@@ -58,7 +104,7 @@ Now, running our script (`node example`) yields:
58104

59105
## API
60106

61-
This package exports the following identifiers: `matter`.
107+
This package exports the identifier `matter`.
62108
There is no default export.
63109

64110
### `matter(file[, options])`
@@ -69,18 +115,52 @@ Parse the YAML front matter in a [`vfile`][vfile], and add it as
69115
If no matter is found in the file, nothing happens, except that
70116
`file.data.matter` is set to an empty object (`{}`).
71117

72-
###### Parameters
118+
##### `options`
73119

74-
* `file` ([`VFile`][vfile])
75-
— Virtual file
76-
* `options.strip` (`boolean`, default: `false`)
77-
— Remove the YAML front matter from the file
78-
* `options.yaml` (`Object`, default: `{}`)
79-
— Options passed to `jsYaml.load()`
120+
Configuration (optional).
121+
122+
###### `options.strip`
123+
124+
Remove the YAML front matter from the file (`boolean`, default: `false`).
125+
126+
###### `options.yaml`
127+
128+
Options for the YAML parser (default: `{}`).
129+
These are passed to [`yaml`][yaml] as `x` in `yaml.parse('', x)`, which is
130+
equivalent to the combination of
131+
[`ParseOptions`](https://eemeli.org/yaml/#parse-options),
132+
[`DocumentOptions`](https://eemeli.org/yaml/#document-options),
133+
[`SchemaOptions`](https://eemeli.org/yaml/#schema-options), and
134+
[`ToJsOptions`](https://eemeli.org/yaml/#tojs-options).
80135

81136
###### Returns
82137

83-
The given `file`.
138+
The given `file` ([`VFile`][vfile]).
139+
140+
## Types
141+
142+
This package is fully typed with [TypeScript][].
143+
It exports the additional types `YamlOptions` and `Options`.
144+
145+
To type `file.data.matter`, you can augment `DataMap` from `vfile` as follows:
146+
147+
```ts
148+
declare module 'vfile' {
149+
interface DataMap {
150+
matter: {
151+
// `file.data.matter.string` is typed as `string?`.
152+
title?: string
153+
}
154+
}
155+
}
156+
```
157+
158+
## Compatibility
159+
160+
Projects maintained by the unified collective are compatible with all maintained
161+
versions of Node.js.
162+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
163+
Our projects sometimes work with older versions, but this is not guaranteed.
84164

85165
## Contribute
86166

@@ -126,16 +206,28 @@ abide by its terms.
126206

127207
[npm]: https://docs.npmjs.com/cli/install
128208

129-
[contributing]: https://github.com/vfile/.github/blob/HEAD/contributing.md
209+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
210+
211+
[esmsh]: https://esm.sh
212+
213+
[typescript]: https://www.typescriptlang.org
130214

131-
[support]: https://github.com/vfile/.github/blob/HEAD/support.md
215+
[contributing]: https://github.com/vfile/.github/blob/main/contributing.md
216+
217+
[support]: https://github.com/vfile/.github/blob/main/support.md
132218

133219
[health]: https://github.com/vfile/.github
134220

135-
[coc]: https://github.com/vfile/.github/blob/HEAD/code-of-conduct.md
221+
[coc]: https://github.com/vfile/.github/blob/main/code-of-conduct.md
136222

137223
[license]: license
138224

139225
[author]: https://wooorm.com
140226

141227
[vfile]: https://github.com/vfile/vfile
228+
229+
[remark]: https://github.com/remarkjs/remark
230+
231+
[remark-frontmatter]: https://github.com/remarkjs/remark-frontmatter
232+
233+
[yaml]: https://github.com/eemeli/yaml

0 commit comments

Comments
 (0)