2
2
* @typedef {import('unist').Node } UnistNode
3
3
* @typedef {import('hast').Parent } Parent
4
4
* @typedef {import('hast').Root } Root
5
- * @typedef {import('hast').DocType } Doctype
6
5
* @typedef {import('hast').Element } Element
7
- * @typedef {import('hast').Text } Text
8
- * @typedef {import('hast').Comment } Comment
9
6
* @typedef {Parent['children'][number] } Child
10
7
* @typedef {Element['children'][number] } ElementChild
11
8
* @typedef {Child|Root } Node
@@ -31,20 +28,20 @@ import {toString as nlcstToString} from 'nlcst-to-string'
31
28
import { pointStart } from 'unist-util-position'
32
29
import vfileLocation from 'vfile-location'
33
30
34
- var push = [ ] . push
31
+ const push = [ ] . push
35
32
36
- var source = convertElement ( [ 'code' , dataNlcstSourced ] )
37
- var ignore = convertElement ( [
33
+ const source = convertElement ( [ 'code' , dataNlcstSourced ] )
34
+ const ignore = convertElement ( [
38
35
'script' ,
39
36
'style' ,
40
37
'svg' ,
41
38
'math' ,
42
39
'del' ,
43
40
dataNlcstIgnore
44
41
] )
45
- var explicit = convertElement ( [ 'p' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] )
42
+ const explicit = convertElement ( [ 'p' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] )
46
43
47
- var flowAccepting = convertElement ( [
44
+ const flowAccepting = convertElement ( [
48
45
'body' ,
49
46
'article' ,
50
47
'section' ,
@@ -71,7 +68,7 @@ var flowAccepting = convertElement([
71
68
] )
72
69
73
70
// See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
74
- var unravelInParagraph = convertElement ( [ 'a' , 'ins' , 'del' , 'map' ] )
71
+ const unravelInParagraph = convertElement ( [ 'a' , 'ins' , 'del' , 'map' ] )
75
72
76
73
/**
77
74
* Transform `tree` to nlcst.
@@ -81,15 +78,6 @@ var unravelInParagraph = convertElement(['a', 'ins', 'del', 'map'])
81
78
* @param {ParserInstance|ParserConstructor } Parser
82
79
*/
83
80
export function toNlcst ( tree , file , Parser ) {
84
- /** @type {ParserInstance } */
85
- var parser
86
- /** @type {Location } */
87
- var location
88
- /** @type {Array.<UnistNode> } */
89
- var results
90
- /** @type {string } */
91
- var doc
92
-
93
81
// Warn for invalid parameters.
94
82
if ( ! tree || ! tree . type ) {
95
83
throw new Error ( 'hast-util-to-nlcst expected node' )
@@ -108,13 +96,11 @@ export function toNlcst(tree, file, Parser) {
108
96
throw new Error ( 'hast-util-to-nlcst expected position on nodes' )
109
97
}
110
98
111
- doc = String ( file )
112
- location = vfileLocation ( doc )
113
- parser = 'parse' in Parser ? Parser : new Parser ( )
114
-
115
- // Transform hast to nlcst, and pass these into `parser.parse` to insert
116
- // sentences, paragraphs where needed.
117
- results = [ ]
99
+ const doc = String ( file )
100
+ const location = vfileLocation ( doc )
101
+ const parser = 'parse' in Parser ? Parser : new Parser ( )
102
+ /** @type {Array.<UnistNode> } */
103
+ const results = [ ]
118
104
119
105
find ( tree )
120
106
@@ -148,7 +134,7 @@ export function toNlcst(tree, file, Parser) {
148
134
* @param {Array.<Child> } children
149
135
*/
150
136
function findAll ( children ) {
151
- var index = - 1
137
+ let index = - 1
152
138
153
139
while ( ++ index < children . length ) {
154
140
find ( children [ index ] )
@@ -161,8 +147,8 @@ export function toNlcst(tree, file, Parser) {
161
147
*/
162
148
function flattenAll ( children ) {
163
149
/** @type {Array.<ElementChild> } */
164
- var results = [ ]
165
- var index = - 1
150
+ const results = [ ]
151
+ let index = - 1
166
152
167
153
while ( ++ index < children . length ) {
168
154
if ( unravelInParagraph ( children [ index ] ) ) {
@@ -182,7 +168,7 @@ export function toNlcst(tree, file, Parser) {
182
168
function add ( node ) {
183
169
/** @type {Array.<UnistNode> } */
184
170
// @ts -ignore Assume child.
185
- var result = Array . isArray ( node ) ? all ( node ) : one ( node )
171
+ const result = Array . isArray ( node ) ? all ( node ) : one ( node )
186
172
187
173
if ( result . length > 0 ) {
188
174
results . push ( parser . tokenizeParagraph ( result ) )
@@ -193,15 +179,13 @@ export function toNlcst(tree, file, Parser) {
193
179
* @param {Array.<ElementChild> } children
194
180
*/
195
181
function implicit ( children ) {
196
- var index = - 1
197
- var start = - 1
182
+ let index = - 1
183
+ let start = - 1
198
184
/** @type {boolean } */
199
- var viable
200
- /** @type {ElementChild } */
201
- var child
185
+ let viable
202
186
203
187
while ( ++ index <= children . length ) {
204
- child = children [ index ]
188
+ const child = children [ index ]
205
189
206
190
if ( child && phrasing ( child ) ) {
207
191
if ( start === - 1 ) start = index
@@ -233,9 +217,9 @@ export function toNlcst(tree, file, Parser) {
233
217
*/
234
218
function one ( node ) {
235
219
/** @type {Array.<UnistNode> } */
236
- var replacement
220
+ let replacement
237
221
/** @type {boolean } */
238
- var change
222
+ let change
239
223
240
224
if ( node . type === 'text' ) {
241
225
replacement = parser . tokenize ( node . value )
@@ -268,8 +252,8 @@ export function toNlcst(tree, file, Parser) {
268
252
*/
269
253
function all ( children ) {
270
254
/** @type {Array.<UnistNode> } */
271
- var results = [ ]
272
- var index = - 1
255
+ const results = [ ]
256
+ let index = - 1
273
257
274
258
while ( ++ index < children . length ) {
275
259
push . apply ( results , one ( children [ index ] ) || [ ] )
@@ -292,22 +276,18 @@ export function toNlcst(tree, file, Parser) {
292
276
* @returns {T }
293
277
*/
294
278
function patch ( nodes , location , offset ) {
295
- var index = - 1
296
- var start = offset
297
- /** @type {number } */
298
- var end
299
- /** @type {UnistNode } */
300
- var node
279
+ let index = - 1
280
+ let start = offset
301
281
302
282
while ( ++ index < nodes . length ) {
303
- node = nodes [ index ]
283
+ const node = nodes [ index ]
304
284
305
285
if ( 'children' in node ) {
306
286
// @ts -ignore Looks like a parent.
307
287
patch ( node . children , location , start )
308
288
}
309
289
310
- end = start + nlcstToString ( node ) . length
290
+ const end = start + nlcstToString ( node ) . length
311
291
312
292
node . position = {
313
293
start : location . toPoint ( start ) ,
0 commit comments