Skip to content

Commit d1f26cc

Browse files
authored
Add support for node kinds (hast, xast)
Closes GH-13. Reviewed-by: Christian Murphy <[email protected]>
1 parent fa74749 commit d1f26cc

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ var green = ansiColor(32, 39)
1717
// Define ANSII color removal functionality.
1818
var colorExpression = /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
1919

20-
// Standard keys defined by unist: https://github.com/syntax-tree/unist.
20+
// Standard keys defined by unist (<https://github.com/syntax-tree/unist>) that
21+
// we format differently.
2122
// We don’t ignore `data` though.
22-
var ignore = ['type', 'value', 'children', 'position']
23+
// Also includes `name` (from xast) and `tagName` (from `hast`).
24+
var ignore = ['type', 'value', 'children', 'position', 'name', 'tagName']
2325

2426
// Inspects a node, without using color.
2527
function noColor(node) {
@@ -81,12 +83,17 @@ function inspect(node) {
8183
// Colored node formatter.
8284
function formatNode(node) {
8385
var result = [node.type]
86+
var kind = node.tagName || node.name
8487
var position = node.position || {}
8588
var location = stringifyPosition(position.start, position.end)
8689
var attributes = []
8790
var key
8891
var value
8992

93+
if (kind) {
94+
result.push('<', kind, '>')
95+
}
96+
9097
if (node.children) {
9198
result.push(dim('['), yellow(node.children.length), dim(']'))
9299
} else if (typeof node.value === 'string') {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"strip-ansi": "^6.0.0",
5353
"tape": "^5.0.0",
5454
"tinyify": "^2.0.0",
55+
"xast-util-from-xml": "^1.0.0",
5556
"xo": "^0.29.0"
5657
},
5758
"scripts": {

test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var test = require('tape')
44
var chalk = require('chalk')
55
var strip = require('strip-ansi')
66
var retext = require('retext')
7+
var fromXml = require('xast-util-from-xml')
78
var inspect = require('.')
89

910
var chalkEnabled = new chalk.Instance({level: 1})
@@ -167,7 +168,7 @@ test('inspect()', function (t) {
167168
children: []
168169
})
169170
),
170-
'element[0] [tagName="br"]',
171+
'element<br>[0]',
171172
'should work on parent nodes without children'
172173
)
173174

@@ -183,6 +184,15 @@ test('inspect()', function (t) {
183184
'should work on void nodes'
184185
)
185186

187+
t.equal(
188+
strip(inspect(fromXml('<album id="123" />'))),
189+
[
190+
'root[1]',
191+
'└─ element<album>[0] (1:1-1:19, 0-18) [attributes={"id":"123"}]'
192+
].join('\n'),
193+
'should work nodes of a certain kind (xast, hast)'
194+
)
195+
186196
t.equal(
187197
strip(
188198
inspect({

0 commit comments

Comments
 (0)