Skip to content

Add support for node kinds (hast, xast) #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ var green = ansiColor(32, 39)
// Define ANSII color removal functionality.
var colorExpression = /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g

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

// Inspects a node, without using color.
function noColor(node) {
Expand Down Expand Up @@ -81,12 +83,17 @@ function inspect(node) {
// Colored node formatter.
function formatNode(node) {
var result = [node.type]
var kind = node.tagName || node.name
var position = node.position || {}
var location = stringifyPosition(position.start, position.end)
var attributes = []
var key
var value

if (kind) {
result.push('<', kind, '>')
}

if (node.children) {
result.push(dim('['), yellow(node.children.length), dim(']'))
} else if (typeof node.value === 'string') {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"strip-ansi": "^6.0.0",
"tape": "^5.0.0",
"tinyify": "^2.0.0",
"xast-util-from-xml": "^1.0.0",
"xo": "^0.29.0"
},
"scripts": {
Expand Down
12 changes: 11 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var test = require('tape')
var chalk = require('chalk')
var strip = require('strip-ansi')
var retext = require('retext')
var fromXml = require('xast-util-from-xml')
var inspect = require('.')

var chalkEnabled = new chalk.Instance({level: 1})
Expand Down Expand Up @@ -167,7 +168,7 @@ test('inspect()', function (t) {
children: []
})
),
'element[0] [tagName="br"]',
'element<br>[0]',
'should work on parent nodes without children'
)

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

t.equal(
strip(inspect(fromXml('<album id="123" />'))),
[
'root[1]',
'└─ element<album>[0] (1:1-1:19, 0-18) [attributes={"id":"123"}]'
].join('\n'),
'should work nodes of a certain kind (xast, hast)'
)

t.equal(
strip(
inspect({
Expand Down