3
3
[ ![ Build] [ build-badge ]] [ build ]
4
4
[ ![ Coverage] [ coverage-badge ]] [ coverage ]
5
5
[ ![ Downloads] [ downloads-badge ]] [ downloads ]
6
+ [ ![ Size] [ size-badge ]] [ size ]
7
+ [ ![ Sponsors] [ sponsors-badge ]] [ collective ]
8
+ [ ![ Backers] [ backers-badge ]] [ collective ]
6
9
[ ![ Chat] [ chat-badge ]] [ chat ]
7
10
8
- Transform [ hast] [ ] (HTML) to [ mdast] [ ] (markdown) .
11
+ [ ** hast** ] [ hast ] utility to transform to [ ** mdast** ] [ mdast ] .
9
12
10
13
> ** Note** : You probably want to use [ rehype-remark] [ ] .
11
14
12
- ## Installation
15
+ ## Install
13
16
14
17
[ npm] [ ] :
15
18
16
- ``` bash
19
+ ``` sh
17
20
npm install hast-util-to-mdast
18
21
```
19
22
@@ -27,7 +30,7 @@ Say we have the following `example.html`:
27
30
28
31
…and next to it, ` example.js ` :
29
32
30
- ``` javascript
33
+ ``` js
31
34
var unified = require (' unified' )
32
35
var parse = require (' rehype-parse' )
33
36
var stringify = require (' remark-stringify' )
@@ -57,59 +60,67 @@ Now, running `node example.js` yields:
57
60
58
61
## API
59
62
60
- ### ` toMdast(node [, options]) `
63
+ ### ` toMdast(tree [, options]) `
61
64
62
- Transform the given [ hast] [ ] tree to [ mdast] [ ] .
65
+ Transform the given [ ** hast** ] [ hast ] [ * tree* ] [ tree ] to [ ** mdast** ] [ mdast ] .
63
66
64
67
##### Options
65
68
66
69
###### ` options.handlers `
67
70
68
- Object mapping tag-names to functions handling those elements.
69
- Take a look at [ ` handlers/ ` ] [ handlers ] for examples.
71
+ Object mapping tag names or [ * types* ] [ type ] to functions handling those
72
+ [ * elements* ] [ element ] or [ * nodes* ] [ hast-node ] .
73
+ See [ ` handlers/ ` ] [ handlers ] for examples.
70
74
71
75
###### ` options.document `
72
76
73
- Whether the given tree is a complete document. If ` document: true ` ,
74
- implicit paragraphs are added in the ` root ` node around inline mdast nodes.
75
- Otherwise, inline mdast nodes are wrapped when needed.
77
+ Whether the given [ * tree* ] [ tree ] is a complete document.
78
+ Applies if the given ` tree ` is a [ ` root ` ] [ hast-root ] .
79
+ First its [ * children* ] [ child ] are transformed to [ ** mast** ] [ mdast ] .
80
+ By default, if one or more of the new mdast children are [ * phrasing* ] [ phrasing ]
81
+ nodes, and one or more are not, the phrasing nodes are wrapped in
82
+ [ * paragraphs* ] [ mdast-paragraph ] .
83
+ If ` document: true ` , all mdast phrasing children are wrapped in paragraphs.
76
84
77
85
###### ` options.newlines `
78
86
79
- Whether to collapse to a newline (` \n ` ) instead of a single space (default) if
87
+ Whether to collapse to a line feed (` \n ` ) instead of a single space (default) if
80
88
a streak of white-space in a text node contains a newline.
81
89
82
90
##### Returns
83
91
84
- [ ` MDASTNode ` ] [ mdast ] .
92
+ [ ` MDASTNode ` ] [ mdast-node ] .
85
93
86
94
##### Notes
87
95
88
- ###### Implied sentences
96
+ ###### Implied paragraphs
89
97
90
- The algorithm supports implicit and explicit paragraphs, such as:
98
+ The algorithm supports implicit and explicit paragraphs (see [ HTML Standard,
99
+ A. van Kesteren; et al. WHATWG § 3.2.5.4 Paragraphs] [ spec ] ), such as:
91
100
92
101
``` html
93
102
<article >
94
- An implicit sentence .
95
- <h1 >An explicit sentence .</h1 >
103
+ An implicit paragraphs .
104
+ <h1 >An explicit paragraphs .</h1 >
96
105
</article >
97
106
```
98
107
99
108
Yields:
100
109
101
110
``` markdown
102
- An implicit sentence .
111
+ An implicit paragraphs .
103
112
104
- # An explicit sentence .
113
+ # An explicit paragraphs .
105
114
```
106
115
107
116
###### Ignoring nodes
108
117
109
- Some nodes are ignored and their content will not be present in mdast.
110
- To ignore custom elements, configure a handler for their tag-name or type that
111
- returns nothing.
112
- For example, to ignore ` em ` elements, pass ` handlers: {'em': function () {}} ` :
118
+ Some [ * nodes* ] [ hast-node ] are ignored and their content will not be present in
119
+ the [ ** mdast** ] [ mdast ] [ * tree* ] [ tree ] .
120
+ To ignore nodes, configure a [ handler] [ ] for their tag name or [ * type* ] [ type ]
121
+ that returns nothing.
122
+ For example, to ignore ` em ` [ * elements* ] [ element ] , pass `handlers: {'em':
123
+ function () {}}`:
113
124
114
125
``` html
115
126
<p ><strong >Importance</strong > and <em >emphasis</em >.</p >
@@ -121,7 +132,8 @@ Yields:
121
132
**Importance** and .
122
133
```
123
134
124
- To ignore a specific element from HTML, set ` data-mdast ` to ` ignore ` :
135
+ To ignore a specific element from the HTML source, set ` data-mdast ` to
136
+ ` ignore ` :
125
137
126
138
``` html
127
139
<p ><strong >Importance</strong > and <em data-mdast =" ignore" >emphasis</em >.</p >
@@ -148,11 +160,13 @@ Yields:
148
160
149
161
## Contribute
150
162
151
- See [ ` contributing.md ` in ` syntax-tree/hast ` ] [ contributing ] for ways to get
163
+ See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
152
164
started.
165
+ See [ ` support.md ` ] [ support ] for ways to get help.
153
166
154
- This organisation has a [ Code of Conduct] [ coc ] . By interacting with this
155
- repository, organisation, or community you agree to abide by its terms.
167
+ This project has a [ Code of Conduct] [ coc ] .
168
+ By interacting with this repository, organisation, or community you agree to
169
+ abide by its terms.
156
170
157
171
## License
158
172
@@ -172,24 +186,58 @@ repository, organisation, or community you agree to abide by its terms.
172
186
173
187
[ downloads ] : https://www.npmjs.com/package/hast-util-to-mdast
174
188
189
+ [ size-badge ] : https://img.shields.io/bundlephobia/minzip/hast-util-to-mdast.svg
190
+
191
+ [ size ] : https://bundlephobia.com/result?p=hast-util-to-mdast
192
+
193
+ [ sponsors-badge ] : https://opencollective.com/unified/sponsors/badge.svg
194
+
195
+ [ backers-badge ] : https://opencollective.com/unified/backers/badge.svg
196
+
197
+ [ collective ] : https://opencollective.com/unified
198
+
175
199
[ chat-badge ] : https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
176
200
177
- [ chat ] : https://spectrum.chat/unified/rehype
201
+ [ chat ] : https://spectrum.chat/unified/syntax-tree
178
202
179
203
[ npm ] : https://docs.npmjs.com/cli/install
180
204
181
205
[ license ] : license
182
206
183
207
[ author ] : https://wooorm.com
184
208
209
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/master/contributing.md
210
+
211
+ [ support ] : https://github.com/syntax-tree/.github/blob/master/support.md
212
+
213
+ [ coc ] : https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
214
+
215
+ [ tree ] : https://github.com/syntax-tree/unist#tree
216
+
217
+ [ child ] : https://github.com/syntax-tree/unist#child
218
+
219
+ [ type ] : https://github.com/syntax-tree/unist#type
220
+
185
221
[ mdast ] : https://github.com/syntax-tree/mdast
186
222
223
+ [ mdast-paragraph ] : https://github.com/syntax-tree/mdast#paragraph
224
+
225
+ [ mdast-node ] : https://github.com/syntax-tree/mdast#nodes
226
+
227
+ [ phrasing ] : https://github.com/syntax-tree/mdast#phrasingcontent
228
+
187
229
[ hast ] : https://github.com/syntax-tree/hast
188
230
231
+ [ hast-node ] : https://github.com/syntax-tree/hast#nodes
232
+
233
+ [ hast-root ] : https://github.com/syntax-tree/hast#root
234
+
235
+ [ element ] : https://github.com/syntax-tree/hast#element
236
+
189
237
[ rehype-remark ] : https://github.com/rehypejs/rehype-remark
190
238
191
- [ handlers ] : https://github.com/syntax-tree/hast-util-to-mdast/tree/master/lib/handlers
239
+ [ handler ] : #optionshandlers
192
240
193
- [ contributing ] : https://github.com/syntax-tree/hast/blob /master/contributing.md
241
+ [ handlers ] : https://github.com/syntax-tree/hast-util-to-mdast/tree /master/lib/handlers
194
242
195
- [ coc ] : https://github.com/syntax-tree/hast/blob/master/code-of-conduct.md
243
+ [ spec ] : https://html.spec.whatwg.org/#paragraphs
0 commit comments