Skip to content

Commit c96a9eb

Browse files
committed
Refactor code-style
1 parent f82228d commit c96a9eb

File tree

4 files changed

+141
-127
lines changed

4 files changed

+141
-127
lines changed

lib/contents.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
* @typedef ContentsOptions
1010
* Build configuration.
1111
* @property {boolean | null | undefined} [tight=false]
12-
* Whether to compile list items tightly.
12+
* Whether to compile list items tightly (default: `false`).
1313
* @property {boolean | null | undefined} [ordered=false]
1414
* Whether to compile list items as an ordered list, otherwise they are
15-
* unordered.
15+
* unordered (default: `false`).
1616
* @property {string | null | undefined} [prefix=undefined]
17-
* Add a prefix to links to headings in the table of contents.
17+
* Add a prefix to links to headings in the table of contents (default:
18+
* `undefined`).
1819
*
1920
* Useful for example when later going from mdast to hast and sanitizing with
2021
* `hast-util-sanitize`.
@@ -99,7 +100,7 @@ function insert(entry, parent, settings) {
99100
insert(entry, item, settings)
100101
}
101102
}
102-
// List item
103+
// List item.
103104
else if (tail && tail.type === 'list') {
104105
entry.depth--
105106
insert(entry, tail, settings)
@@ -131,26 +132,30 @@ function insert(entry, parent, settings) {
131132
}
132133

133134
/**
134-
* @param {Array<PhrasingContent>} [nodes]
135+
* @param {Array<PhrasingContent>} nodes
135136
* @returns {Array<PhrasingContent>}
136137
*/
137138
function all(nodes) {
138139
/** @type {Array<PhrasingContent>} */
139-
let result = []
140+
const results = []
140141
let index = -1
141142

142-
if (nodes) {
143-
while (++index < nodes.length) {
144-
result = result.concat(one(nodes[index]))
143+
while (++index < nodes.length) {
144+
const result = one(nodes[index])
145+
146+
if (Array.isArray(result)) {
147+
results.push(...result)
148+
} else {
149+
results.push(result)
145150
}
146151
}
147152

148-
return result
153+
return results
149154
}
150155

151156
/**
152157
* @param {PhrasingContent} node
153-
* @returns {PhrasingContent | Array<PhrasingContent>}
158+
* @returns {Array<PhrasingContent> | PhrasingContent}
154159
*/
155160
function one(node) {
156161
if (node.type === 'footnoteReference') {

lib/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/**
2-
* @typedef {import('mdast').Nodes} Nodes
32
* @typedef {import('mdast').List} List
3+
* @typedef {import('mdast').Nodes} Nodes
44
* @typedef {import('./search.js').SearchOptions} SearchOptions
55
* @typedef {import('./contents.js').ContentsOptions} ContentsOptions
66
*/
77

88
/**
9-
* @typedef {SearchOptions & ContentsOptions & ExtraOptions} Options
9+
* @typedef {ContentsOptions & ExtraOptions & SearchOptions} Options
1010
*
1111
* @typedef ExtraOptions
1212
* Extra configuration fields.
1313
* @property {string | null | undefined} [heading]
14-
* Heading to look for, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
14+
* Heading to look for, wrapped in `new RegExp('^(' + value + ')$', 'i')`
15+
* (default: `undefined`).
1516
*
1617
* @typedef Result
1718
* Results.
@@ -49,7 +50,7 @@ import {toExpression} from './to-expression.js'
4950
* @param {Nodes} tree
5051
* Tree to search and generate from.
5152
* @param {Options | null | undefined} [options]
52-
* Configuration.
53+
* Configuration (optional).
5354
* @returns {Result}
5455
* Results.
5556
*/

lib/search.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
*/
@@ -14,18 +14,20 @@
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.
@@ -51,8 +53,8 @@
5153

5254
import Slugger from 'github-slugger'
5355
import {toString} from 'mdast-util-to-string'
54-
import {visit} from 'unist-util-visit'
5556
import {convert} from 'unist-util-is'
57+
import {visit} from 'unist-util-visit'
5658
import {toExpression} from './to-expression.js'
5759

5860
const slugs = new Slugger()
@@ -66,8 +68,14 @@ const slugs = new Slugger()
6668
* @returns {SearchResult}
6769
*/
6870
export 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

Comments
 (0)