Skip to content

Commit a3f6d5e

Browse files
committed
Add improved docs
1 parent edc230a commit a3f6d5e

File tree

1 file changed

+123
-34
lines changed

1 file changed

+123
-34
lines changed

readme.md

Lines changed: 123 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,98 @@
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
13-
autolink literals in **[mdast][]**.
14-
When parsing (`from-markdown`), must be combined with
15-
[`micromark-extension-gfm-autolink-literal`][extension].
11+
[mdast][] extensions to parse and serialize [GFM][] autolinks.
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+
* [`gfmAutolinkLiteralFromMarkdown`](#gfmautolinkliteralfrommarkdown)
21+
* [`gfmAutolinkLiteralToMarkdown`](#gfmautolinkliteraltomarkdown)
22+
* [Syntax tree](#syntax-tree)
23+
* [Types](#types)
24+
* [Compatibility](#compatibility)
25+
* [Related](#related)
26+
* [Contribute](#contribute)
27+
* [License](#license)
28+
29+
## What is this?
30+
31+
This package contains extensions that add support for the autolink syntax
32+
enabled by GFM to [`mdast-util-from-markdown`][mdast-util-from-markdown] and
33+
[`mdast-util-to-markdown`][mdast-util-to-markdown].
34+
35+
GitHub employs different algorithms to autolink: one at parse time and one at
36+
transform time (similar to how @mentions are done at transform time).
37+
This difference can be observed because character references and escapes are
38+
handled differently.
39+
But also because issues/PRs/comments omit (perhaps by accident?) the second
40+
algorithm for `www.`, `http://`, and `https://` links (but not for email links).
41+
42+
As the corresponding micromark extension
43+
[`micromark-extension-gfm-autolink-literal`][extension] is a syntax extension,
44+
it can only perform the first algorithm.
45+
The tree extension `gfmAutolinkLiteralFromMarkdown` from this package can
46+
perform the second algorithm, and as they are combined, both are done.
1647

1748
## When to use this
1849

19-
Use [`mdast-util-gfm`][mdast-util-gfm] if you want all of GFM.
20-
Use this otherwise.
50+
These tools are all rather low-level.
51+
In most cases, you’d want to use [`remark-gfm`][remark-gfm] with remark instead.
2152

22-
## Install
53+
When you are working with syntax trees and want all of GFM, use
54+
[`mdast-util-gfm`][mdast-util-gfm] instead.
2355

24-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
25-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
56+
When working with `mdast-util-from-markdown`, you must combine this package with
57+
[`micromark-extension-gfm-autolink-literal`][extension].
2658

27-
[npm][]:
59+
This utility does not handle how markdown is turned to HTML.
60+
That’s done by [`mdast-util-to-hast`][mdast-util-to-hast].
61+
62+
## Install
63+
64+
This package is [ESM only][esm].
65+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
2866

2967
```sh
3068
npm install mdast-util-gfm-autolink-literal
3169
```
3270

71+
In Deno with [`esm.sh`][esmsh]:
72+
73+
```js
74+
import {gfmAutolinkLiteralFromMarkdown, gfmAutolinkLiteralToMarkdown} from 'https://esm.sh/mdast-util-gfm-autolink-literal@1'
75+
```
76+
77+
In browsers with [`esm.sh`][esmsh]:
78+
79+
```html
80+
<script type="module">
81+
import {gfmAutolinkLiteralFromMarkdown, gfmAutolinkLiteralToMarkdown} from 'https://esm.sh/mdast-util-gfm-autolink-literal@1?bundle'
82+
</script>
83+
```
84+
3385
## Use
3486

35-
Say our module, `example.js`, looks as follows:
87+
Say our document `example.md` contains:
88+
89+
```markdown
90+
www.example.com, https://example.com, and [email protected].
91+
```
92+
93+
…and our module `example.js` looks as follows:
3694

3795
```js
96+
import fs from 'node:fs/promises'
3897
import {fromMarkdown} from 'mdast-util-from-markdown'
3998
import {toMarkdown} from 'mdast-util-to-markdown'
4099
import {gfmAutolinkLiteral} from 'micromark-extension-gfm-autolink-literal'
41100
import {gfmAutolinkLiteralFromMarkdown, gfmAutolinkLiteralToMarkdown} from 'mdast-util-gfm-autolink-literal'
42101

43-
const doc = 'www.example.com, https://example.com, and contact@example.com.'
102+
const doc = await fs.readFile('example.md')
44103

45104
const tree = fromMarkdown(doc, {
46105
extensions: [gfmAutolinkLiteral],
@@ -54,7 +113,7 @@ const out = toMarkdown(tree, {extensions: [gfmAutolinkLiteralToMarkdown]})
54113
console.log(out)
55114
```
56115

57-
Now, running `node example` yields:
116+
…now running `node example.js` yields (positional info removed for brevity):
58117

59118
```js
60119
{
@@ -96,34 +155,54 @@ Now, running `node example` yields:
96155

97156
## API
98157

158+
This package exports the identifiers `gfmAutolinkLiteralFromMarkdown` and
159+
`gfmAutolinkLiteralToMarkdown`.
160+
There is no default export.
161+
99162
### `gfmAutolinkLiteralFromMarkdown`
100163

164+
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown].
165+
101166
### `gfmAutolinkLiteralToMarkdown`
102167

103-
Support literal autolinks.
104-
The exports are extensions, respectively
105-
for [`mdast-util-from-markdown`][from-markdown] and
106-
[`mdast-util-to-markdown`][to-markdown].
168+
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown].
169+
170+
## Syntax tree
171+
172+
There are no interfaces added to **[mdast][]** by this utility, as it reuses
173+
the existing [**Link**][dfn-link] interface.
174+
175+
## Types
176+
177+
This package is fully typed with [TypeScript][].
178+
It does not export additional types.
179+
180+
The `Link` node type is supported in `@types/mdast` by default.
181+
182+
## Compatibility
183+
184+
Projects maintained by the unified collective are compatible with all maintained
185+
versions of Node.js.
186+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
187+
Our projects sometimes work with older versions, but this is not guaranteed.
188+
189+
This plugin works with `mdast-util-from-markdown` version 1+ and
190+
`mdast-util-to-markdown` version 1+.
107191

108192
## Related
109193

110-
* [`remarkjs/remark`][remark]
111-
— markdown processor powered by plugins
112194
* [`remarkjs/remark-gfm`][remark-gfm]
113195
— remark plugin to support GFM
114-
* [`micromark/micromark`][micromark]
115-
— the smallest commonmark-compliant markdown parser that exists
196+
* [`syntax-tree/mdast-util-gfm`][mdast-util-gfm]
197+
— same but all of GFM (autolink literals, footnotes, strikethrough, tables,
198+
tasklists)
116199
* [`micromark/micromark-extension-gfm-autolink-literal`][extension]
117200
— micromark extension to parse GFM autolink literals
118-
* [`syntax-tree/mdast-util-from-markdown`][from-markdown]
119-
— mdast parser using `micromark` to create mdast from markdown
120-
* [`syntax-tree/mdast-util-to-markdown`][to-markdown]
121-
— mdast serializer to create markdown from mdast
122201

123202
## Contribute
124203

125-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
126-
started.
204+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
205+
ways to get started.
127206
See [`support.md`][support] for ways to get help.
128207

129208
This project has a [code of conduct][coc].
@@ -164,10 +243,18 @@ abide by its terms.
164243

165244
[npm]: https://docs.npmjs.com/cli/install
166245

246+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
247+
248+
[esmsh]: https://esm.sh
249+
250+
[typescript]: https://www.typescriptlang.org
251+
167252
[license]: license
168253

169254
[author]: https://wooorm.com
170255

256+
[health]: https://github.com/syntax-tree/.github
257+
171258
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
172259

173260
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
@@ -178,14 +265,16 @@ abide by its terms.
178265

179266
[mdast-util-gfm]: https://github.com/syntax-tree/mdast-util-gfm
180267

181-
[remark]: https://github.com/remarkjs/remark
268+
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
182269

183-
[remark-gfm]: https://github.com/remarkjs/remark-gfm
270+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
184271

185-
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
272+
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast
186273

187-
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
188-
189-
[micromark]: https://github.com/micromark/micromark
274+
[remark-gfm]: https://github.com/remarkjs/remark-gfm
190275

191276
[extension]: https://github.com/micromark/micromark-extension-gfm-autolink-literal
277+
278+
[gfm]: https://github.github.com/gfm/
279+
280+
[dfn-link]: https://github.com/syntax-tree/mdast#link

0 commit comments

Comments
 (0)