1
1
/**
2
- * @typedef {import('mdast').Root|import('mdast').Content } Node
2
+ * @typedef {import('mdast').Root } Root
3
+ * @typedef {import('mdast').Content } Content
3
4
* @typedef {import('mdast').Heading } Heading
4
5
* @typedef {import('mdast').PhrasingContent } PhrasingContent
5
- * @typedef {string } IsType
6
- * @typedef {Record<string, unknown> } IsProps
7
- * @typedef {import('unist-util-is').TestFunctionAnything } IsTestFunctionAnything
6
+ * @typedef {import('unist-util-is').Test } Test
7
+ */
8
+
9
+ /**
10
+ * @typedef {Root | Content } Node
11
+ * @typedef {Heading['depth'] } Rank
8
12
*
13
+ */
14
+
15
+ /**
9
16
* @typedef SearchOptions
10
- * @property {string } [skip]
17
+ * Search configuration.
18
+ * @property {string | null | undefined } [skip]
11
19
* Headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
12
20
* Any heading matching this expression will not be present in the table of
13
21
* contents.
14
- * @property {IsType|IsProps|IsTestFunctionAnything|Array<IsType|IsProps|IsTestFunctionAnything> } [parents]
22
+ * @property {Test } [parents]
15
23
* Allow headings to be children of certain node types (default: the to `toc`
16
24
* given `tree`, to only allow top-level headings).
25
+ *
17
26
* Internally, uses `unist-util-is` to check, so `parents` can be any
18
27
* `is`-compatible test.
19
- * @property {Heading['depth'] } [maxDepth=6]
28
+ * @property {Rank | null | undefined } [maxDepth=6]
20
29
* Maximum heading depth to include in the table of contents.
30
+ *
21
31
* This is inclusive: when set to `3`, level three headings are included
22
32
* (those with three hashes, `###`).
23
33
*
24
34
* @typedef SearchEntry
25
- * @property {Heading['depth'] } depth
26
- * @property {Array<PhrasingContent> } children
35
+ * Entry.
27
36
* @property {string } id
37
+ * ID of entry.
38
+ * @property {Array<PhrasingContent> } children
39
+ * Contents of entry.
40
+ * @property {Rank } depth
41
+ * Rank of entry.
28
42
*
29
43
* @typedef SearchResult
44
+ * Results.
30
45
* @property {number } index
46
+ * Where the contents section starts, if looking for a heading.
31
47
* @property {number } endIndex
48
+ * Where the contents section ends, if looking for a heading.
32
49
* @property {Array<SearchEntry> } map
50
+ * List of entries.
33
51
*/
34
52
35
53
import Slugger from 'github-slugger'
@@ -44,20 +62,20 @@ const slugs = new Slugger()
44
62
* Search a node for a toc.
45
63
*
46
64
* @param {Node } root
47
- * @param {RegExp|null } expression
65
+ * @param {RegExp | undefined } expression
48
66
* @param {SearchOptions } settings
49
67
* @returns {SearchResult }
50
68
*/
51
69
export function search ( root , expression , settings ) {
52
- const skip = settings . skip && toExpression ( settings . skip )
70
+ const skip = settings . skip ? toExpression ( settings . skip ) : undefined
53
71
const parents = convert ( settings . parents || ( ( d ) => d === root ) )
54
72
/** @type {Array<SearchEntry> } */
55
73
const map = [ ]
56
- /** @type {number| undefined } */
74
+ /** @type {number | undefined } */
57
75
let index
58
- /** @type {number } */
76
+ /** @type {number | undefined } */
59
77
let endIndex
60
- /** @type {Heading } */
78
+ /** @type {Heading | undefined } */
61
79
let opening
62
80
63
81
slugs . reset ( )
@@ -103,10 +121,10 @@ export function search(root, expression, settings) {
103
121
} )
104
122
105
123
return {
106
- index : index || - 1 ,
124
+ index : index === undefined ? - 1 : index ,
107
125
// <sindresorhus/eslint-plugin-unicorn#980>
108
126
// @ts -expect-error Looks like a parent.
109
- endIndex : index ? endIndex || root . children . length : - 1 , // eslint-disable-line unicorn/explicit-length-check
127
+ endIndex : index === undefined ? - 1 : endIndex || root . children . length , // eslint-disable-line unicorn/explicit-length-check
110
128
map
111
129
}
112
130
}
0 commit comments