8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- Extension for [ ` mdast-util-from-markdown ` ] [ from-markdown ] and/or
12
- [ ` mdast-util-to-markdown ` ] [ to-markdown ] to support frontmatter in ** [ mdast] [ ] ** .
13
- When parsing (` from-markdown ` ), must be combined with
14
- [ ` micromark-extension-frontmatter ` ] [ extension ] .
11
+ [ mdast] [ ] extensions to parse and serialize frontmatter (YAML, TOML, and more).
12
+
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When to use this] ( #when-to-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` frontmatterFromMarkdown(options?) ` ] ( #frontmatterfrommarkdownoptions )
21
+ * [ ` frontmatterToMarkdown(options?) ` ] ( #frontmattertomarkdownoptions )
22
+ * [ Syntax tree] ( #syntax-tree )
23
+ * [ Nodes] ( #nodes )
24
+ * [ Content model] ( #content-model )
25
+ * [ Types] ( #types )
26
+ * [ Compatibility] ( #compatibility )
27
+ * [ Related] ( #related )
28
+ * [ Contribute] ( #contribute )
29
+ * [ License] ( #license )
30
+
31
+ ## What is this?
32
+
33
+ This package contains extensions that add support for the frontmatter syntax
34
+ enabled by GitHub and many other places to
35
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] and
36
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
37
+
38
+ Frontmatter is a metadata format in front of content.
39
+ It’s typically written in YAML and is often used with markdown.
40
+ Frontmatter does not work everywhere so it makes markdown less portable.
15
41
16
42
## When to use this
17
43
18
- Use this if you’re dealing with the AST manually.
19
- It’s probably nicer to use [ ` remark-frontmatter ` ] [ remark-frontmatter ] with
20
- ** [ remark] [ ] ** , which includes this but provides a nicer interface and
21
- makes it easier to combine with hundreds of plugins.
44
+ These tools are all rather low-level.
45
+ In most cases, you’d want to use [ ` remark-frontmatter ` ] [ remark-frontmatter ] with
46
+ remark instead.
22
47
23
- ## Install
48
+ When working with ` mdast-util-from-markdown ` , you must combine this package with
49
+ [ ` micromark-extension-frontmatter ` ] [ extension ] .
24
50
25
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
26
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
51
+ ## Install
27
52
28
- [ npm] [ ] :
53
+ This package is [ ESM only] [ esm ] .
54
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
29
55
30
56
``` sh
31
57
npm install mdast-util-frontmatter
32
58
```
33
59
60
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
61
+
62
+ ``` js
63
+ import {frontmatterFromMarkdown , frontmatterToMarkdown } from ' https://esm.sh/mdast-util-frontmatter@1'
64
+ ```
65
+
66
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
67
+
68
+ ``` html
69
+ <script type =" module" >
70
+ import {frontmatterFromMarkdown , frontmatterToMarkdown } from ' https://esm.sh/mdast-util-frontmatter@1?bundle'
71
+ </script >
72
+ ```
73
+
34
74
## Use
35
75
36
- Say we have the following file, ` example.md ` :
76
+ Say our document ` example.md ` contains :
37
77
38
78
``` markdown
39
79
+++
@@ -43,16 +83,16 @@ title = "New Website"
43
83
# Other markdown
44
84
```
45
85
46
- And our module, ` example.js ` , looks as follows:
86
+ …and our module ` example.js ` looks as follows:
47
87
48
88
``` js
49
- import fs from ' node:fs'
89
+ import fs from ' node:fs/promises '
50
90
import {fromMarkdown } from ' mdast-util-from-markdown'
51
91
import {toMarkdown } from ' mdast-util-to-markdown'
52
92
import {frontmatter } from ' micromark-extension-frontmatter'
53
93
import {frontmatterFromMarkdown , frontmatterToMarkdown } from ' mdast-util-frontmatter'
54
94
55
- const doc = fs .readFileSync (' example.md' )
95
+ const doc = await fs .readFile (' example.md' )
56
96
57
97
const tree = fromMarkdown (doc, {
58
98
extensions: [frontmatter ([' yaml' , ' toml' ])],
@@ -66,7 +106,7 @@ const out = toMarkdown(tree, {extensions: [frontmatterToMarkdown(['yaml', 'toml'
66
106
console .log (out)
67
107
```
68
108
69
- Now, running ` node example ` yields:
109
+ …now running ` node example.js ` yields (positional info removed for brevity) :
70
110
71
111
``` js
72
112
{
@@ -92,40 +132,132 @@ title = "New Website"
92
132
93
133
## API
94
134
95
- This package exports the following identifiers: ` frontmatterFromMarkdown ` ,
135
+ This package exports the identifiers ` frontmatterFromMarkdown ` and
96
136
` frontmatterToMarkdown ` .
97
137
There is no default export.
98
138
99
- ### ` frontmatterFromMarkdown([options]) `
139
+ ### ` frontmatterFromMarkdown(options?) `
140
+
141
+ Function that can be called to get an extension for
142
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
143
+
144
+ ###### ` options `
145
+
146
+ Configuration (optional).
147
+ Same as [ ` micromark-extension-frontmatter ` ] [ options ] .
148
+
149
+ ### ` frontmatterToMarkdown(options?) `
150
+
151
+ Function that can be called to get an extension for
152
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
153
+
154
+ ###### ` options `
155
+
156
+ Configuration (optional).
157
+ Same as [ ` micromark-extension-frontmatter ` ] [ options ] .
158
+
159
+ ## Syntax tree
160
+
161
+ The following interfaces are added to ** [ mdast] [ ] ** by this utility.
162
+
163
+ ### Nodes
164
+
165
+ > 👉 ** Note** : other nodes are not enabled by default, but when passing options
166
+ > to enable them, they work the same as YAML.
167
+
168
+ #### ` YAML `
169
+
170
+ ``` idl
171
+ interface YAML <: Literal {
172
+ type: "yaml"
173
+ }
174
+ ```
175
+
176
+ ** YAML** ([ ** Literal** ] [ dfn-literal ] ) represents a collection of metadata for
177
+ the document in the YAML data serialisation language.
178
+
179
+ ** YAML** can be used where [ ** frontmatter** ] [ dfn-frontmatter-content ] content is
180
+ expected.
181
+ Its content is represented by its ` value ` field.
182
+
183
+ For example, the following markdown:
184
+
185
+ ``` markdown
186
+ ---
187
+ foo: bar
188
+ ---
189
+ ```
190
+
191
+ Yields:
192
+
193
+ ``` js
194
+ {type: ' yaml' , value: ' foo: bar' }
195
+ ```
196
+
197
+ ### Content model
198
+
199
+ #### ` FrontmatterContent `
200
+
201
+ ``` idl
202
+ type FrontmatterContent = YAML
203
+ ```
204
+
205
+ ** Frontmatter** content represent out-of-band information about the document.
206
+
207
+ If frontmatter is present, it must be limited to one node in the
208
+ [ * tree* ] [ term-tree ] , and can only exist as a [ * head* ] [ term-head ] .
209
+
210
+ #### ` FlowContent ` (frontmatter)
211
+
212
+ ``` idl
213
+ type FlowContentFrontmatter = FrontmatterContent | FlowContent
214
+ ```
215
+
216
+ ## Types
217
+
218
+ This package is fully typed with [ TypeScript] [ ] .
219
+ It exports the additional types ` Options ` , ` Matter ` , and ` Info ` .
220
+
221
+ The YAML node type is supported in ` @types/mdast ` by default.
222
+ To add other node types, register them by adding them to
223
+ ` FrontmatterContentMap ` :
100
224
101
- ### ` frontmatterToMarkdown([options]) `
225
+ ``` ts
226
+ import type {Literal } from ' mdast'
102
227
103
- Support frontmatter (YAML, TOML, and more).
104
- These functions can be called with options and return extensions, respectively
105
- for [ ` mdast-util-from-markdown ` ] [ from-markdown ] and
106
- [ ` mdast-util-to-markdown ` ] [ to-markdown ] .
228
+ interface TOML extends Literal {
229
+ type: ' toml'
230
+ }
231
+
232
+ declare module ' mdast' {
233
+ interface FrontmatterContentMap {
234
+ // Allow using TOML nodes defined by `mdast-util-frontmatter`.
235
+ toml: TOML
236
+ }
237
+ }
238
+ ```
107
239
108
- Options are the same as [ ` micromark-extension-frontmatter ` ] [ options ] .
240
+ ## Compatibility
241
+
242
+ Projects maintained by the unified collective are compatible with all maintained
243
+ versions of Node.js.
244
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
245
+ Our projects sometimes work with older versions, but this is not guaranteed.
246
+
247
+ This plugin works with ` mdast-util-from-markdown ` version 1+ and
248
+ ` mdast-util-to-markdown ` version 1+.
109
249
110
250
## Related
111
251
112
- * [ ` remarkjs/remark ` ] [ remark ]
113
- — markdown processor powered by plugins
114
252
* [ ` remarkjs/remark-frontmatter ` ] [ remark-frontmatter ]
115
253
— remark plugin to support frontmatter
116
- * [ ` micromark/micromark ` ] [ micromark ]
117
- — the smallest commonmark-compliant markdown parser that exists
118
254
* [ ` micromark/micromark-extension-frontmatter ` ] [ extension ]
119
255
— micromark extension to parse frontmatter
120
- * [ ` syntax-tree/mdast-util-from-markdown ` ] [ from-markdown ]
121
- — mdast parser using ` micromark ` to create mdast from markdown
122
- * [ ` syntax-tree/mdast-util-to-markdown ` ] [ to-markdown ]
123
- — mdast serializer to create markdown from mdast
124
256
125
257
## Contribute
126
258
127
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
128
- started.
259
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
260
+ ways to get started.
129
261
See [ ` support.md ` ] [ support ] for ways to get help.
130
262
131
263
This project has a [ code of conduct] [ coc ] .
@@ -166,28 +298,40 @@ abide by its terms.
166
298
167
299
[ npm ] : https://docs.npmjs.com/cli/install
168
300
301
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
302
+
303
+ [ esmsh ] : https://esm.sh
304
+
305
+ [ typescript ] : https://www.typescriptlang.org
306
+
169
307
[ license ] : license
170
308
171
309
[ author ] : https://wooorm.com
172
310
173
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
311
+ [ health ] : https://github.com/syntax-tree/.github
174
312
175
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
313
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
176
314
177
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct .md
315
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support .md
178
316
179
- [ mdast ] : https://github.com/syntax-tree/mdast
317
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
180
318
181
- [ remark ] : https://github.com/remarkjs/remark
319
+ [ mdast ] : https://github.com/syntax-tree/mdast
182
320
183
321
[ remark-frontmatter ] : https://github.com/remarkjs/remark-frontmatter
184
322
185
- [ from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
186
-
187
- [ to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
323
+ [ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
188
324
189
- [ micromark ] : https://github.com/micromark/micromark
325
+ [ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
190
326
191
327
[ extension ] : https://github.com/micromark/micromark-extension-frontmatter
192
328
193
329
[ options ] : https://github.com/micromark/micromark-extension-frontmatter#options
330
+
331
+ [ dfn-literal ] : https://github.com/syntax-tree/mdast#literal
332
+
333
+ [ dfn-frontmatter-content ] : #frontmattercontent
334
+
335
+ [ term-tree ] : https://github.com/syntax-tree/unist#tree
336
+
337
+ [ term-head ] : https://github.com/syntax-tree/unist#head
0 commit comments