Skip to content

Commit d9a0849

Browse files
committed
Add improved docs
1 parent 6c3c79f commit d9a0849

File tree

1 file changed

+121
-45
lines changed

1 file changed

+121
-45
lines changed

readme.md

Lines changed: 121 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,91 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
**[mdast][]** utility to parse markdown.
12-
13-
## When to use this
14-
15-
Use this if you want to use **[micromark][]** but need an AST.
16-
Use **[remark][]** instead, which includes both to provide a nice interface and
17-
hundreds of plugins.
11+
**[mdast][]** utility that turns markdown into a syntax tree.
12+
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+
* [`fromMarkdown(doc[, encoding][, options])`](#frommarkdowndoc-encoding-options)
21+
* [List of extensions](#list-of-extensions)
22+
* [Syntax](#syntax)
23+
* [Syntax tree](#syntax-tree)
24+
* [Types](#types)
25+
* [Compatibility](#compatibility)
26+
* [Security](#security)
27+
* [Related](#related)
28+
* [Contribute](#contribute)
29+
* [License](#license)
30+
31+
## What is this?
32+
33+
This package is a utility that takes markdown input and turns it into an
34+
[mdast][] syntax tree.
35+
36+
This utility uses [`micromark`][micromark], which turns markdown into tokens,
37+
while it turns those tokens into nodes.
38+
It’s used in [`remark-parse`][remark-parse], which focusses on making it easier
39+
to transform content by abstracting these internals away.
40+
41+
## When should I use this?
42+
43+
If you want to handle syntax trees manually, use this.
44+
Use [`micromark`][micromark] instead when you *just* want to turn markdown into
45+
HTML.
46+
For an easier time processing content, use the **[remark][]** ecosystem instead.
1847

1948
## Install
2049

21-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
22-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
23-
24-
[npm][]:
50+
This package is [ESM only][esm].
51+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
2552

2653
```sh
2754
npm install mdast-util-from-markdown
2855
```
2956

57+
In Deno with [Skypack][]:
58+
59+
```js
60+
import {fromMarkdown} from 'https://cdn.skypack.dev/mdast-util-from-markdown@1?dts'
61+
```
62+
63+
In browsers with [Skypack][]:
64+
65+
```html
66+
<script type="module">
67+
import {fromMarkdown} from 'https://cdn.skypack.dev/mdast-util-from-markdown@1?min'
68+
</script>
69+
```
70+
3071
## Use
3172

32-
Say we have the following markdown file, `example.md`:
73+
Say we have the following markdown file `example.md`:
3374

3475
```markdown
3576
## Hello, *World*!
3677
```
3778

38-
And our script, `example.js`, looks as follows:
79+
…and our module `example.js` looks as follows:
3980

4081
```js
41-
import fs from 'node:fs'
82+
import {promises as fs} from 'node:fs'
4283
import {fromMarkdown} from 'mdast-util-from-markdown'
4384

44-
const doc = fs.readFileSync('example.md')
85+
main()
4586

46-
const tree = fromMarkdown(doc)
87+
async function main() {
88+
const doc = await fs.readFile('example.md')
89+
const tree = fromMarkdown(doc)
4790

48-
console.log(tree)
91+
console.log(tree)
92+
}
4993
```
5094

51-
Now, running `node example` yields (positional info removed for brevity):
95+
…now running `node example.js` yields (positional info removed for brevity):
5296

5397
```js
5498
{
@@ -59,10 +103,7 @@ Now, running `node example` yields (positional info removed for brevity):
59103
depth: 2,
60104
children: [
61105
{type: 'text', value: 'Hello, '},
62-
{
63-
type: 'emphasis',
64-
children: [{type: 'text', value: 'World'}]
65-
},
106+
{type: 'emphasis', children: [{type: 'text', value: 'World'}]},
66107
{type: 'text', value: '!'}
67108
]
68109
}
@@ -82,7 +123,7 @@ Without this condition, production code is loaded.
82123

83124
### `fromMarkdown(doc[, encoding][, options])`
84125

85-
Parse markdown to a **[mdast][]** tree.
126+
Turn markdown into a syntax tree.
86127

87128
##### Parameters
88129

@@ -97,12 +138,12 @@ Value to parse (`string` or [`Buffer`][buffer]).
97138

98139
###### `options.extensions`
99140

100-
Array of syntax extensions (`Array<MicromarkSyntaxExtension>`, default: `[]`).
101-
Passed to [`micromark` as `extensions`][micromark-extensions].
141+
List of syntax extensions (`Array<MicromarkSyntaxExtension>`, default: `[]`).
142+
Passed to [`micromark` as `options.extensions`][micromark-extensions].
102143

103144
###### `options.mdastExtensions`
104145

105-
Array of mdast extensions (`Array<MdastExtension>`, default: `[]`).
146+
List of mdast extensions (`Array<MdastExtension>`, default: `[]`).
106147

107148
##### Returns
108149

@@ -111,48 +152,73 @@ Array of mdast extensions (`Array<MdastExtension>`, default: `[]`).
111152
## List of extensions
112153

113154
* [`syntax-tree/mdast-util-directive`](https://github.com/syntax-tree/mdast-util-directive)
114-
parse directives
155+
— directives
115156
* [`syntax-tree/mdast-util-frontmatter`](https://github.com/syntax-tree/mdast-util-frontmatter)
116-
parse frontmatter (YAML, TOML, more)
157+
— frontmatter (YAML, TOML, more)
117158
* [`syntax-tree/mdast-util-gfm`](https://github.com/syntax-tree/mdast-util-gfm)
118-
parse GFM
159+
— GFM
119160
* [`syntax-tree/mdast-util-gfm-autolink-literal`](https://github.com/syntax-tree/mdast-util-gfm-autolink-literal)
120-
parse GFM autolink literals
161+
— GFM autolink literals
121162
* [`syntax-tree/mdast-util-gfm-footnote`](https://github.com/syntax-tree/mdast-util-gfm-footnote)
122-
parse GFM footnotes
163+
— GFM footnotes
123164
* [`syntax-tree/mdast-util-gfm-strikethrough`](https://github.com/syntax-tree/mdast-util-gfm-strikethrough)
124-
parse GFM strikethrough
165+
— GFM strikethrough
125166
* [`syntax-tree/mdast-util-gfm-table`](https://github.com/syntax-tree/mdast-util-gfm-table)
126-
parse GFM tables
167+
— GFM tables
127168
* [`syntax-tree/mdast-util-gfm-task-list-item`](https://github.com/syntax-tree/mdast-util-gfm-task-list-item)
128-
parse GFM task list items
169+
— GFM task list items
129170
* [`syntax-tree/mdast-util-math`](https://github.com/syntax-tree/mdast-util-math)
130-
parse math
171+
— math
131172
* [`syntax-tree/mdast-util-mdx`](https://github.com/syntax-tree/mdast-util-mdx)
132-
parse MDX or MDX.js
173+
— MDX
133174
* [`syntax-tree/mdast-util-mdx-expression`](https://github.com/syntax-tree/mdast-util-mdx-expression)
134-
parse MDX or MDX.js expressions
175+
— MDX expressions
135176
* [`syntax-tree/mdast-util-mdx-jsx`](https://github.com/syntax-tree/mdast-util-mdx-jsx)
136-
parse MDX or MDX.js JSX
177+
— MDX JSX
137178
* [`syntax-tree/mdast-util-mdxjs-esm`](https://github.com/syntax-tree/mdast-util-mdxjs-esm)
138-
— parse MDX.js ESM
179+
— MDX ESM
180+
181+
## Syntax
182+
183+
Markdown is parsed according to CommonMark.
184+
Extensions can add support for other syntax.
185+
If you’re interested in extending markdown,
186+
[more information is available in micromark’s readme][micromark-extend].
187+
188+
## Syntax tree
189+
190+
The syntax tree is [mdast][].
191+
192+
## Types
193+
194+
This package is fully typed with [TypeScript][].
195+
It exports the types `Value`, `Encoding`, `Options`, `Extension`, `Handle`,
196+
`Transform`, `Token`, `CompileContext`, `OnEnterError`, `OnExitError`, which
197+
model the interfaces used in parameters, options, and extensions.
198+
199+
## Compatibility
200+
201+
Projects maintained by the unified collective are compatible with all maintained
202+
versions of Node.js.
203+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
204+
Our projects sometimes work with older versions, but this is not guaranteed.
139205

140206
## Security
141207

142208
As markdown is sometimes used for HTML, and improper use of HTML can open you up
143209
to a [cross-site scripting (XSS)][xss] attack, use of `mdast-util-from-markdown`
144210
can also be unsafe.
145211
When going to HTML, use this utility in combination with
146-
[`hast-util-sanitize`][sanitize] to make the tree safe.
212+
[`hast-util-sanitize`][hast-util-sanitize] to make the tree safe.
147213

148214
## Related
149215

216+
* [`syntax-tree/mdast-util-to-markdown`](https://github.com/syntax-tree/mdast-util-to-markdown)
217+
— serialize mdast as markdown
150218
* [`micromark/micromark`](https://github.com/micromark/micromark)
151-
the smallest commonmark-compliant markdown parser that exists
219+
parse markdown
152220
* [`remarkjs/remark`](https://github.com/remarkjs/remark)
153-
— markdown processor powered by plugins
154-
* [`syntax-tree/mdast-util-to-markdown`](https://github.com/syntax-tree/mdast-util-to-markdown)
155-
— serialize mdast to markdown
221+
— process markdown
156222

157223
## Contribute
158224

@@ -198,6 +264,8 @@ abide by its terms.
198264

199265
[npm]: https://docs.npmjs.com/cli/install
200266

267+
[skypack]: https://www.skypack.dev
268+
201269
[license]: license
202270

203271
[author]: https://wooorm.com
@@ -208,6 +276,10 @@ abide by its terms.
208276

209277
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
210278

279+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
280+
281+
[typescript]: https://www.typescriptlang.org
282+
211283
[mdast]: https://github.com/syntax-tree/mdast
212284

213285
[root]: https://github.com/syntax-tree/mdast#root
@@ -218,10 +290,14 @@ abide by its terms.
218290

219291
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
220292

221-
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
293+
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
222294

223295
[micromark]: https://github.com/micromark/micromark
224296

225297
[micromark-extensions]: https://github.com/micromark/micromark#optionsextensions
226298

299+
[micromark-extend]: https://github.com/micromark/micromark#extensions
300+
227301
[remark]: https://github.com/remarkjs/remark
302+
303+
[remark-parse]: https://github.com/remarkjs/remark/tree/main/packages/remark-parse

0 commit comments

Comments
 (0)