Skip to content

Commit a7f8ade

Browse files
authored
Merge pull request #90 from andrejewski/position
Add line, column, and index numbers to nodes
2 parents 7f1f890 + 1f3daa7 commit a7f8ade

File tree

12 files changed

+8807
-401
lines changed

12 files changed

+8807
-401
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,37 @@ Himalaya handles a lot of HTML's fringe cases, like:
8888
### Preserves Whitespace
8989
Himalaya does not cut corners and returns an accurate representation of the HTML supplied. To remove whitespace, post-process the JSON; check out [an example script](https://gist.github.com/andrejewski/773487d4f4a46b16865405d7b74eabf9).
9090

91+
### Line, column, and index positions
92+
Himalaya can include the start and end positions of nodes in the parse output.
93+
To enable this, you can pass `parse` the `parseDefaults` extended with `includePositions: true`:
94+
95+
```js
96+
import { parse, parseDefaults } from 'himalaya'
97+
parse('<img>', { ...parseDefaults, includePositions: true })
98+
/* =>
99+
[
100+
{
101+
"type": "element",
102+
"tagName": "img",
103+
"attributes": [],
104+
"children": [],
105+
"position": {
106+
"start": {
107+
"index": 0,
108+
"line": 0,
109+
"column": 0
110+
},
111+
"end": {
112+
"index": 5,
113+
"line": 0,
114+
"column": 5
115+
}
116+
}
117+
}
118+
]
119+
*/
120+
```
121+
91122
## Going back to HTML
92123
Himalaya provides a `stringify` method. The following example parses the HTML to JSON then parses the JSON back into HTML.
93124

0 commit comments

Comments
 (0)