@@ -12,46 +12,41 @@ npm install hast-util-from-parse5
12
12
13
13
## Usage
14
14
15
- Dependencies :
15
+ Say we have the following file, ` example.html ` :
16
16
17
- ``` javascript
18
- var parse5 = require (' parse5' );
19
- var inspect = require (' unist-util-inspect' );
20
- var vfile = require (' vfile' );
21
- var fromParse5 = require (' hast-util-from-parse5' );
22
- ```
23
-
24
- Fixture:
25
-
26
- ``` javascript
27
- var doc = ' <!doctype html><title>Hello!</title><h1 id="world">World!<!--after-->' ;
17
+ ``` html
18
+ <!doctype html><title >Hello!</title ><h1 id =" world" >World!<!-- after-->
28
19
```
29
20
30
- Parse :
21
+ And our script, ` example.js ` , looks as follows :
31
22
32
23
``` javascript
33
- var ast = parse5 .parse (doc, {locationInfo: true });
34
- ```
24
+ var vfile = require (' to-vfile' );
25
+ var parse5 = require (' parse5' );
26
+ var inspect = require (' unist-util-inspect' );
27
+ var fromParse5 = require (' hast-util-from-parse5' );
35
28
36
- Transform:
29
+ var doc = vfile .readSync (' example.html' )
30
+ var ast = parse5 .parse (String (doc), {locationInfo: true });
31
+ var hast = fromParse5 (ast, doc);
37
32
38
- ``` javascript
39
- var hast = fromParse5 (ast, vfile (doc));
33
+ console .log (inspect (hast));
40
34
```
41
35
42
- Yields :
36
+ Now, running ` node example ` yields :
43
37
44
- ``` txt
45
- root[2] (1:1-1:70 , 0-69 ) [data={"quirksMode":false}]
38
+ ``` text
39
+ root[2] (1:1-2:1 , 0-70 ) [data={"quirksMode":false}]
46
40
├─ doctype (1:1-1:16, 0-15) [name="html"]
47
41
└─ element[2] [tagName="html"]
48
42
├─ element[1] [tagName="head"]
49
43
│ └─ element[1] (1:16-1:37, 15-36) [tagName="title"]
50
44
│ └─ text: "Hello!" (1:23-1:29, 22-28)
51
45
└─ element[1] [tagName="body"]
52
- └─ element[2 ] (1:37-1:70 , 36-69 ) [tagName="h1"][properties={"id":"world"}]
46
+ └─ element[3 ] (1:37-2:1 , 36-70 ) [tagName="h1"][properties={"id":"world"}]
53
47
├─ text: "World!" (1:52-1:58, 51-57)
54
- └─ comment: "after" (1:58-1:70, 57-69)
48
+ ├─ comment: "after" (1:58-1:70, 57-69)
49
+ └─ text: "\n" (1:70-2:1, 69-70)
55
50
```
56
51
57
52
## API
0 commit comments