Skip to content

Commit 353622c

Browse files
committed
Add improved docs
1 parent b03ce26 commit 353622c

File tree

1 file changed

+91
-32
lines changed

1 file changed

+91
-32
lines changed

readme.md

+91-32
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,103 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
**[mdast][]** utility to get the plain text content of a node.
11+
[mdast][] utility to get the text content of a node.
1212

13-
## Install
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+
* [`toString(node[, options])`](#tostringnode-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 tiny utility that gets the textual content of a node.
31+
32+
## When should I use this?
33+
34+
This utility is useful when you have a node, say a heading, and want to get the
35+
text inside it.
36+
37+
This package does not serialize markdown, that’s what
38+
[`mdast-util-to-markdown`][mdast-util-to-markdown] does.
39+
40+
Similar packages, [`hast-util-to-string`][hast-util-to-string] and
41+
[`hast-util-to-text`][hast-util-to-text], do the same but on [hast][].
1442

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

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

2048
```sh
2149
npm install mdast-util-to-string
2250
```
2351

52+
In Deno with [`esm.sh`][esmsh]:
53+
54+
```js
55+
import {toString} from 'https://esm.sh/mdast-util-to-string@3'
56+
```
57+
58+
In browsers with [`esm.sh`][esmsh]:
59+
60+
```html
61+
<script type="module">
62+
import {toString} from 'https://esm.sh/mdast-util-to-string@3?bundle'
63+
</script>
64+
```
65+
2466
## Use
2567

2668
```js
2769
import {unified} from 'unified'
28-
import remarkParse from 'remark-parse'
70+
import {fromMarkdown} from 'mdast-util-from-markdown'
2971
import {toString} from 'mdast-util-to-string'
3072

31-
const tree = unified()
32-
.use(remarkParse)
33-
.parse('Some _emphasis_, **importance**, and `code`.')
73+
const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.')
3474

3575
console.log(toString(tree)) // => 'Some emphasis, importance, and code.'
3676
```
3777

3878
## API
3979

40-
This package exports the following identifiers: `toString`.
80+
This package exports the identifier `toString`.
4181
There is no default export.
4282

4383
### `toString(node[, options])`
4484

4585
Get the text content of a [node][] or list of nodes.
86+
Prefers the node’s plain-text fields, otherwise serializes its children, and if
87+
the given value is an array, serialize the nodes in it.
4688

47-
The algorithm checks `value` of `node` and then `alt`.
48-
If no value is found, the algorithm checks the children of `node` and joins them
49-
(without spaces or newlines).
89+
##### `options`
5090

51-
> This is not a markdown to plain-text library.
52-
> Use [`strip-markdown`][strip-markdown] for that.
91+
Configuration (optional).
5392

5493
###### `options.includeImageAlt`
5594

56-
Whether to use `alt` (`boolean`, default: `true`)
95+
Whether to use `alt` (`boolean`, default: `true`).
96+
97+
## Types
98+
99+
This package is fully typed with [TypeScript][].
100+
It exports the type `Options`.
101+
102+
## Compatibility
103+
104+
Projects maintained by the unified collective are compatible with all maintained
105+
versions of Node.js.
106+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
107+
Our projects sometimes work with older versions, but this is not guaranteed.
57108

58109
## Security
59110

@@ -63,19 +114,15 @@ attacks.
63114

64115
## Related
65116

66-
* [`nlcst-to-string`](https://github.com/syntax-tree/nlcst-to-string)
67-
— Get text content in nlcst
68-
* [`hast-util-to-string`](https://github.com/wooorm/rehype-minify/tree/HEAD/packages/hast-util-to-string)
69-
— Get text content in hast
117+
* [`hast-util-to-string`](https://github.com/wooorm/rehype-minify/tree/main/packages/hast-util-to-string)
118+
— get text content in hast
70119
* [`hast-util-to-text`](https://github.com/syntax-tree/hast-util-to-text)
71-
— Get text content in hast according to the `innerText` algorithm
72-
* [`hast-util-from-string`](https://github.com/wooorm/rehype-minify/tree/HEAD/packages/hast-util-from-string)
73-
— Set text content in hast
120+
— get text content in hast according to the `innerText` algorithm
74121

75122
## Contribute
76123

77-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
78-
started.
124+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
125+
ways to get started.
79126
See [`support.md`][support] for ways to get help.
80127

81128
This project has a [code of conduct][coc].
@@ -116,22 +163,34 @@ abide by its terms.
116163

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

166+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
167+
168+
[esmsh]: https://esm.sh
169+
170+
[typescript]: https://www.typescriptlang.org
171+
119172
[license]: license
120173

121174
[author]: https://wooorm.com
122175

123-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
176+
[health]: https://github.com/syntax-tree/.github
177+
178+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
124179

125-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
180+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
126181

127-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
182+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
128183

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

131-
[node]: https://github.com/syntax-tree/mdast#nodes
186+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
132187

133-
[strip-markdown]: https://github.com/remarkjs/strip-markdown
188+
[hast]: https://github.com/syntax-tree/hast
134189

135-
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
190+
[hast-util-to-string]: https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-to-string
136191

137-
[hast]: https://github.com/syntax-tree/hast
192+
[hast-util-to-text]: https://github.com/syntax-tree/hast-util-to-text
193+
194+
[node]: https://github.com/syntax-tree/mdast#nodes
195+
196+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting

0 commit comments

Comments
 (0)