Skip to content

Commit 8d37953

Browse files
committed
Add examples to docs
1 parent 9d8ce95 commit 8d37953

File tree

1 file changed

+113
-8
lines changed

1 file changed

+113
-8
lines changed

readme.md

+113-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Transform [MDAST][] to [HAST][].
44

5-
> **Note** You probably want to use [remark-rehype][].
5+
> **Note**: You probably want to use [remark-rehype][].
66
77
## Installation
88

@@ -51,11 +51,12 @@ root[1] (1:1-2:1, 0-20)
5151

5252
Transform the given [MDAST][] tree to a [HAST][] tree.
5353

54+
##### Options
55+
5456
###### `options.allowDangerousHTML`
5557

56-
Whether to allow `html` nodes and inject them as raw HTML (`boolean`,
57-
default: `false`). Only do this when compiling later with
58-
`hast-util-to-html`.
58+
Whether to allow `html` nodes and inject them as raw HTML (`boolean`, default:
59+
`false`). Only do this when compiling later with `hast-util-to-html`.
5960

6061
###### `options.commonmark`
6162

@@ -64,15 +65,14 @@ are found. The default behaviour is to prefer the last duplicate definition.
6465

6566
###### `options.handlers`
6667

67-
Object mapping [MDAST nodes][mdast] to functions
68-
handling those elements.
68+
Object mapping [MDAST nodes][mdast] to functions handling those elements.
6969
Take a look at [`lib/handlers/`][handlers] for examples.
7070

71-
###### Returns
71+
##### Returns
7272

7373
[`HASTNode`][hast].
7474

75-
###### Note
75+
##### Notes
7676

7777
* `yaml` and `toml` nodes are ignored
7878
* [`html`][mdast-html] nodes are ignored if `allowDangerousHTML` is `false`
@@ -85,6 +85,111 @@ Take a look at [`lib/handlers/`][handlers] for examples.
8585
* If `node.data.hChildren` is set, it’s used as the element’s HAST
8686
children
8787

88+
##### Examples
89+
90+
###### `hName`
91+
92+
`node.data.hName` in MDAST sets the tag-name of an element in HAST.
93+
The following [MDAST][]:
94+
95+
```js
96+
{
97+
type: 'strong',
98+
data: {hName: 'b'},
99+
children: [{type: 'text', value: 'Alpha'}]
100+
}
101+
```
102+
103+
Yields, in HAST:
104+
105+
```js
106+
{
107+
type: 'element',
108+
tagName: 'b',
109+
properties: {},
110+
children: [{type: 'text', value: 'Alpha'}]
111+
}
112+
```
113+
114+
###### `hProperties`
115+
116+
`node.data.hProperties` in MDAST sets the properties of an element in HAST.
117+
The following [MDAST][]:
118+
119+
```js
120+
{
121+
type: 'image',
122+
src: 'circle.svg',
123+
alt: 'Big red circle on a black background',
124+
title: null
125+
data: {hProperties: {className: ['responsive']}}
126+
}
127+
```
128+
129+
Yields, in HAST:
130+
131+
```js
132+
{
133+
type: 'element',
134+
tagName: 'img',
135+
properties: {
136+
src: 'circle.svg',
137+
alt: 'Big red circle on a black background',
138+
className: ['responsive']
139+
},
140+
children: []
141+
}
142+
```
143+
144+
###### `hChildren`
145+
146+
`node.data.hChildren` in MDAST sets the children of an element in HAST.
147+
The following [MDAST][]:
148+
149+
```js
150+
{
151+
type: 'code',
152+
lang: 'js',
153+
data: {
154+
hChildren: [
155+
{
156+
type: 'element',
157+
tagName: 'span',
158+
properties: {className: ['hljs-meta']},
159+
children: [{type: 'text', value: '"use strict"'}]
160+
},
161+
{type: 'text', value: ';'}
162+
]
163+
},
164+
value: '"use strict";'
165+
}
166+
```
167+
168+
Yields, in HAST (**note**: the `pre` and `language-js` class are added
169+
normal `mdast-util-to-hast` functionality):
170+
171+
```js
172+
{
173+
type: 'element',
174+
tagName: 'pre',
175+
properties: {},
176+
children: [{
177+
type: 'element',
178+
tagName: 'code',
179+
properties: {className: ['language-js']},
180+
children: [
181+
{
182+
type: 'element',
183+
tagName: 'span',
184+
properties: {className: ['hljs-meta']},
185+
children: [{type: 'text', value: '"use strict"'}]
186+
},
187+
{type: 'text', value: ';'}
188+
]
189+
}]
190+
}
191+
```
192+
88193
## Related
89194

90195
* [`mdast-util-to-nlcst`](https://github.com/syntax-tree/mdast-util-to-nlcst)

0 commit comments

Comments
 (0)