From ab5c0282fdf7f5e6bfee0415eb0f13a45aa6a181 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 25 Apr 2020 10:41:41 +0200 Subject: [PATCH] Add support for node kinds (hast, xast) --- index.js | 11 +++++++++-- package.json | 1 + test.js | 12 +++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2f6b1e9..f73a1f3 100644 --- a/index.js +++ b/index.js @@ -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 () 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) { @@ -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') { diff --git a/package.json b/package.json index bcac2f0..c449ad8 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test.js b/test.js index 1e011db..99ad3b1 100644 --- a/test.js +++ b/test.js @@ -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}) @@ -167,7 +168,7 @@ test('inspect()', function (t) { children: [] }) ), - 'element[0] [tagName="br"]', + 'element
[0]', 'should work on parent nodes without children' ) @@ -183,6 +184,15 @@ test('inspect()', function (t) { 'should work on void nodes' ) + t.equal( + strip(inspect(fromXml(''))), + [ + 'root[1]', + '└─ element[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({