Skip to content

Commit

Permalink
Add improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 21, 2022
1 parent ccbae89 commit 9da3339
Showing 1 changed file with 124 additions and 39 deletions.
163 changes: 124 additions & 39 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,72 @@
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

Extension for [`mdast-util-from-markdown`][from-markdown] and/or
[`mdast-util-to-markdown`][to-markdown] to support GitHub flavored markdown
task list items in **[mdast][]**.
When parsing (`from-markdown`), must be combined with
[`micromark-extension-gfm-task-list-item`][extension].
[mdast][] extensions to parse and serialize [GFM][] task list items.

## Contents

* [What is this?](#what-is-this)
* [When to use this](#when-to-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`gfmTaskListItemFromMarkdown`](#gfmtasklistitemfrommarkdown)
* [`gfmTaskListItemToMarkdown`](#gfmtasklistitemtomarkdown)
* [Syntax tree](#syntax-tree)
* [Nodes](#nodes)
* [Content model](#content-model)
* [Types](#types)
* [Compatibility](#compatibility)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)

## What is this?

This package contains extensions that add support for the task list item syntax
enabled by GFM to [`mdast-util-from-markdown`][mdast-util-from-markdown] and
[`mdast-util-to-markdown`][mdast-util-to-markdown].

## When to use this

Use this if you’re dealing with the AST manually.
It’s might be better to use [`remark-gfm`][remark-gfm] with **[remark][]**,
which includes this but provides a nicer interface and makes it easier to
combine with hundreds of plugins.
These tools are all rather low-level.
In most cases, you’d want to use [`remark-gfm`][remark-gfm] with remark instead.

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

When working with `mdast-util-from-markdown`, you must combine this package with
[`micromark-extension-gfm-task-list-item`][extension].

This utility does not handle how markdown is turned to HTML.
That’s done by [`mdast-util-to-hast`][mdast-util-to-hast].

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
## Install

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

```sh
npm install mdast-util-gfm-task-list-item
```

In Deno with [`esm.sh`][esmsh]:

```js
import {gfmTaskListItemFromMarkdown, gfmTaskListItemToMarkdown} from 'https://esm.sh/mdast-util-gfm-task-list-item@1'
```

In browsers with [`esm.sh`][esmsh]:

```html
<script type="module">
import {gfmTaskListItemFromMarkdown, gfmTaskListItemToMarkdown} from 'https://esm.sh/mdast-util-gfm-task-list-item@1?bundle'
</script>
```

## Use

Say we have the following file, `example.md`:
Say our document `example.md` contains:

```markdown
* [ ] To do
Expand All @@ -44,16 +83,16 @@ Say we have the following file, `example.md`:
2. [x] …messages
```

And our module, `example.js`, looks as follows:
…and our module `example.js` looks as follows:

```js
import fs from 'node:fs'
import fs from 'node:fs/promises'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {gfmTaskListItem} from 'micromark-extension-gfm-task-list-item'
import {gfmTaskListItemFromMarkdown, gfmTaskListItemToMarkdown} from 'mdast-util-gfm-task-list-item'

const doc = fs.readFileSync('example.md')
const doc = await fs.readFile('example.md')

const tree = fromMarkdown(doc, {
extensions: [gfmTaskListItem],
Expand All @@ -67,8 +106,7 @@ const out = toMarkdown(tree, {extensions: [gfmTaskListItemToMarkdown]})
console.log(out)
```

Now, running `node example` yields (positional info removed for the sake of
brevity):
…now running `node example.js` yields (positional info removed for brevity):

```js
{
Expand Down Expand Up @@ -136,38 +174,75 @@ brevity):

## API

This package exports the following identifier: `gfmTaskListItemFromMarkdown`,
This package exports the identifiers `gfmTaskListItemFromMarkdown` and
`gfmTaskListItemToMarkdown`.
There is no default export.

### `gfmTaskListItemFromMarkdown`

Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown].

### `gfmTaskListItemToMarkdown`

Support task list items.
The exports are extensions, respectively
for [`mdast-util-from-markdown`][from-markdown] and
[`mdast-util-to-markdown`][to-markdown].
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown].

## Syntax tree

The following interfaces are added to **[mdast][]** by this utility.

### Nodes

#### `ListItem` (GFM)

```idl
interface ListItemGfm <: ListItem {
checked: boolean?
}
```

In GFM, a `checked` field can be present.
It represents whether the item is done (when `true`), not done (when `false`),
or indeterminate or not applicable (when `null` or not present).

### Content model

#### `ListContent` (GFM)

```idl
type ListContentGfm = ListItemGfm
```

## Types

This package is fully typed with [TypeScript][].
It does not export additional types.

The `ListItemGfm` node type is supported in `@types/mdast` by default.

## Compatibility

Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.

This plugin works with `mdast-util-from-markdown` version 1+ and
`mdast-util-to-markdown` version 1+.

## Related

* [`remarkjs/remark`][remark]
— markdown processor powered by plugins
* [`remarkjs/remark-gfm`][remark-gfm]
— remark plugin to support GFM
* [`micromark/micromark`][micromark]
— the smallest commonmark-compliant markdown parser that exists
* [`syntax-tree/mdast-util-gfm`][mdast-util-gfm]
— same but all of GFM (autolink literals, footnotes, strikethrough, tables,
tasklists)
* [`micromark/micromark-extension-gfm-task-list-item`][extension]
— micromark extension to parse GFM task list items
* [`syntax-tree/mdast-util-from-markdown`][from-markdown]
— mdast parser using `micromark` to create mdast from markdown
* [`syntax-tree/mdast-util-to-markdown`][to-markdown]
— mdast serializer to create markdown from mdast

## Contribute

See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
started.
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
ways to get started.
See [`support.md`][support] for ways to get help.

This project has a [code of conduct][coc].
Expand Down Expand Up @@ -208,10 +283,18 @@ abide by its terms.

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

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[esmsh]: https://esm.sh

[typescript]: https://www.typescriptlang.org

[license]: license

[author]: https://wooorm.com

[health]: https://github.com/syntax-tree/.github

[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md

[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
Expand All @@ -220,14 +303,16 @@ abide by its terms.

[mdast]: https://github.com/syntax-tree/mdast

[remark]: https://github.com/remarkjs/remark

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

[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown

[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown

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

[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast

[extension]: https://github.com/micromark/micromark-extension-gfm-task-list-item

[gfm]: https://github.github.com/gfm/

0 comments on commit 9da3339

Please sign in to comment.