1
1
/**
2
- * @typedef {import('unist').Point } Point
3
- *
4
2
* @typedef {import('nlcst').Root } NlcstRoot
5
3
* @typedef {import('nlcst').Paragraph } NlcstParagraph
6
- * @typedef {import('nlcst').WhiteSpace } NlcstWhiteSpace
7
4
* @typedef {import('nlcst').Sentence } NlcstSentence
8
- * @typedef {import('nlcst').Source } NlcstSource
9
5
* @typedef {import('nlcst').Content } NlcstContent
10
6
* @typedef {import('nlcst').SentenceContent } NlcstSentenceContent
11
- * @typedef {NlcstRoot|NlcstContent } NlcstNode
12
- * @typedef {Extract<NlcstNode, import('unist').Parent> } NlcstParent
13
- *
14
7
* @typedef {import('hast').Root } HastRoot
15
8
* @typedef {import('hast').Element } HastElement
16
9
* @typedef {import('hast').Content } HastContent
17
10
* @typedef {import('hast').ElementContent } HastElementContent
18
- * @typedef {HastRoot|HastContent } HastNode
19
- * @typedef {Extract<HastNode, import('unist').Parent> } HastParent
20
- *
21
11
* @typedef {import('vfile').VFile } VFile
12
+ */
13
+
14
+ /**
15
+ * @typedef {NlcstRoot | NlcstContent } NlcstNode
16
+ * @typedef {Extract<NlcstNode, import('unist').Parent> } NlcstParent
17
+ * @typedef {HastRoot | HastContent } HastNode
18
+ *
22
19
*
23
20
* @typedef {{
24
21
* tokenizeSentencePlugins: Array<(node: NlcstSentence) => void>,
@@ -81,12 +78,16 @@ const flowAccepting = convertElement([
81
78
const terminalMarker = / ^ ( [ ! . ? \u2026 \u203D ] + ) $ /
82
79
83
80
/**
84
- * Transform ` tree` to nlcst.
81
+ * Turn a hast tree into an nlcst tree .
85
82
*
86
83
* @param {HastNode } tree
84
+ * hast tree to transform.
87
85
* @param {VFile } file
88
- * @param {ParserInstance|ParserConstructor } Parser
86
+ * Virtual file.
87
+ * @param {ParserInstance | ParserConstructor } Parser
88
+ * Parser to use.
89
89
* @returns {NlcstRoot }
90
+ * nlcst tree.
90
91
*/
91
92
export function toNlcst ( tree , file , Parser ) {
92
93
// Warn for invalid parameters.
@@ -122,7 +123,12 @@ export function toNlcst(tree, file, Parser) {
122
123
}
123
124
124
125
/**
126
+ * Transform a hast node.
127
+ *
125
128
* @param {HastNode } node
129
+ * hast node.
130
+ * @returns {void }
131
+ * Nothing.
126
132
*/
127
133
function find ( node ) {
128
134
if ( node . type === 'root' ) {
@@ -132,7 +138,8 @@ export function toNlcst(tree, file, Parser) {
132
138
// Explicit paragraph.
133
139
add ( node )
134
140
} else if ( flowAccepting ( node ) ) {
135
- // Slightly simplified version of: <https://html.spec.whatwg.org/#paragraphs>.
141
+ // Slightly simplified version of:
142
+ // <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>.
136
143
implicit ( flattenAll ( node . children ) )
137
144
} else {
138
145
// Dig deeper.
@@ -142,7 +149,12 @@ export function toNlcst(tree, file, Parser) {
142
149
}
143
150
144
151
/**
152
+ * Transform hast children.
153
+ *
145
154
* @param {Array<HastContent> } children
155
+ * hast children.
156
+ * @returns {void }
157
+ * Nothing.
146
158
*/
147
159
function findAll ( children ) {
148
160
let index = - 1
@@ -153,8 +165,12 @@ export function toNlcst(tree, file, Parser) {
153
165
}
154
166
155
167
/**
168
+ * Flatten hast children: this unravels `a`, `ins`, `del`, and `map` elements.
169
+ *
156
170
* @param {Array<HastElementContent> } children
171
+ * Children.
157
172
* @returns {Array<HastElementContent> }
173
+ * Flattened children.
158
174
*/
159
175
function flattenAll ( children ) {
160
176
/** @type {Array<HastElementContent> } */
@@ -182,7 +198,12 @@ export function toNlcst(tree, file, Parser) {
182
198
}
183
199
184
200
/**
185
- * @param {HastElementContent|Array<HastElementContent> } node
201
+ * Add one or more nodes.
202
+ *
203
+ * @param {HastElementContent | Array<HastElementContent> } node
204
+ * hast node.
205
+ * @returns {void }
206
+ * Nothing.
186
207
*/
187
208
function add ( node ) {
188
209
/** @type {Array<NlcstSentenceContent> | undefined } */
@@ -220,12 +241,19 @@ export function toNlcst(tree, file, Parser) {
220
241
}
221
242
222
243
/**
244
+ * Handle implicit paragraphs.
245
+ *
246
+ * See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>.
247
+ *
223
248
* @param {Array<HastElementContent> } children
249
+ * hast nodes.
250
+ * @returns {void }
251
+ * Nothing.
224
252
*/
225
253
function implicit ( children ) {
226
254
let index = - 1
227
255
let start = - 1
228
- /** @type {boolean| undefined } */
256
+ /** @type {boolean | undefined } */
229
257
let viable
230
258
231
259
while ( ++ index <= children . length ) {
@@ -254,15 +282,17 @@ export function toNlcst(tree, file, Parser) {
254
282
}
255
283
256
284
/**
257
- * Convert `node` ( hast) to nlcst.
285
+ * Turn a hast node into nlcst nodes .
258
286
*
259
287
* @param {HastContent } node
260
- * @returns {Array<NlcstSentenceContent>|undefined }
288
+ * hast node.
289
+ * @returns {Array<NlcstSentenceContent> | undefined }
290
+ * nlcst sentence content.
261
291
*/
262
292
function one ( node ) {
263
- /** @type {Array<NlcstSentenceContent>| undefined } */
293
+ /** @type {Array<NlcstSentenceContent> | undefined } */
264
294
let replacement
265
- /** @type {boolean| undefined } */
295
+ /** @type {boolean | undefined } */
266
296
let change
267
297
268
298
if ( node . type === 'text' ) {
@@ -283,41 +313,52 @@ export function toNlcst(tree, file, Parser) {
283
313
}
284
314
}
285
315
286
- return change && replacement
287
- ? patch ( replacement , loc , loc . toOffset ( pointStart ( node ) ) )
288
- : replacement
316
+ if ( change && replacement ) {
317
+ patch ( replacement , loc , loc . toOffset ( pointStart ( node ) ) )
318
+ }
319
+
320
+ return replacement
289
321
}
290
322
291
323
/**
292
- * Convert all `children` ( hast) to nlcst.
324
+ * Turn hast nodes into nlcst nodes .
293
325
*
294
- * @param {Array<HastContent> } children
295
- * @returns {Array<NlcstSentenceContent> }
326
+ * @param {Array<HastContent> } nodes
327
+ * hast nodes.
328
+ * @returns {Array<NlcstSentenceContent> | undefined }
329
+ * nlcst sentence content.
296
330
*/
297
- function all ( children ) {
331
+ function all ( nodes ) {
298
332
/** @type {Array<NlcstSentenceContent> } */
299
333
const results = [ ]
300
334
let index = - 1
301
335
302
- while ( ++ index < children . length ) {
303
- results . push ( ...( one ( children [ index ] ) || [ ] ) )
336
+ while ( ++ index < nodes . length ) {
337
+ const result = one ( nodes [ index ] )
338
+ if ( result ) {
339
+ results . push ( ...result )
340
+ }
304
341
}
305
342
306
343
return results
307
344
}
308
345
309
346
/**
310
347
* Patch a position on each node in `nodes`.
348
+ *
311
349
* `offset` is the offset in `file` this run of content starts at.
312
350
*
313
351
* Note that nlcst nodes are concrete, meaning that their starting and ending
314
352
* positions can be inferred from their content.
315
353
*
316
- * @template {Array<NlcstContent>} T
317
- * @param { T } nodes
354
+ * @param {Array<NlcstContent> } nodes
355
+ * Nodes to patch.
318
356
* @param {ReturnType<location> } location
357
+ * Location info.
319
358
* @param {number } offset
320
- * @returns {T }
359
+ * Current offset.
360
+ * @returns {void }
361
+ * Nothing.
321
362
*/
322
363
function patch ( nodes , location , offset ) {
323
364
let index = - 1
@@ -339,22 +380,28 @@ export function toNlcst(tree, file, Parser) {
339
380
340
381
start = end
341
382
}
342
-
343
- return nodes
344
383
}
345
384
}
346
385
347
386
/**
387
+ * Check if an element has a `data-nlcst` attribute set to `source`.
388
+ *
348
389
* @param {HastElement } node
390
+ * Element.
349
391
* @returns {boolean }
392
+ * Whether `node` has a `data-nlcst` attribute set to `source`.
350
393
*/
351
394
function dataNlcstSourced ( node ) {
352
395
return Boolean ( node . properties && node . properties . dataNlcst === 'source' )
353
396
}
354
397
355
398
/**
399
+ * Check if an element has a `data-nlcst` attribute set to `ignore`.
400
+ *
356
401
* @param {HastElement } node
402
+ * Element.
357
403
* @returns {boolean }
404
+ * Whether `node` has a `data-nlcst` attribute set to `ignore`.
358
405
*/
359
406
function dataNlcstIgnore ( node ) {
360
407
return Boolean ( node . properties && node . properties . dataNlcst === 'ignore' )
0 commit comments