Skip to content

Commit

Permalink
Add improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 24, 2023
1 parent 2543409 commit c635d51
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
19 changes: 13 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Position} Position
* @typedef {import('unist').Point} Point
*
* @typedef Options
* Configuration.
Expand All @@ -17,6 +14,16 @@

import {color} from './color.js'

/**
* Inspect a node, with color in Node, without color in browsers.
*
* @param tree
* Tree to inspect.
* @param options
* Configuration.
* @returns
* Pretty printed `tree`.
*/
/* c8 ignore next */
export const inspect = color ? inspectColor : inspectNoColor

Expand All @@ -36,15 +43,15 @@ const colorExpression =
/**
* Inspect a node, without color.
*
* @param {unknown} node
* @param {unknown} tree
* Tree to inspect.
* @param {Options | null | undefined} [options]
* Configuration.
* @returns {string}
* Pretty printed `tree`.
*/
export function inspectNoColor(node, options) {
return inspectColor(node, options).replace(colorExpression, '')
export function inspectNoColor(tree, options) {
return inspectColor(tree, options).replace(colorExpression, '')
}

/**
Expand Down
65 changes: 43 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`inspect(node[, options])`](#inspectnode-options)
* [`inspectColor(node[, options])`](#inspectcolornode-options)
* [`inspectNoColor(node[, options])`](#inspectnocolornode-options)
* [`inspect(tree[, options])`](#inspecttree-options)
* [`inspectColor(tree[, options])`](#inspectcolortree-options)
* [`inspectNoColor(tree[, options])`](#inspectnocolortree-options)
* [`Options`](#options)
* [Types](#types)
* [Compatibility](#compatibility)
* [Contribute](#contribute)
Expand All @@ -38,7 +39,7 @@ to more easily spot bugs and see what’s going on in the tree.
## Install

This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
In Node.js (version 14.14+ and 16.0+), install with [npm][]:

```sh
npm install unist-util-inspect
Expand All @@ -47,14 +48,14 @@ npm install unist-util-inspect
In Deno with [`esm.sh`][esmsh]:

```js
import {inspect} from "https://esm.sh/unist-util-inspect@8"
import {inspect} from 'https://esm.sh/unist-util-inspect@7'
```

In browsers with [`esm.sh`][esmsh]:

```html
<script type="module">
import {inspect} from "https://esm.sh/unist-util-inspect@7?bundle"
import {inspect} from 'https://esm.sh/unist-util-inspect@7?bundle'
</script>
```

Expand Down Expand Up @@ -91,42 +92,54 @@ root[2]

## API

This package exports the identifiers `inspect`, `inspectColor`, and
`inspectNoColor`.
This package exports the identifiers [`inspect`][api-inspect],
[`inspectColor`][api-inspectcolor], and [`inspectNoColor`][api-inspectnocolor].
There is no default export.

### `inspect(node[, options])`
### `inspect(tree[, options])`

Inspect the given `node` ([`Node`][node]).
By default, colors are added in Node, and not in other places.
See below on how to change that.
Inspect a tree, with color in Node, without color in browsers.

###### `options.showPositions`
###### Parameters

Whether to include positional information (`boolean`, default: `true`).
* `tree` ([`Node`][node])
— tree to inspect
* `options` ([`Options`][api-options], optional)
— configuration

##### Returns
###### Returns

Pretty printed `node` (`string`).
Pretty printed `tree` (`string`).

### `inspectColor(node[, options])`
### `inspectColor(tree[, options])`

Inspect with ANSI color sequences (default in Node, Deno).
Inspect a tree, with color.
Otherwise same as [`inspect`][api-inspect].

### `inspectNoColor(node[, options])`
### `inspectNoColor(tree[, options])`

Inspect without ANSI color sequences (default in browser, `react-native`).
Inspect a tree, without color.
Otherwise same as [`inspect`][api-inspect].

### `Options`

Configuration (TypeScript type).

###### Fields

* `showPositions` (`boolean`, default: `true`)
— whether to include positional information

## Types

This package is fully typed with [TypeScript][].
It exports the additional type `Options`.
It exports the additional type [`Options`][api-options].

## Compatibility

Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
As of now, that is Node.js 14.14+ and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.

## Contribute
Expand Down Expand Up @@ -194,3 +207,11 @@ abide by its terms.
[unist]: https://github.com/syntax-tree/unist

[node]: https://github.com/syntax-tree/unist#node

[api-inspect]: #inspecttree-options

[api-inspectcolor]: #inspectcolortree-options

[api-inspectnocolor]: #inspectnocolortree-options

[api-options]: #options

0 comments on commit c635d51

Please sign in to comment.