Skip to content

Commit d9ce4df

Browse files
committed
Add improved docs
1 parent c20b853 commit d9ce4df

File tree

3 files changed

+133
-71
lines changed

3 files changed

+133
-71
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
*
2626
* @typedef Options
2727
* @property {Array<string>} [ignore]
28+
* List of mdast node types to ignore.
2829
* @property {Array<string>} [source]
30+
* List of mdast node types to mark as `source`.
2931
*
3032
* @typedef Context
3133
* @property {string} doc

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@
8383
},
8484
"remarkConfig": {
8585
"plugins": [
86-
"preset-wooorm"
86+
"preset-wooorm",
87+
[
88+
"remark-lint-no-html",
89+
false
90+
]
8791
]
8892
},
8993
"typeCoverage": {

readme.md

Lines changed: 126 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,82 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**mdast**][mdast] utility to transform to [**nlcst**][nlcst].
11+
[mdast][] utility to transform to [nlcst][].
1212

13-
> **Note**: You probably want to use [`remark-retext`][remark-retext].
13+
## Contents
1414

15-
## Install
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+
* [`toNlcst(tree, file, Parser[, options])`](#tonlcsttree-file-parser-options)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Security](#security)
24+
* [Related](#related)
25+
* [Contribute](#contribute)
26+
* [License](#license)
27+
28+
## What is this?
29+
30+
This package is a utility that takes an [mdast][] (markdown) syntax tree as
31+
input and turns it into [nlcst][] (natural language).
32+
33+
## When should I use this?
1634

17-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
18-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
35+
This project is useful when you want to deal with ASTs and inspect the natural
36+
language inside markdown.
37+
Unfortunately, there is no way yet to apply changes to the nlcst back into
38+
mdast.
39+
40+
The hast utility [`hast-util-to-nlcst`][hast-util-to-nlcst] does the same but
41+
uses an HTML tree as input.
42+
43+
The remark plugin [`remark-retext`][remark-retext] wraps this utility to do the
44+
same at a higher-level (easier) abstraction.
45+
46+
## Install
1947

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

2251
```sh
2352
npm install mdast-util-to-nlcst
2453
```
2554

55+
In Deno with [`esm.sh`][esmsh]:
56+
57+
```js
58+
import {toNlcst} from "https://esm.sh/mdast-util-to-nlcst@5"
59+
```
60+
61+
In browsers with [`esm.sh`][esmsh]:
62+
63+
```html
64+
<script type="module">
65+
import {toNlcst} from "https://esm.sh/mdast-util-to-nlcst@5?bundle"
66+
</script>
67+
```
68+
2669
## Use
2770

71+
Say we have the following `example.md`:
72+
73+
```markdown
74+
Some *foo*sball.
75+
```
76+
77+
…and next to it a module `example.js`:
78+
2879
```js
29-
import {VFile} from 'vfile'
80+
import {read} from 'to-vfile'
3081
import {ParseEnglish} from 'parse-english'
3182
import {inspect} from 'unist-util-inspect'
32-
import fromMarkdown from 'mdast-util-from-markdown'
83+
import {fromMarkdown} from 'mdast-util-from-markdown'
3384
import {toNlcst} from 'mdast-util-to-nlcst'
3485

35-
const file = new VFile('Some *foo*sball.')
86+
const file = await read('example.md')
3687
const mdast = fromMarkdown(file)
3788
const nlcst = toNlcst(mdast, file, ParseEnglish)
3889

@@ -56,49 +107,28 @@ RootNode[1] (1:1-1:17, 0-16)
56107

57108
## API
58109

59-
This package exports the following identifiers: `toNlcst`.
110+
This package exports the identifier `toNlcst`.
60111
There is no default export.
61112

62113
### `toNlcst(tree, file, Parser[, options])`
63114

64-
Transform a [tree][] in [mdast][], with a corresponding [virtual file][vfile],
65-
into [nlcst][].
66-
67-
##### Parameters
68-
69-
###### `node`
70-
71-
Tree in [mdast][] with positional information ([`MdastNode`][mdastnode]).
72-
73-
###### `file`
115+
[mdast][] utility to transform to [nlcst][].
74116

75-
Virtual file ([`VFile`][vfile]).
117+
> 👉 **Note**: `tree` must have positional info, `file` must be a [vfile][]
118+
> corresponding to `tree`, and `Parser` must be a parser such as
119+
> [`parse-english`][parse-english], [`parse-dutch`][parse-dutch], or
120+
> [`parse-latin`][parse-latin].
76121
77-
###### `parser`
122+
##### `options`
78123

79-
[nlcst][] parser (`Function`).
80-
For example, [`parse-english`][english], [`parse-dutch`][dutch], or
81-
[`parse-latin`][latin].
124+
Configuration (optional).
82125

83126
###### `options.ignore`
84127

85-
List of [types][type] to ignore (`Array<string>`).
86-
87-
`'table'`, `'tableRow'`, and `'tableCell'` are always ignored.
88-
89-
###### `options.source`
90-
91-
List of [types][type] to mark as [source][] (`Array<string>`).
92-
93-
`'inlineCode'` is always marked as source.
94-
95-
##### Returns
96-
97-
[`NlcstNode`][nlcstnode].
98-
99-
##### Examples
128+
List of [mdast][] node types to ignore (`Array<string>`, optional).
129+
The types `'table'`, `'tableRow'`, and `'tableCell'` are always ignored.
100130

101-
###### `ignore`
131+
<details><summary>Show example</summary>
102132

103133
Say we have the following file `example.md`:
104134

@@ -123,7 +153,15 @@ RootNode[2] (1:1-3:1, 0-14)
123153
└─1 WhiteSpaceNode "\n\n" (1:13-3:1, 12-14)
124154
```
125155

126-
###### `source`
156+
</details>
157+
158+
###### `options.source`
159+
160+
List of [mdast][] node types to mark as [nlcst][] source nodes
161+
(`Array<string>`, optional).
162+
The type `'inlineCode'` is always marked as source.
163+
164+
<details><summary>Show example</summary>
127165

128166
Say we have the following file `example.md`:
129167

@@ -151,28 +189,46 @@ RootNode[3] (1:1-3:32, 0-45)
151189
└─0 SourceNode "> A paragraph in a block quote." (3:1-3:32, 14-45)
152190
```
153191

192+
</details>
193+
194+
##### Returns
195+
196+
[`NlcstNode`][nlcst-node].
197+
198+
## Types
199+
200+
This package is fully typed with [TypeScript][].
201+
It exports the type `Options`.
202+
203+
## Compatibility
204+
205+
Projects maintained by the unified collective are compatible with all maintained
206+
versions of Node.js.
207+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
208+
Our projects sometimes work with older versions, but this is not guaranteed.
209+
154210
## Security
155211

156212
Use of `mdast-util-to-nlcst` does not involve [**hast**][hast] so there are no
157213
openings for [cross-site scripting (XSS)][xss] attacks.
158214

159215
## Related
160216

161-
* [`remark-retext`][remark-retext]
162-
**retext** support for **remark**
217+
* [`mdast-util-to-hast`](https://github.com/syntax-tree/mdast-util-to-hast)
218+
transform mdast to hast
163219
* [`hast-util-to-nlcst`](https://github.com/syntax-tree/hast-util-to-nlcst)
164-
Transform [hast][] to [nlcst][]
220+
transform hast to nlcst
165221
* [`hast-util-to-mdast`](https://github.com/syntax-tree/hast-util-to-mdast)
166-
Transform [hast][] to [mdast][]
222+
transform hast to mdast
167223
* [`hast-util-to-xast`](https://github.com/syntax-tree/hast-util-to-xast)
168-
Transform [hast][] to [xast][]
169-
* [`mdast-util-to-hast`](https://github.com/syntax-tree/mdast-util-to-hast)
170-
Transform [mdast][] to [hast][]
224+
transform hast to xast
225+
* [`hast-util-sanitize`](https://github.com/syntax-tree/hast-util-sanitize)
226+
sanitize hast nodes
171227

172228
## Contribute
173229

174-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
175-
started.
230+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
231+
ways to get started.
176232
See [`support.md`][support] for ways to get help.
177233

178234
This project has a [code of conduct][coc].
@@ -213,42 +269,42 @@ abide by its terms.
213269

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

272+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
273+
274+
[esmsh]: https://esm.sh
275+
276+
[typescript]: https://www.typescriptlang.org
277+
216278
[license]: license
217279

218280
[author]: https://wooorm.com
219281

220-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
282+
[health]: https://github.com/syntax-tree/.github
221283

222-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
284+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
223285

224-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
286+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
287+
288+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
289+
290+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
225291

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

228294
[nlcst]: https://github.com/syntax-tree/nlcst
229295

296+
[nlcst-node]: https://github.com/syntax-tree/nlcst#node
297+
230298
[hast]: https://github.com/syntax-tree/hast
231299

232-
[xast]: https://github.com/syntax-tree/xast
300+
[hast-util-to-nlcst]: https://github.com/syntax-tree/hast-util-to-nlcst
233301

234302
[remark-retext]: https://github.com/remarkjs/remark-retext
235303

236304
[vfile]: https://github.com/vfile/vfile
237305

238-
[english]: https://github.com/wooorm/parse-english
306+
[parse-english]: https://github.com/wooorm/parse-english
239307

240-
[latin]: https://github.com/wooorm/parse-latin
308+
[parse-latin]: https://github.com/wooorm/parse-latin
241309

242-
[dutch]: https://github.com/wooorm/parse-dutch
243-
244-
[type]: https://github.com/syntax-tree/mdast#ast
245-
246-
[source]: https://github.com/syntax-tree/nlcst#source
247-
248-
[tree]: https://github.com/syntax-tree/unist#tree
249-
250-
[mdastnode]: https://github.com/syntax-tree/mdast#nodes
251-
252-
[nlcstnode]: https://github.com/syntax-tree/nlcst#nodes
253-
254-
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
310+
[parse-dutch]: https://github.com/wooorm/parse-dutch

0 commit comments

Comments
 (0)