Skip to content

Commit 2a9afde

Browse files
committed
Add improved docs
1 parent 8b57b44 commit 2a9afde

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

readme.md

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<!--lint disable no-html-->
2-
31
# hast-util-select
42

53
[![Build][build-badge]][build]
@@ -18,6 +16,7 @@ and `matches`.
1816
* [What is this?](#what-is-this)
1917
* [When should I use this?](#when-should-i-use-this)
2018
* [Install](#install)
19+
* [Use](#use)
2120
* [API](#api)
2221
* [`matches(selector, node[, space])`](#matchesselector-node-space)
2322
* [`select(selector, tree[, space])`](#selectselector-tree-space)
@@ -56,7 +55,7 @@ find and match any unist node.
5655
## Install
5756

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

6160
```sh
6261
npm install hast-util-select
@@ -76,6 +75,31 @@ In browsers with [`esm.sh`][esmsh]:
7675
</script>
7776
```
7877

78+
## Use
79+
80+
```js
81+
import {h} from 'hastscript'
82+
import {matches, select, selectAll} from 'hast-util-select'
83+
84+
const tree = h('section', [
85+
h('p', 'Alpha'),
86+
h('p', 'Bravo'),
87+
h('h1', 'Charlie'),
88+
h('p', 'Delta'),
89+
h('p', 'Echo'),
90+
h('p', 'Foxtrot'),
91+
h('p', 'Golf')
92+
])
93+
94+
matches('section', tree) // `true`
95+
96+
console.log(select('h1 ~ :nth-child(even)', tree))
97+
// The paragraph with `Delta`
98+
99+
console.log(selectAll('h1 ~ :nth-child(even)', tree))
100+
// The paragraphs with `Delta` and `Foxtrot`
101+
```
102+
79103
## API
80104

81105
This package exports the identifiers `matches`, `select`, and `selectAll`.
@@ -116,7 +140,6 @@ matches('.classy', h('a', {className: ['classy']})) // => true
116140
matches('#id', h('a', {id: 'id'})) // => true
117141
matches('[lang|=en]', h('a', {lang: 'en'})) // => true
118142
matches('[lang|=en]', h('a', {lang: 'en-GB'})) // => true
119-
//
120143
```
121144

122145
### `select(selector, tree[, space])`
@@ -278,13 +301,12 @@ Yields:
278301
* [ ]`[*|attr]` (any namespace attribute)
279302
* [ ]`[|attr]` (no namespace attribute)
280303
* [ ]`[attr=value i]` (attribute case-insensitive)
281-
* [ ]`:has()` (functional pseudo-class). <small>Relative
282-
selectors (`:has(> img)`) are not supported, but scope is
283-
(`:has(:scope > img)`) </small>
284-
* [ ]`:nth-child(n of S)` (functional pseudo-class). <small>Scoping
285-
to parents is not supported</small>
286-
* [ ]`:nth-last-child(n of S)` (scoped to parent S). <small>Scoping
287-
to parents is not supported</small>
304+
* [ ]`:has()` (functional pseudo-class, note: relative selectors such as
305+
`:has(> img)` are not supported, but scope is: `:has(:scope > img)`)
306+
* [ ]`:nth-child(n of S)` (functional pseudo-class, note: scoping to
307+
parents is not supported)
308+
* [ ]`:nth-last-child(n of S)` (functional pseudo-class, note: scoping to
309+
parents is not supported)
288310
* [ ]`:active` (pseudo-class)
289311
* [ ]`:current` (pseudo-class)
290312
* [ ]`:current()` (functional pseudo-class)
@@ -323,11 +345,11 @@ Yields:
323345

324346
###### Notes
325347

326-
* \*Not supported in `matches`
327-
* † — Needs a user, browser, interactivity, or scripting to make sense
328-
* ‡ — Not supported by the underlying algorithm
329-
* § — Not very interested in writing / including the code for this
330-
* ‖ — Too new, the spec is still changing
348+
* \*not supported in `matches`
349+
* † — needs a user, browser, interactivity, or scripting to make sense
350+
* ‡ — not supported by the underlying algorithm
351+
* § — not very interested in writing / including the code for this
352+
* ‖ — too new, the spec is still changing
331353

332354
## Types
333355

@@ -338,12 +360,12 @@ It exports the additional type `Space`.
338360

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

344366
## Security
345367

346-
`hast-util-select` does not change the syntax tree so there are no openings for
368+
This package does not change the syntax tree so there are no openings for
347369
[cross-site scripting (XSS)][xss] attacks.
348370

349371
## Related

0 commit comments

Comments
 (0)