10
10
11
11
[ ** remark** ] [ remark ] plugin to support frontmatter (YAML, TOML, and more).
12
12
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.
20
56
21
57
## Install
22
58
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] [ ] :
27
61
28
62
``` sh
29
63
npm install remark-frontmatter
30
64
```
31
65
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
+
32
80
## Use
33
81
34
82
Say we have the following file, ` example.md ` :
@@ -44,27 +92,26 @@ title = "New Website"
44
92
And our module, ` example.js ` , looks as follows:
45
93
46
94
``` js
47
- import {readSync } from ' to-vfile'
48
- import {reporter } from ' vfile-reporter'
95
+ import {read } from ' to-vfile'
49
96
import {unified } from ' unified'
50
97
import remarkParse from ' remark-parse'
51
98
import remarkFrontmatter from ' remark-frontmatter'
52
99
import remarkStringify from ' remark-stringify'
53
100
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
+ }
68
115
```
69
116
70
117
Now, running ` node example ` yields:
@@ -84,7 +131,6 @@ Now, running `node example` yields:
84
131
```
85
132
86
133
``` markdown
87
- example.md: no issues found
88
134
+++
89
135
title = "New Website"
90
136
+++
@@ -101,10 +147,134 @@ The default export is `remarkFrontmatter`.
101
147
102
148
Configures remark so that it can parse and serialize frontmatter (YAML, TOML,
103
149
and more).
150
+ Doesn’t parse the data inside them: [ create your own plugin] [ create-plugin ] to
151
+ do that.
104
152
105
153
##### ` options `
106
154
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+.
108
278
109
279
## Security
110
280
@@ -114,14 +284,17 @@ Use of `remark-frontmatter` does not involve [**rehype**][rehype]
114
284
115
285
## Related
116
286
287
+ * [ ` remark-yaml-config ` ] ( https://github.com/remarkjs/remark-yaml-config )
288
+ — configure remark from YAML configuration
117
289
* [ ` 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)
121
291
* [ ` 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
125
298
126
299
## Contribute
127
300
@@ -167,6 +340,8 @@ abide by its terms.
167
340
168
341
[ npm ] : https://docs.npmjs.com/cli/install
169
342
343
+ [ skypack ] : https://www.skypack.dev
344
+
170
345
[ health ] : https://github.com/remarkjs/.github
171
346
172
347
[ contributing ] : https://github.com/remarkjs/.github/blob/HEAD/contributing.md
@@ -179,12 +354,16 @@ abide by its terms.
179
354
180
355
[ author ] : https://wooorm.com
181
356
357
+ [ unified ] : https://github.com/unifiedjs/unified
358
+
182
359
[ remark ] : https://github.com/remarkjs/remark
183
360
184
361
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
185
362
363
+ [ typescript ] : https://www.typescriptlang.org
364
+
186
365
[ rehype ] : https://github.com/rehypejs/rehype
187
366
188
367
[ hast ] : https://github.com/syntax-tree/hast
189
368
190
- [ options ] : https://github .com/micromark/micromark-extension-frontmatter#options
369
+ [ create-plugin ] : https://unifiedjs .com/learn/guide/create-a-plugin/
0 commit comments