Skip to content

Commit b56dae7

Browse files
authored
Add improved doc
Related-to: unifiedjs/unified#168. Closes GH-17.
1 parent 853af5a commit b56dae7

File tree

1 file changed

+215
-36
lines changed

1 file changed

+215
-36
lines changed

readme.md

+215-36
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,73 @@
1010

1111
[**remark**][remark] plugin to support frontmatter (YAML, TOML, and more).
1212

13-
## Important!
14-
15-
This plugin is affected by the new parser in remark
16-
([`micromark`](https://github.com/micromark/micromark),
17-
see [`remarkjs/remark#536`](https://github.com/remarkjs/remark/pull/536)).
18-
Use version 2 while you’re still on remark 12.
19-
Use version 3 for remark 13+.
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+
* [`unified().use(remarkFrontmatter[, options])`](#unifieduseremarkfrontmatter-options)
21+
* [Examples](#examples)
22+
* [Example: custom marker](#example-custom-marker)
23+
* [Example: custom fence](#example-custom-fence)
24+
* [Syntax](#syntax)
25+
* [Syntax tree](#syntax-tree)
26+
* [Types](#types)
27+
* [Compatibility](#compatibility)
28+
* [Security](#security)
29+
* [Related](#related)
30+
* [Contribute](#contribute)
31+
* [License](#license)
32+
33+
## What is this?
34+
35+
This package is a [unified][] ([remark][]) plugin to add support for YAML, TOML,
36+
and other frontmatter.
37+
You can use this to add support for parsing and serializing this syntax
38+
extension.
39+
40+
unified is an AST (abstract syntax tree) based transform project.
41+
**remark** is everything unified that relates to markdown.
42+
The layer under remark is called mdast, which is only concerned with syntax
43+
trees.
44+
Another layer underneath is micromark, which is only concerned with parsing.
45+
This package is a small wrapper to integrate all of these.
46+
47+
## When should I use this?
48+
49+
Frontmatter is a metadata format in front of content.
50+
It’s typically written in YAML and is often used with markdown.
51+
This mechanism works well when you want authors, that have some markup
52+
experience, to configure where or how the content is displayed or supply
53+
metadata about content.
54+
Frontmatter does not work everywhere so it makes markdown less portable.
55+
A good example use case is markdown being rendered by (static) site generators.
2056

2157
## Install
2258

23-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
24-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
25-
26-
[npm][]:
59+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
60+
In Node.js (12.20+, 14.14+, 16.0+), install with [npm][]:
2761

2862
```sh
2963
npm install remark-frontmatter
3064
```
3165

66+
In Deno with [Skypack][]:
67+
68+
```js
69+
import remarkFrontmatter from 'https://cdn.skypack.dev/remark-frontmatter@4?dts'
70+
```
71+
72+
In browsers with [Skypack][]:
73+
74+
```html
75+
<script type="module">
76+
import remarkFrontmatter from 'https://cdn.skypack.dev/remark-frontmatter@4?min'
77+
</script>
78+
```
79+
3280
## Use
3381

3482
Say we have the following file, `example.md`:
@@ -44,27 +92,26 @@ title = "New Website"
4492
And our module, `example.js`, looks as follows:
4593

4694
```js
47-
import {readSync} from 'to-vfile'
48-
import {reporter} from 'vfile-reporter'
95+
import {read} from 'to-vfile'
4996
import {unified} from 'unified'
5097
import remarkParse from 'remark-parse'
5198
import remarkFrontmatter from 'remark-frontmatter'
5299
import remarkStringify from 'remark-stringify'
53100

54-
const file = readSync('example.md')
55-
56-
unified()
57-
.use(remarkParse)
58-
.use(remarkStringify)
59-
.use(remarkFrontmatter, ['yaml', 'toml'])
60-
.use(() => (tree) => {
61-
console.dir(tree)
62-
})
63-
.process(file)
64-
.then((file) => {
65-
console.error(reporter(file))
66-
console.log(String(file))
67-
})
101+
main()
102+
103+
async function main() {
104+
const file = await unified()
105+
.use(remarkParse)
106+
.use(remarkStringify)
107+
.use(remarkFrontmatter, ['yaml', 'toml'])
108+
.use(() => (tree) => {
109+
console.dir(tree)
110+
})
111+
.process(await read('example.md'))
112+
113+
console.log(String(file))
114+
}
68115
```
69116

70117
Now, running `node example` yields:
@@ -84,7 +131,6 @@ Now, running `node example` yields:
84131
```
85132

86133
```markdown
87-
example.md: no issues found
88134
+++
89135
title = "New Website"
90136
+++
@@ -101,10 +147,134 @@ The default export is `remarkFrontmatter`.
101147

102148
Configures remark so that it can parse and serialize frontmatter (YAML, TOML,
103149
and more).
150+
Doesn’t parse the data inside them: [create your own plugin][create-plugin] to
151+
do that.
104152

105153
##### `options`
106154

107-
See [`micromark-extension-frontmatter`][options] for a description of `options`.
155+
One `preset` or `Matter`, or an array of them, defining all the supported
156+
frontmatters (default: `'yaml'`).
157+
158+
##### `preset`
159+
160+
Either `'yaml'` or `'toml'`:
161+
162+
* `'yaml'``Matter` defined as `{type: 'yaml', marker: '-'}`
163+
* `'toml'``Matter` defined as `{type: 'toml', marker: '+'}`
164+
165+
##### `Matter`
166+
167+
An object with a `type` and either a `marker` or a `fence`:
168+
169+
* `type` (`string`)
170+
— Type to tokenize as
171+
* `marker` (`string` or `{open: string, close: string}`)
172+
— Character used to construct fences.
173+
By providing an object with `open` and `close` different characters can be
174+
used for opening and closing fences.
175+
For example the character `'-'` will result in `'---'` being used as the
176+
fence
177+
* `fence` (`string` or `{open: string, close: string}`)
178+
— String used as the complete fence.
179+
By providing an object with `open` and `close` different values can be used
180+
for opening and closing fences.
181+
This can be used too if fences contain different characters or lengths other
182+
than 3
183+
* `anywhere` (`boolean`, default: `false`)
184+
– if `true`, matter can be found anywhere in the document.
185+
If `false` (default), only matter at the start of the document is recognized
186+
187+
## Examples
188+
189+
### Example: custom marker
190+
191+
A custom frontmatter with different open and close markers, repeated 3 times,
192+
that looks like this:
193+
194+
```text
195+
<<<
196+
data
197+
>>>
198+
199+
# hi
200+
```
201+
202+
…can be supported with:
203+
204+
```js
205+
//
206+
.use(remarkFrontmatter, {type: 'custom', marker: {open: '<', close: '>'}})
207+
//
208+
```
209+
210+
### Example: custom fence
211+
212+
A custom frontmatter with custom fences that are not repeated like this:
213+
214+
```text
215+
{
216+
"key": "value"
217+
}
218+
219+
# hi
220+
```
221+
222+
…can be supported with:
223+
224+
```js
225+
//
226+
.use(remarkFrontmatter, {type: 'json', fence: {open: '{', close: '}'}})
227+
//
228+
```
229+
230+
## Syntax
231+
232+
This plugin applies a micromark extensions to parse the syntax.
233+
See its readme for how it works:
234+
235+
* [`micromark-extension-frontmatter`](https://github.com/micromark/micromark-extension-frontmatter)
236+
237+
The syntax supported depends on the given configuration.
238+
239+
## Syntax tree
240+
241+
This plugin applies one mdast utility to build and serialize the AST.
242+
See its readme for how it works:
243+
244+
* [`mdast-util-frontmatter`](https://github.com/syntax-tree/mdast-util-directive)
245+
246+
The node types supported in the tree depend on the given configuration.
247+
248+
## Types
249+
250+
This package is fully typed with [TypeScript][].
251+
The YAML node type is supported in `@types/mdast` by default.
252+
To add other node types, register them by adding them to
253+
`FrontmatterContentMap`:
254+
255+
```ts
256+
import type {Literal} from 'mdast'
257+
258+
interface TOML extends Literal {
259+
type: 'toml'
260+
}
261+
262+
declare module 'mdast' {
263+
interface FrontmatterContentMap {
264+
// Allow using toml nodes defined by `remark-frontmatter`.
265+
toml: TOML
266+
}
267+
}
268+
```
269+
270+
## Compatibility
271+
272+
Projects maintained by the unified collective are compatible with all maintained
273+
versions of Node.js.
274+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
275+
Our projects sometimes work with older versions, but this is not guaranteed.
276+
277+
This plugin works with unified 6+ and remark 13+.
108278

109279
## Security
110280

@@ -114,14 +284,17 @@ Use of `remark-frontmatter` does not involve [**rehype**][rehype]
114284

115285
## Related
116286

287+
* [`remark-yaml-config`](https://github.com/remarkjs/remark-yaml-config)
288+
— configure remark from YAML configuration
117289
* [`remark-gfm`](https://github.com/remarkjs/remark-gfm)
118-
— GitHub Flavored Markdown
119-
* [`remark-math`](https://github.com/remarkjs/remark-math)
120-
— Math
290+
— support GFM (autolink literals, strikethrough, tables, tasklists)
121291
* [`remark-github`](https://github.com/remarkjs/remark-github)
122-
— Auto-link references like in GitHub issues, PRs, and comments
123-
* [`remark-yaml-config`](https://github.com/remarkjs/remark-yaml-config)
124-
— Configure remark from YAML configuration
292+
— link references to commits, issues, pull-requests, and users, like on
293+
GitHub
294+
* [`remark-directive`](https://github.com/remarkjs/remark-directive)
295+
— support directives
296+
* [`remark-math`](https://github.com/remarkjs/remark-math)
297+
— support math
125298

126299
## Contribute
127300

@@ -167,6 +340,8 @@ abide by its terms.
167340

168341
[npm]: https://docs.npmjs.com/cli/install
169342

343+
[skypack]: https://www.skypack.dev
344+
170345
[health]: https://github.com/remarkjs/.github
171346

172347
[contributing]: https://github.com/remarkjs/.github/blob/HEAD/contributing.md
@@ -179,12 +354,16 @@ abide by its terms.
179354

180355
[author]: https://wooorm.com
181356

357+
[unified]: https://github.com/unifiedjs/unified
358+
182359
[remark]: https://github.com/remarkjs/remark
183360

184361
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
185362

363+
[typescript]: https://www.typescriptlang.org
364+
186365
[rehype]: https://github.com/rehypejs/rehype
187366

188367
[hast]: https://github.com/syntax-tree/hast
189368

190-
[options]: https://github.com/micromark/micromark-extension-frontmatter#options
369+
[create-plugin]: https://unifiedjs.com/learn/guide/create-a-plugin/

0 commit comments

Comments
 (0)