11/**
2- * @typedef {import('mdast').Nodes } Nodes
32 * @typedef {import('mdast').Heading } Heading
3+ * @typedef {import('mdast').Nodes } Nodes
44 * @typedef {import('mdast').PhrasingContent } PhrasingContent
55 * @typedef {import('unist-util-is').Test } Test
66 */
1414 * @typedef SearchOptions
1515 * Search configuration.
1616 * @property {Rank | null | undefined } [maxDepth=6]
17- * Maximum heading depth to include in the table of contents.
17+ * Maximum heading depth to include in the table of contents (default: `6`) .
1818 *
1919 * This is inclusive: when set to `3`, level three headings are included
2020 * (those with three hashes, `###`).
2121 * @property {string | null | undefined } [skip]
22- * Headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
22+ * Headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`
23+ * (default: `undefined`).
2324 *
2425 * Any heading matching this expression will not be present in the table of
2526 * contents.
2627 * @property {Test } [parents]
2728 * Allow headings to be children of certain node types (default: the to `toc`
28- * given `tree`, to only allow top-level headings).
29+ * given `tree`, to only allow top-level headings) (default:
30+ * `d => d === tree`).
2931 *
3032 * Internally, uses `unist-util-is` to check, so `parents` can be any
3133 * `is`-compatible test.
5153
5254import Slugger from 'github-slugger'
5355import { toString } from 'mdast-util-to-string'
54- import { visit } from 'unist-util-visit'
5556import { convert } from 'unist-util-is'
57+ import { visit } from 'unist-util-visit'
5658import { toExpression } from './to-expression.js'
5759
5860const slugs = new Slugger ( )
@@ -66,8 +68,14 @@ const slugs = new Slugger()
6668 * @returns {SearchResult }
6769 */
6870export function search ( root , expression , settings ) {
71+ const max = 'children' in root ? root . children . length : 0
6972 const skip = settings . skip ? toExpression ( settings . skip ) : undefined
70- const parents = convert ( settings . parents || ( ( d ) => d === root ) )
73+ const parents = convert (
74+ settings . parents ||
75+ function ( d ) {
76+ return d === root
77+ }
78+ )
7179 /** @type {Array<SearchEntry> } */
7280 const map = [ ]
7381 /** @type {number | undefined } */
@@ -81,7 +89,7 @@ export function search(root, expression, settings) {
8189
8290 // Visit all headings in `root`. We `slug` all headings (to account for
8391 // duplicates), but only create a TOC from top-level headings (by default).
84- visit ( root , 'heading' , ( node , position , parent ) => {
92+ visit ( root , 'heading' , function ( node , position , parent ) {
8593 const value = toString ( node , { includeImageAlt : false } )
8694 /** @type {string } */
8795 // @ts -expect-error `hProperties` from <https://github.com/syntax-tree/mdast-util-to-hast>
@@ -126,9 +134,7 @@ export function search(root, expression, settings) {
126134
127135 return {
128136 index : index === undefined ? - 1 : index ,
129- // <sindresorhus/eslint-plugin-unicorn#980>
130- // @ts -expect-error Looks like a parent.
131- endIndex : index === undefined ? - 1 : endIndex || root . children . length , // eslint-disable-line unicorn/explicit-length-check
137+ endIndex : index === undefined ? - 1 : endIndex || max ,
132138 map
133139 }
134140}
0 commit comments