8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- ** [ mdast] [ ] ** utility to get the plain text content of a node.
11
+ [ mdast] [ ] utility to get the text content of a node.
12
12
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] [ ] .
14
42
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
17
44
18
- [ npm] [ ] :
45
+ This package is [ ESM only] [ esm ] .
46
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
47
20
48
``` sh
21
49
npm install mdast-util-to-string
22
50
```
23
51
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
+
24
66
## Use
25
67
26
68
``` js
27
69
import {unified } from ' unified'
28
- import remarkParse from ' remark-parse '
70
+ import { fromMarkdown } from ' mdast-util-from-markdown '
29
71
import {toString } from ' mdast-util-to-string'
30
72
31
- const tree = unified ()
32
- .use (remarkParse)
33
- .parse (' Some _emphasis_, **importance**, and `code`.' )
73
+ const tree = fromMarkdown (' Some _emphasis_, **importance**, and `code`.' )
34
74
35
75
console .log (toString (tree)) // => 'Some emphasis, importance, and code.'
36
76
```
37
77
38
78
## API
39
79
40
- This package exports the following identifiers: ` toString ` .
80
+ This package exports the identifier ` toString ` .
41
81
There is no default export.
42
82
43
83
### ` toString(node[, options]) `
44
84
45
85
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.
46
88
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 `
50
90
51
- > This is not a markdown to plain-text library.
52
- > Use [ ` strip-markdown ` ] [ strip-markdown ] for that.
91
+ Configuration (optional).
53
92
54
93
###### ` options.includeImageAlt `
55
94
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.
57
108
58
109
## Security
59
110
@@ -63,19 +114,15 @@ attacks.
63
114
64
115
## Related
65
116
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
70
119
* [ ` 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
74
121
75
122
## Contribute
76
123
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.
79
126
See [ ` support.md ` ] [ support ] for ways to get help.
80
127
81
128
This project has a [ code of conduct] [ coc ] .
@@ -116,22 +163,34 @@ abide by its terms.
116
163
117
164
[ npm ] : https://docs.npmjs.com/cli/install
118
165
166
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
167
+
168
+ [ esmsh ] : https://esm.sh
169
+
170
+ [ typescript ] : https://www.typescriptlang.org
171
+
119
172
[ license ] : license
120
173
121
174
[ author ] : https://wooorm.com
122
175
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
124
179
125
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
180
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
126
181
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
128
183
129
184
[ mdast ] : https://github.com/syntax-tree/mdast
130
185
131
- [ node ] : https://github.com/syntax-tree/mdast#nodes
186
+ [ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
132
187
133
- [ strip-markdown ] : https://github.com/remarkjs/strip-markdown
188
+ [ hast ] : https://github.com/syntax-tree/hast
134
189
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
136
191
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