Skip to content

Commit 75078c4

Browse files
committed
Refactor code-style
1 parent 6a09f12 commit 75078c4

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

index.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,61 @@
44
* @typedef {import('mdast').Heading} Heading
55
*
66
* @typedef {(value: string, node: Heading) => boolean} TestFunction
7+
* Function called for each heading with its content and `node` itself check
8+
* if it’s the one to look for.
9+
*
710
* @typedef {string|RegExp|TestFunction} Test
811
*
912
* @typedef Options
13+
* Configuration (optional).
1014
* @property {Test} test
15+
* Heading to look for.
16+
* When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
17+
* when `RegExp`, wrapped in `function (value) {expression.test(value)}`
1118
* @property {boolean} [ignoreFinalDefinitions=false]
19+
* Ignore final definitions otherwise in the section.
1220
*
1321
* @typedef ZoneInfo
22+
* Extra info.
23+
* @property {Parent|null} parent
24+
* Parent of the range.
1425
* @property {number} start
26+
* index of `start` in `parent`
1527
* @property {number} end
16-
* @property {Parent|null} parent
28+
* index of `end` in `parent`
1729
*
1830
* @callback Handler
31+
* Callback called when a range is found.
1932
* @param {Heading|undefined} start
20-
* @param {Array.<Node>} between
33+
* Start of range.
34+
* @param {Array<Node>} between
35+
* Nodes between `start` and `end`.
2136
* @param {Node|undefined} end
22-
* @param {ZoneInfo} info
37+
* End of range, if any.
38+
* @param {ZoneInfo} scope
39+
* Extra info.
2340
*/
2441

2542
import {toString} from 'mdast-util-to-string'
2643

2744
/**
28-
* Search `node` with `options` and invoke `callback`.
45+
* Search `tree` and transform a section without affecting other parts with
46+
* `handler`.
47+
*
48+
* A “section” is a heading that passes `test`, until the next heading of the
49+
* same or lower depth, or the end of the document.
50+
* If `ignoreFinalDefinitions: true`, final definitions “in” the section are
51+
* excluded.
2952
*
30-
* @param {Node} node
53+
* @param {Node} tree
3154
* @param {Test|Options} options
3255
* @param {Handler} handler
3356
*/
3457
// eslint-disable-next-line complexity
35-
export function headingRange(node, options, handler) {
58+
export function headingRange(tree, options, handler) {
3659
let test = options
37-
/** @type {Array.<Node>} */
38-
const children = 'children' in node ? node.children : []
60+
/** @type {Array<Node>} */
61+
const children = 'children' in tree ? tree.children : []
3962
/** @type {boolean|undefined} */
4063
let ignoreFinalDefinitions
4164

@@ -101,19 +124,19 @@ export function headingRange(node, options, handler) {
101124
}
102125
}
103126

104-
/** @type {Array.<Node>} */
127+
/** @type {Array<Node>} */
105128
const nodes = handler(
106129
// @ts-expect-error `start` points to a heading.
107130
children[start],
108131
children.slice(start + 1, end),
109132
children[end],
110-
{parent: node, start, end: children[end] ? end : null}
133+
{parent: tree, start, end: children[end] ? end : null}
111134
)
112135

113136
if (nodes) {
114137
// Ensure no empty nodes are inserted.
115138
// This could be the case if `end` is in `nodes` but no `end` node exists.
116-
/** @type {Array.<Node>} */
139+
/** @type {Array<Node>} */
117140
const result = []
118141
let index = -1
119142

0 commit comments

Comments
 (0)