1
- 'use strict' ;
1
+ 'use strict'
2
2
3
- var vfileLocation = require ( 'vfile-location' ) ;
4
- var toString = require ( 'nlcst-to-string' ) ;
5
- var position = require ( 'unist-util-position' ) ;
6
- var phrasing = require ( 'hast-util-phrasing' ) ;
7
- var embedded = require ( 'hast-util-embedded' ) ;
8
- var whitespace = require ( 'hast-util-whitespace' ) ;
9
- var textContent = require ( 'hast-util-to-string' ) ;
10
- var is = require ( 'hast-util-is-element' ) ;
3
+ var vfileLocation = require ( 'vfile-location' )
4
+ var toString = require ( 'nlcst-to-string' )
5
+ var position = require ( 'unist-util-position' )
6
+ var phrasing = require ( 'hast-util-phrasing' )
7
+ var embedded = require ( 'hast-util-embedded' )
8
+ var whitespace = require ( 'hast-util-whitespace' )
9
+ var textContent = require ( 'hast-util-to-string' )
10
+ var is = require ( 'hast-util-is-element' )
11
11
12
- module . exports = toNLCST ;
12
+ module . exports = toNLCST
13
13
14
14
/* Elements representing source. */
15
- var SOURCE = [ 'code' ] ;
16
- var IGNORE = [ 'script' , 'style' , 'svg' , 'math' , 'del' ] ;
17
- var EXPLICIT = [ 'p' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] ;
15
+ var SOURCE = [ 'code' ]
16
+ var IGNORE = [ 'script' , 'style' , 'svg' , 'math' , 'del' ]
17
+ var EXPLICIT = [ 'p' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ]
18
18
19
19
/* Constants. */
20
20
var FLOW_ACCEPTING = [
@@ -41,43 +41,43 @@ var FLOW_ACCEPTING = [
41
41
'fieldset' ,
42
42
'details' ,
43
43
'dialog'
44
- ] ;
44
+ ]
45
45
46
46
/* Transform `tree` to `nlcst`. */
47
47
function toNLCST ( tree , file , Parser ) {
48
- var parser ;
49
- var location ;
50
- var results ;
51
- var doc ;
48
+ var parser
49
+ var location
50
+ var results
51
+ var doc
52
52
53
53
/* Warn for invalid parameters. */
54
54
if ( ! tree || ! tree . type ) {
55
- throw new Error ( 'hast-util-to-nlcst expected node' ) ;
55
+ throw new Error ( 'hast-util-to-nlcst expected node' )
56
56
}
57
57
58
58
if ( ! file || ! file . messages ) {
59
- throw new Error ( 'hast-util-to-nlcst expected file' ) ;
59
+ throw new Error ( 'hast-util-to-nlcst expected file' )
60
60
}
61
61
62
62
/* Construct parser. */
63
63
if ( ! Parser ) {
64
- throw new Error ( 'hast-util-to-nlcst expected parser' ) ;
64
+ throw new Error ( 'hast-util-to-nlcst expected parser' )
65
65
}
66
66
67
67
if ( ! position . start ( tree ) . line || ! position . start ( tree ) . column ) {
68
- throw new Error ( 'hast-util-to-nlcst expected position on nodes' ) ;
68
+ throw new Error ( 'hast-util-to-nlcst expected position on nodes' )
69
69
}
70
70
71
- location = vfileLocation ( file ) ;
72
- doc = String ( file ) ;
73
- parser = 'parse' in Parser ? Parser : new Parser ( ) ;
71
+ location = vfileLocation ( file )
72
+ doc = String ( file )
73
+ parser = 'parse' in Parser ? Parser : new Parser ( )
74
74
75
75
/* Transform HAST into NLCST tokens, and pass these
76
76
* into `parser.parse` to insert sentences, paragraphs
77
77
* where needed. */
78
- results = [ ] ;
78
+ results = [ ]
79
79
80
- find ( tree ) ;
80
+ find ( tree )
81
81
82
82
return {
83
83
type : 'RootNode' ,
@@ -86,146 +86,146 @@ function toNLCST(tree, file, Parser) {
86
86
start : location . toPosition ( 0 ) ,
87
87
end : location . toPosition ( doc . length )
88
88
}
89
- } ;
89
+ }
90
90
91
91
function find ( node ) {
92
- var children = node . children ;
92
+ var children = node . children
93
93
94
94
if ( node . type === 'root' ) {
95
- findAll ( children ) ;
95
+ findAll ( children )
96
96
} else if ( is ( node ) && ! ignored ( node ) ) {
97
97
/* Explicit paragraph. */
98
98
if ( is ( node , EXPLICIT ) ) {
99
- add ( node ) ;
100
- /* Slightly simplified version of:
99
+ add ( node )
100
+ /* Slightly simplified version of:
101
101
* https://html.spec.whatwg.org/#paragraphs */
102
102
} else if ( is ( node , FLOW_ACCEPTING ) ) {
103
- implicit ( flattenAll ( children ) ) ;
104
- /* Dig deeper. */
103
+ implicit ( flattenAll ( children ) )
104
+ /* Dig deeper. */
105
105
} else {
106
- findAll ( children ) ;
106
+ findAll ( children )
107
107
}
108
108
}
109
109
}
110
110
111
111
function findAll ( children ) {
112
- var length = children . length ;
113
- var index = - 1 ;
112
+ var length = children . length
113
+ var index = - 1
114
114
115
115
while ( ++ index < length ) {
116
- find ( children [ index ] ) ;
116
+ find ( children [ index ] )
117
117
}
118
118
}
119
119
120
120
function flatten ( node ) {
121
121
if ( is ( node , [ 'a' , 'ins' , 'del' , 'map' ] ) ) {
122
- return flattenAll ( node . children ) ;
122
+ return flattenAll ( node . children )
123
123
}
124
124
125
- return node ;
125
+ return node
126
126
}
127
127
128
128
function flattenAll ( children ) {
129
- var results = [ ] ;
130
- var length = children . length ;
131
- var index = - 1 ;
129
+ var results = [ ]
130
+ var length = children . length
131
+ var index = - 1
132
132
133
133
while ( ++ index < length ) {
134
- results = results . concat ( flatten ( children [ index ] ) ) ;
134
+ results = results . concat ( flatten ( children [ index ] ) )
135
135
}
136
136
137
- return results ;
137
+ return results
138
138
}
139
139
140
140
function add ( node ) {
141
- var result = ( 'length' in node ? all : one ) ( node ) ;
141
+ var result = ( 'length' in node ? all : one ) ( node )
142
142
143
143
if ( result . length !== 0 ) {
144
- results . push ( parser . tokenizeParagraph ( result ) ) ;
144
+ results . push ( parser . tokenizeParagraph ( result ) )
145
145
}
146
146
}
147
147
148
148
function implicit ( children ) {
149
- var length = children . length + 1 ;
150
- var index = - 1 ;
151
- var viable = false ;
152
- var start = - 1 ;
153
- var child ;
149
+ var length = children . length + 1
150
+ var index = - 1
151
+ var viable = false
152
+ var start = - 1
153
+ var child
154
154
155
155
while ( ++ index < length ) {
156
- child = children [ index ] ;
156
+ child = children [ index ]
157
157
158
158
if ( child && phrasing ( child ) ) {
159
159
if ( start === - 1 ) {
160
- start = index ;
160
+ start = index
161
161
}
162
162
163
163
if ( ! viable && ! embedded ( child ) && ! whitespace ( child ) ) {
164
- viable = true ;
164
+ viable = true
165
165
}
166
166
} else if ( child && start === - 1 ) {
167
- find ( child ) ;
167
+ find ( child )
168
168
} else {
169
- ( viable ? add : findAll ) ( children . slice ( start , index ) ) ;
169
+ ; ( viable ? add : findAll ) ( children . slice ( start , index ) )
170
170
171
171
if ( child ) {
172
- find ( child ) ;
172
+ find ( child )
173
173
}
174
174
175
- viable = false ;
176
- start = - 1 ;
175
+ viable = false
176
+ start = - 1
177
177
}
178
178
}
179
179
}
180
180
181
181
/* Convert `node` (HAST) to NLCST. */
182
182
function one ( node ) {
183
- var type = node . type ;
184
- var tagName = type === 'element' ? node . tagName : null ;
185
- var change ;
186
- var replacement ;
183
+ var type = node . type
184
+ var tagName = type === 'element' ? node . tagName : null
185
+ var change
186
+ var replacement
187
187
188
188
if ( type === 'text' ) {
189
- change = true ;
190
- replacement = parser . tokenize ( node . value ) ;
189
+ change = true
190
+ replacement = parser . tokenize ( node . value )
191
191
} else if ( tagName === 'wbr' ) {
192
- change = true ;
193
- replacement = [ parser . tokenizeWhiteSpace ( ' ' ) ] ;
192
+ change = true
193
+ replacement = [ parser . tokenizeWhiteSpace ( ' ' ) ]
194
194
} else if ( tagName === 'br' ) {
195
- change = true ;
196
- replacement = [ parser . tokenizeWhiteSpace ( '\n' ) ] ;
195
+ change = true
196
+ replacement = [ parser . tokenizeWhiteSpace ( '\n' ) ]
197
197
} else if ( sourced ( node ) ) {
198
- change = true ;
199
- replacement = [ parser . tokenizeSource ( textContent ( node ) ) ] ;
198
+ change = true
199
+ replacement = [ parser . tokenizeSource ( textContent ( node ) ) ]
200
200
} else if ( type === 'root' || ! ignored ( node ) ) {
201
- replacement = all ( node . children ) ;
201
+ replacement = all ( node . children )
202
202
} else {
203
- return ;
203
+ return
204
204
}
205
205
206
206
if ( ! change ) {
207
- return replacement ;
207
+ return replacement
208
208
}
209
209
210
- return patch ( replacement , location , location . toOffset ( position . start ( node ) ) ) ;
210
+ return patch ( replacement , location , location . toOffset ( position . start ( node ) ) )
211
211
}
212
212
213
213
/* Convert all `children` (HAST) to NLCST. */
214
214
function all ( children ) {
215
- var length = children && children . length ;
216
- var index = - 1 ;
217
- var result = [ ] ;
218
- var child ;
215
+ var length = children && children . length
216
+ var index = - 1
217
+ var result = [ ]
218
+ var child
219
219
220
220
while ( ++ index < length ) {
221
- child = one ( children [ index ] ) ;
221
+ child = one ( children [ index ] )
222
222
223
223
if ( child ) {
224
- result = result . concat ( child ) ;
224
+ result = result . concat ( child )
225
225
}
226
226
}
227
227
228
- return result ;
228
+ return result
229
229
}
230
230
231
231
/* Patch a position on each node in `nodes`.
@@ -236,41 +236,41 @@ function toNLCST(tree, file, Parser) {
236
236
* starting and ending positions can be inferred from their
237
237
* content. */
238
238
function patch ( nodes , location , offset ) {
239
- var length = nodes . length ;
240
- var index = - 1 ;
241
- var start = offset ;
242
- var children ;
243
- var node ;
244
- var end ;
239
+ var length = nodes . length
240
+ var index = - 1
241
+ var start = offset
242
+ var children
243
+ var node
244
+ var end
245
245
246
246
while ( ++ index < length ) {
247
- node = nodes [ index ] ;
248
- children = node . children ;
247
+ node = nodes [ index ]
248
+ children = node . children
249
249
250
250
if ( children ) {
251
- patch ( children , location , start ) ;
251
+ patch ( children , location , start )
252
252
}
253
253
254
- end = start + toString ( node ) . length ;
254
+ end = start + toString ( node ) . length
255
255
256
256
node . position = {
257
257
start : location . toPosition ( start ) ,
258
258
end : location . toPosition ( end )
259
- } ;
259
+ }
260
260
261
- start = end ;
261
+ start = end
262
262
}
263
263
264
- return nodes ;
264
+ return nodes
265
265
}
266
266
}
267
267
268
268
function sourced ( node ) {
269
- var props = node . properties ;
270
- return is ( node ) && ( is ( node , SOURCE ) || props . dataNlcst === 'source' ) ;
269
+ var props = node . properties
270
+ return is ( node ) && ( is ( node , SOURCE ) || props . dataNlcst === 'source' )
271
271
}
272
272
273
273
function ignored ( node ) {
274
- var props = node . properties ;
275
- return is ( node ) && ( is ( node , IGNORE ) || props . dataNlcst === 'ignore' ) ;
274
+ var props = node . properties
275
+ return is ( node ) && ( is ( node , IGNORE ) || props . dataNlcst === 'ignore' )
276
276
}
0 commit comments