Skip to content

Commit ada74f4

Browse files
committed
Improve some docs
1 parent d3b990d commit ada74f4

File tree

2 files changed

+194
-44
lines changed

2 files changed

+194
-44
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import {matters} from 'micromark-extension-frontmatter/matters.js'
1515

1616
/**
17+
* Function that can be called to get an extension for
18+
* `mdast-util-from-markdown`.
19+
*
1720
* @param {Options} [options]
1821
* @returns {FromMarkdownExtension}
1922
*/
@@ -64,6 +67,9 @@ function value(token) {
6467
}
6568

6669
/**
70+
* Function that can be called to get an extension for
71+
* `mdast-util-to-markdown`.
72+
*
6773
* @param {Options} [options]
6874
* @returns {ToMarkdownExtension}
6975
*/

readme.md

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

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.
1541

1642
## When to use this
1743

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.
2247

23-
## Install
48+
When working with `mdast-util-from-markdown`, you must combine this package with
49+
[`micromark-extension-frontmatter`][extension].
2450

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
2752

28-
[npm][]:
53+
This package is [ESM only][esm].
54+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
2955

3056
```sh
3157
npm install mdast-util-frontmatter
3258
```
3359

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+
3474
## Use
3575

36-
Say we have the following file, `example.md`:
76+
Say our document `example.md` contains:
3777

3878
```markdown
3979
+++
@@ -43,16 +83,16 @@ title = "New Website"
4383
# Other markdown
4484
```
4585

46-
And our module, `example.js`, looks as follows:
86+
…and our module `example.js` looks as follows:
4787

4888
```js
49-
import fs from 'node:fs'
89+
import fs from 'node:fs/promises'
5090
import {fromMarkdown} from 'mdast-util-from-markdown'
5191
import {toMarkdown} from 'mdast-util-to-markdown'
5292
import {frontmatter} from 'micromark-extension-frontmatter'
5393
import {frontmatterFromMarkdown, frontmatterToMarkdown} from 'mdast-util-frontmatter'
5494

55-
const doc = fs.readFileSync('example.md')
95+
const doc = await fs.readFile('example.md')
5696

5797
const tree = fromMarkdown(doc, {
5898
extensions: [frontmatter(['yaml', 'toml'])],
@@ -66,7 +106,7 @@ const out = toMarkdown(tree, {extensions: [frontmatterToMarkdown(['yaml', 'toml'
66106
console.log(out)
67107
```
68108

69-
Now, running `node example` yields:
109+
…now running `node example.js` yields (positional info removed for brevity):
70110

71111
```js
72112
{
@@ -92,40 +132,132 @@ title = "New Website"
92132

93133
## API
94134

95-
This package exports the following identifiers: `frontmatterFromMarkdown`,
135+
This package exports the identifiers `frontmatterFromMarkdown` and
96136
`frontmatterToMarkdown`.
97137
There is no default export.
98138

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`:
100224

101-
### `frontmatterToMarkdown([options])`
225+
```ts
226+
import type {Literal} from 'mdast'
102227

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+
```
107239

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+.
109249

110250
## Related
111251

112-
* [`remarkjs/remark`][remark]
113-
— markdown processor powered by plugins
114252
* [`remarkjs/remark-frontmatter`][remark-frontmatter]
115253
— remark plugin to support frontmatter
116-
* [`micromark/micromark`][micromark]
117-
— the smallest commonmark-compliant markdown parser that exists
118254
* [`micromark/micromark-extension-frontmatter`][extension]
119255
— 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
124256

125257
## Contribute
126258

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.
129261
See [`support.md`][support] for ways to get help.
130262

131263
This project has a [code of conduct][coc].
@@ -166,28 +298,40 @@ abide by its terms.
166298

167299
[npm]: https://docs.npmjs.com/cli/install
168300

301+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
302+
303+
[esmsh]: https://esm.sh
304+
305+
[typescript]: https://www.typescriptlang.org
306+
169307
[license]: license
170308

171309
[author]: https://wooorm.com
172310

173-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
311+
[health]: https://github.com/syntax-tree/.github
174312

175-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
313+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
176314

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
178316

179-
[mdast]: https://github.com/syntax-tree/mdast
317+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
180318

181-
[remark]: https://github.com/remarkjs/remark
319+
[mdast]: https://github.com/syntax-tree/mdast
182320

183321
[remark-frontmatter]: https://github.com/remarkjs/remark-frontmatter
184322

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
188324

189-
[micromark]: https://github.com/micromark/micromark
325+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
190326

191327
[extension]: https://github.com/micromark/micromark-extension-frontmatter
192328

193329
[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

Comments
 (0)