1
- 'use strict' ;
1
+ 'use strict'
2
2
3
- var toString = require ( 'mdast-util-to-string' ) ;
3
+ var toString = require ( 'mdast-util-to-string' )
4
4
5
- module . exports = headingRange ;
5
+ module . exports = headingRange
6
6
7
- var splice = [ ] . splice ;
7
+ var splice = [ ] . splice
8
8
9
- /* Search `node` with `options` and invoke `callback`. */
9
+ // Search `node` with `options` and invoke `callback`.
10
10
function headingRange ( node , options , callback ) {
11
- var test = options ;
12
- var ignoreFinalDefinitions = false ;
11
+ var test = options
12
+ var ignoreFinalDefinitions = false
13
13
14
- /* Object, not regex. */
14
+ // Object, not regex.
15
15
if ( test && typeof test === 'object' && ! ( 'exec' in test ) ) {
16
- ignoreFinalDefinitions = test . ignoreFinalDefinitions === true ;
17
- test = test . test ;
16
+ ignoreFinalDefinitions = test . ignoreFinalDefinitions === true
17
+ test = test . test
18
18
}
19
19
20
20
if ( typeof test === 'string' ) {
21
- test = toExpression ( test ) ;
21
+ test = toExpression ( test )
22
22
}
23
23
24
- /* Regex */
24
+ // Regex
25
25
if ( test && 'exec' in test ) {
26
- test = wrapExpression ( test ) ;
26
+ test = wrapExpression ( test )
27
27
}
28
28
29
29
if ( typeof test !== 'function' ) {
30
30
throw new Error (
31
- 'Expected `string`, `regexp`, or `function` for `test`, ' +
32
- 'not `' + test + '`'
33
- ) ;
31
+ 'Expected `string`, `regexp`, or `function` for `test`, not `' +
32
+ test +
33
+ '`'
34
+ )
34
35
}
35
36
36
- search ( node , test , ignoreFinalDefinitions , callback ) ;
37
+ search ( node , test , ignoreFinalDefinitions , callback )
37
38
}
38
39
39
- /* Search a node for heading range. */
40
+ // Search a node for heading range.
40
41
function search ( root , test , skip , callback ) {
41
- var index = - 1 ;
42
- var children = root . children ;
43
- var length = children . length ;
44
- var depth = null ;
45
- var start = null ;
46
- var end = null ;
47
- var nodes ;
48
- var clean ;
49
- var child ;
42
+ var index = - 1
43
+ var children = root . children
44
+ var length = children . length
45
+ var depth = null
46
+ var start = null
47
+ var end = null
48
+ var nodes
49
+ var clean
50
+ var child
50
51
51
52
while ( ++ index < length ) {
52
- child = children [ index ] ;
53
+ child = children [ index ]
53
54
54
55
if ( closing ( child , depth ) ) {
55
- end = index ;
56
- break ;
56
+ end = index
57
+ break
57
58
}
58
59
59
60
if ( opening ( child , depth , test ) ) {
60
- start = index ;
61
- depth = child . depth ;
61
+ start = index
62
+ depth = child . depth
62
63
}
63
64
}
64
65
65
66
if ( start !== null ) {
66
67
if ( end === null ) {
67
- end = length ;
68
+ end = length
68
69
}
69
70
70
71
if ( skip ) {
71
72
while ( end > start ) {
72
- child = children [ end - 1 ] ;
73
+ child = children [ end - 1 ]
73
74
74
75
if ( ! definition ( child ) ) {
75
- break ;
76
+ break
76
77
}
77
78
78
- end -- ;
79
+ end --
79
80
}
80
81
}
81
82
@@ -88,57 +89,56 @@ function search(root, test, skip, callback) {
88
89
start : start ,
89
90
end : children [ end ] ? end : null
90
91
}
91
- ) ;
92
+ )
92
93
93
- clean = [ ] ;
94
- index = - 1 ;
95
- length = nodes && nodes . length ;
94
+ clean = [ ]
95
+ index = - 1
96
+ length = nodes && nodes . length
96
97
97
- /* Ensure no empty nodes are inserted. This could
98
- * be the case if `end` is in `nodes` but no `end`
99
- * node exists. */
98
+ // Ensure no empty nodes are inserted. This could be the case if `end` is
99
+ // in `nodes` but no `end` node exists.
100
100
while ( ++ index < length ) {
101
101
if ( nodes [ index ] ) {
102
- clean . push ( nodes [ index ] ) ;
102
+ clean . push ( nodes [ index ] )
103
103
}
104
104
}
105
105
106
106
if ( nodes ) {
107
- splice . apply ( children , [ start , end - start + 1 ] . concat ( clean ) ) ;
107
+ splice . apply ( children , [ start , end - start + 1 ] . concat ( clean ) )
108
108
}
109
109
}
110
110
}
111
111
112
- /* Transform a string into an applicable expression. */
112
+ // Transform a string into an applicable expression.
113
113
function toExpression ( value ) {
114
- return new RegExp ( '^(' + value + ')$' , 'i' ) ;
114
+ return new RegExp ( '^(' + value + ')$' , 'i' )
115
115
}
116
116
117
- /* Wrap an expression into an assertion function. */
117
+ // Wrap an expression into an assertion function.
118
118
function wrapExpression ( expression ) {
119
- return assertion ;
119
+ return assertion
120
120
121
- /* Assert `value` matches the bound `expression`. */
121
+ // Assert `value` matches the bound `expression`.
122
122
function assertion ( value ) {
123
- return expression . test ( value ) ;
123
+ return expression . test ( value )
124
124
}
125
125
}
126
126
127
- /* Check if `node` is a heading. */
127
+ // Check if `node` is a heading.
128
128
function heading ( node ) {
129
- return node && node . type === 'heading' ;
129
+ return node && node . type === 'heading'
130
130
}
131
131
132
- /* Check if `node` is the main heading. */
132
+ // Check if `node` is the main heading.
133
133
function opening ( node , depth , test ) {
134
- return depth === null && heading ( node ) && test ( toString ( node ) , node ) ;
134
+ return depth === null && heading ( node ) && test ( toString ( node ) , node )
135
135
}
136
136
137
- /* Check if `node` is the next heading. */
137
+ // Check if `node` is the next heading.
138
138
function closing ( node , depth ) {
139
- return depth && heading ( node ) && node . depth <= depth ;
139
+ return depth && heading ( node ) && node . depth <= depth
140
140
}
141
141
142
142
function definition ( node ) {
143
- return node . type === 'definition' || node . type === 'footnoteDefinition' ;
143
+ return node . type === 'definition' || node . type === 'footnoteDefinition'
144
144
}
0 commit comments