Skip to content

Commit 2ee5cdc

Browse files
committed
Add improved docs
1 parent 812189a commit 2ee5cdc

File tree

1 file changed

+136
-46
lines changed

1 file changed

+136
-46
lines changed

readme.md

Lines changed: 136 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,48 @@
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 GitHub flavored markdown in
13-
**[mdast][]**.
14-
When parsing (`from-markdown`), must be combined with
15-
[`micromark-extension-gfm`][extension].
11+
[mdast][] extensions to parse and serialize [GFM][] (autolink literals,
12+
footnotes, strikethrough, tables, tasklists).
13+
14+
## Contents
15+
16+
* [What is this?](#what-is-this)
17+
* [When to use this](#when-to-use-this)
18+
* [Install](#install)
19+
* [Use](#use)
20+
* [API](#api)
21+
* [`gfmFromMarkdown()`](#gfmfrommarkdown)
22+
* [`gfmToMarkdown(options?)`](#gfmtomarkdownoptions)
23+
* [Syntax tree](#syntax-tree)
24+
* [Types](#types)
25+
* [Compatibility](#compatibility)
26+
* [Related](#related)
27+
* [Contribute](#contribute)
28+
* [License](#license)
29+
30+
## What is this?
31+
32+
This package contains extensions for
33+
[`mdast-util-from-markdown`][mdast-util-from-markdown] and
34+
[`mdast-util-to-markdown`][mdast-util-to-markdown] to enable the features that
35+
GitHub adds to markdown: autolink literals (`www.x.com`), footnotes (`[^1]`),
36+
strikethrough (`~~stuff~~`), tables (`| cell |…`), and tasklists (`* [x]`).
1637

1738
## When to use this
1839

19-
Use this if you’re dealing with the AST manually and need all of GFM.
20-
It’s probably nicer to use [`remark-gfm`][remark-gfm] with
21-
**[remark][]**, which includes this but provides a nicer interface and
22-
makes it easier to combine with hundreds of plugins.
40+
These tools are all rather low-level.
41+
In many cases, you’d want to use [`remark-gfm`][remark-gfm] with remark instead.
42+
43+
This project is useful when you want to support the same features that GitHub
44+
does in files in a repo, Gists, and several other places.
45+
Users frequently believe that some of these extensions, specifically autolink
46+
literals and tables, are part of normal markdown, so using `mdast-util-gfm` will
47+
help match your implementation to their understanding of markdown.
48+
There are several edge cases where GitHub’s implementation works in unexpected
49+
ways or even different than described in their spec, so *writing* in GFM is not
50+
always the best choice.
2351

24-
Alternatively, the extensions can be used separately:
52+
Instead of this package, you can also use the extensions separately:
2553

2654
* [`syntax-tree/mdast-util-gfm-autolink-literal`](https://github.com/syntax-tree/mdast-util-gfm-autolink-literal)
2755
— support GFM autolink literals
@@ -34,20 +62,44 @@ Alternatively, the extensions can be used separately:
3462
* [`syntax-tree/mdast-util-gfm-task-list-item`](https://github.com/syntax-tree/mdast-util-gfm-task-list-item)
3563
— support GFM tasklists
3664

37-
## Install
65+
When working with `mdast-util-from-markdown`, you must combine this package with
66+
[`micromark-extension-gfm`][extension].
3867

39-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
40-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
68+
A different utility, [`mdast-util-frontmatter`][mdast-util-frontmatter], adds
69+
support for frontmatter.
70+
GitHub supports YAML frontmatter for files in repos and Gists but they don’t
71+
treat it as part of GFM.
4172

42-
[npm][]:
73+
This utility does not handle how markdown is turned to HTML.
74+
That’s done by [`mdast-util-to-hast`][mdast-util-to-hast].
75+
If your content is not in English, you should configure that utility.
76+
77+
## Install
78+
79+
This package is [ESM only][esm].
80+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
4381

4482
```sh
4583
npm install mdast-util-gfm
4684
```
4785

86+
In Deno with [`esm.sh`][esmsh]:
87+
88+
```js
89+
import {gfmFromMarkdown, gfmToMarkdown} from 'https://esm.sh/mdast-util-gfm@2'
90+
```
91+
92+
In browsers with [`esm.sh`][esmsh]:
93+
94+
```html
95+
<script type="module">
96+
import {gfmFromMarkdown, gfmToMarkdown} from 'https://esm.sh/mdast-util-gfm@2?bundle'
97+
</script>
98+
```
99+
48100
## Use
49101

50-
Say we have the following file, `example.md`:
102+
Say our document `example.md` contains:
51103

52104
```markdown
53105
# GFM
@@ -77,16 +129,16 @@ A note[^1]
77129
* [x] done
78130
```
79131

80-
And our module, `example.js`, looks as follows:
132+
…and our module `example.js` looks as follows:
81133

82134
```js
83-
import fs from 'node:fs'
135+
import fs from 'node:fs/promises'
84136
import {fromMarkdown} from 'mdast-util-from-markdown'
85137
import {toMarkdown} from 'mdast-util-to-markdown'
86138
import {gfm} from 'micromark-extension-gfm'
87139
import {gfmFromMarkdown, gfmToMarkdown} from 'mdast-util-gfm'
88140

89-
const doc = fs.readFileSync('example.md')
141+
const doc = await fs.readFile('example.md')
90142

91143
const tree = fromMarkdown(doc, {
92144
extensions: [gfm()],
@@ -100,7 +152,7 @@ const out = toMarkdown(tree, {extensions: [gfmToMarkdown()]})
100152
console.log(out)
101153
```
102154

103-
Now, running `node example` yields:
155+
…now running `node example.js` yields (positional info removed for brevity):
104156

105157
```js
106158
{
@@ -249,43 +301,71 @@ A note[^1]
249301

250302
## API
251303

252-
This package exports the following identifiers: `gfmFromMarkdown`,
253-
`gfmToMarkdown`.
304+
This package exports the identifiers `gfmFromMarkdown` and `gfmToMarkdown`.
254305
There is no default export.
255306

256307
### `gfmFromMarkdown()`
257308

309+
Function that can be called to get an extension for
310+
[`mdast-util-from-markdown`][mdast-util-from-markdown].
311+
258312
### `gfmToMarkdown(options?)`
259313

260-
Support GFM.
261-
The export of `fromMarkdown` is a function that can be called and returns an
262-
extension for [`mdast-util-from-markdown`][from-markdown].
263-
The export of `toMarkdown` is a function that can be called with options and
264-
returns an extension for [`mdast-util-to-markdown`][to-markdown].
314+
Function that can be called to get an extension for
315+
[`mdast-util-to-markdown`][mdast-util-to-markdown].
316+
317+
##### `options`
265318

266-
###### `options`
319+
Configuration (optional).
320+
Currently passes through `tableCellPadding`, `tablePipeAlign`, and
321+
`stringLength` to [`mdast-util-gfm-table`][table].
267322

268-
Passed as `options` to [`mdast-util-gfm-table`][table].
323+
## Syntax tree
324+
325+
This utility combines several mdast utilities.
326+
See their readmes for the node types supported in the tree:
327+
328+
* [`syntax-tree/mdast-util-gfm-autolink-literal`](https://github.com/syntax-tree/mdast-util-gfm-autolink-literal#syntax-tree)
329+
— GFM autolink literals
330+
* [`syntax-tree/mdast-util-gfm-footnote`](https://github.com/syntax-tree/mdast-util-gfm-footnote#syntax-tree)
331+
— GFM footnotes
332+
* [`syntax-tree/mdast-util-gfm-strikethrough`](https://github.com/syntax-tree/mdast-util-gfm-strikethrough#syntax-tree)
333+
— GFM strikethrough
334+
* [`syntax-tree/mdast-util-gfm-table`](https://github.com/syntax-tree/mdast-util-gfm-table#syntax-tree)
335+
— GFM tables
336+
* [`syntax-tree/mdast-util-gfm-task-list-item`](https://github.com/syntax-tree/mdast-util-gfm-task-list-item#syntax-tree)
337+
— GFM tasklists
338+
339+
## Types
340+
341+
This package is fully typed with [TypeScript][].
342+
It exports an `Options` type, which specifies the interface of the accepted
343+
options.
344+
345+
The `Delete`, `FootnoteDefinition`, `FootnoteReference`, `Table`, `TableCell`,
346+
and `TableRow` node types are supported in `@types/mdast` by default.
347+
348+
## Compatibility
349+
350+
Projects maintained by the unified collective are compatible with all maintained
351+
versions of Node.js.
352+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
353+
Our projects sometimes work with older versions, but this is not guaranteed.
354+
355+
This plugin works with `mdast-util-from-markdown` version 1+ and
356+
`mdast-util-to-markdown` version 1+.
269357

270358
## Related
271359

272-
* [`remarkjs/remark`][remark]
273-
— markdown processor powered by plugins
274360
* [`remarkjs/remark-gfm`][remark-gfm]
275361
— remark plugin to support GFM
276-
* [`micromark/micromark`][micromark]
277-
— the smallest commonmark-compliant markdown parser that exists
278362
* [`micromark/micromark-extension-gfm`][extension]
279363
— micromark extension to parse GFM
280-
* [`syntax-tree/mdast-util-from-markdown`][from-markdown]
281-
— mdast parser using `micromark` to create mdast from markdown
282-
* [`syntax-tree/mdast-util-to-markdown`][to-markdown]
283-
— mdast serializer to create markdown from mdast
284364

285365
## Contribute
286366

287-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
288-
started.
367+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
368+
ways to get started.
289369
See [`support.md`][support] for ways to get help.
290370

291371
This project has a [code of conduct][coc].
@@ -326,28 +406,38 @@ abide by its terms.
326406

327407
[npm]: https://docs.npmjs.com/cli/install
328408

409+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
410+
411+
[esmsh]: https://esm.sh
412+
413+
[typescript]: https://www.typescriptlang.org
414+
329415
[license]: license
330416

331417
[author]: https://wooorm.com
332418

333-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
419+
[health]: https://github.com/syntax-tree/.github
334420

335-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
421+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
336422

337-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
423+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
338424

339-
[mdast]: https://github.com/syntax-tree/mdast
425+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
340426

341-
[remark]: https://github.com/remarkjs/remark
427+
[mdast]: https://github.com/syntax-tree/mdast
342428

343429
[remark-gfm]: https://github.com/remarkjs/remark-gfm
344430

345-
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
431+
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
432+
433+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
346434

347-
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
435+
[mdast-util-frontmatter]: https://github.com/syntax-tree/mdast-util-frontmatter
348436

349-
[micromark]: https://github.com/micromark/micromark
437+
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast
350438

351439
[extension]: https://github.com/micromark/micromark-extension-gfm
352440

353441
[table]: https://github.com/syntax-tree/mdast-util-gfm-table#options
442+
443+
[gfm]: https://github.github.com/gfm/

0 commit comments

Comments
 (0)