Skip to content

Commit d848bed

Browse files
committed
Add improved docs
1 parent 2a15bff commit d848bed

File tree

2 files changed

+73
-27
lines changed

2 files changed

+73
-27
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ const search = /[#.]/g
88
/**
99
* Create a hast element from a simple CSS selector.
1010
*
11-
* @param selector A simple CSS selector.
11+
* @param selector
12+
* A simple CSS selector.
1213
* Can contain a tag-name (`foo`), classes (`.bar`), and an ID (`#baz`).
1314
* Multiple classes are allowed.
1415
* Uses the last ID if multiple IDs are found.
15-
* @param [defaultTagName='div'] Tag name to use if `selector` does not specify one.
16+
* @param [defaultTagName='div']
17+
* Tag name to use if `selector` does not specify one.
1618
*/
1719
export const parseSelector =
1820
/**

readme.md

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,41 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**hast**][hast] utility to create an [*element*][element] from a simple CSS
12-
selector.
11+
[hast][] utility to create an element from a simple CSS selector.
1312

14-
## Install
13+
## What is this?
14+
15+
This package is a tiny utility that helps create elements.
1516

16-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
17-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17+
## When should I use this?
1818

19-
[npm][]:
19+
This utility is super niche.
20+
You probably want the more powerful [`hastscript`][hastscript] or
21+
[`hast-util-from-selector`][hast-util-from-selector]
22+
23+
## Install
24+
25+
This package is [ESM only][esm].
26+
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
2027

2128
```sh
2229
npm install hast-util-parse-selector
2330
```
2431

32+
In Deno with [`esm.sh`][esmsh]:
33+
34+
```js
35+
import {parseSelector} from 'https://esm.sh/hast-util-parse-selector@3'
36+
```
37+
38+
In browsers with [`esm.sh`][esmsh]:
39+
40+
```html
41+
<script type="module">
42+
import {parseSelector} from 'https://esm.sh/hast-util-parse-selector@3?bundle'
43+
</script>
44+
```
45+
2546
## Use
2647

2748
```js
@@ -41,36 +62,47 @@ Yields:
4162

4263
## API
4364

44-
This package exports the following identifiers: `parseSelector`.
65+
This package exports the identifier `parseSelector`.
4566
There is no default export.
4667

4768
### `parseSelector([selector][, defaultTagName])`
4869

4970
Create an [*element*][element] [*node*][node] from a simple CSS selector.
5071

51-
###### `selector`
52-
53-
`string`, optional — Can contain a tag name (`foo`), classes (`.bar`),
54-
and an ID (`#baz`).
55-
Multiple classes are allowed.
56-
Uses the last ID if multiple IDs are found.
72+
###### Parameters
5773

58-
###### `defaultTagName`
59-
60-
`string`, optional, defaults to `div` — Tag name to use if `selector` does not
61-
specify one.
74+
* `selector` (`string`, optional)
75+
— can contain a tag name (`foo`), classes (`.bar`), and an ID (`#baz`),
76+
multiple classes are allowed, and uses the last ID if multiple IDs are found
77+
* `defaultTagName` (`string`, default: `'div'`)
78+
— tag name to use if `selector` does not specify one
6279

6380
###### Returns
6481

6582
[`Element`][element].
6683

84+
## Types
85+
86+
This package is fully typed with [TypeScript][].
87+
It exports no additional types.
88+
In TypeScript 4.2+, the type system can infer the tag name of literal
89+
`selector`s and knows that the return element has that name.
90+
91+
## Compatibility
92+
93+
Projects maintained by the unified collective are compatible with all maintained
94+
versions of Node.js.
95+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
96+
Our projects sometimes work with older versions, but this is not guaranteed.
97+
6798
## Security
6899

69100
Improper use of the `selector` or `defaultTagName` can open you up to a
70101
[cross-site scripting (XSS)][xss] attack as the value of `tagName`, when
71102
resolving to `script`, injects a `script` element into the syntax tree.
72103

73-
Do not use user input in `selector` or use [`hast-util-santize`][sanitize].
104+
Do not use user input in `selector` or use
105+
[`hast-util-santize`][hast-util-sanitize].
74106

75107
## Related
76108

@@ -79,8 +111,8 @@ Do not use user input in `selector` or use [`hast-util-santize`][sanitize].
79111

80112
## Contribute
81113

82-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
83-
started.
114+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
115+
ways to get started.
84116
See [`support.md`][support] for ways to get help.
85117

86118
This project has a [code of conduct][coc].
@@ -121,22 +153,34 @@ abide by its terms.
121153

122154
[npm]: https://docs.npmjs.com/cli/install
123155

156+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
157+
158+
[esmsh]: https://esm.sh
159+
160+
[typescript]: https://www.typescriptlang.org
161+
124162
[license]: license
125163

126164
[author]: https://wooorm.com
127165

128-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
166+
[health]: https://github.com/syntax-tree/.github
167+
168+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
129169

130-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
170+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
131171

132-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
172+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
133173

134174
[hast]: https://github.com/syntax-tree/hast
135175

176+
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
177+
178+
[hastscript]: https://github.com/syntax-tree/hastscript
179+
180+
[hast-util-from-selector]: https://github.com/syntax-tree/hast-util-from-selector
181+
136182
[node]: https://github.com/syntax-tree/hast#nodes
137183

138184
[element]: https://github.com/syntax-tree/hast#element
139185

140186
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
141-
142-
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize

0 commit comments

Comments
 (0)