1
1
/**
2
- * @typedef {import('unist').Node } UnistNode
3
- * @typedef {import('hast').Parent } Parent
4
- * @typedef {import('hast').Root } Root
5
- * @typedef {import('hast').Element } Element
6
- * @typedef {Parent['children'][number] } Child
7
- * @typedef {Element['children'][number] } ElementChild
8
- * @typedef {Child|Root } Node
2
+ * @typedef {import('unist').Point } Point
3
+ *
4
+ * @typedef {import('nlcst').Root } NlcstRoot
5
+ * @typedef {import('nlcst').Paragraph } NlcstParagraph
6
+ * @typedef {import('nlcst').WhiteSpace } NlcstWhiteSpace
7
+ * @typedef {import('nlcst').Source } NlcstSource
8
+ * @typedef {import('nlcst').Content } NlcstContent
9
+ * @typedef {import('nlcst').SentenceContent } NlcstSentenceContent
10
+ * @typedef {NlcstRoot|NlcstContent } NlcstNode
11
+ *
12
+ * @typedef {import('hast').Root } HastRoot
13
+ * @typedef {import('hast').Element } HastElement
14
+ * @typedef {import('hast').Content } HastContent
15
+ * @typedef {import('hast').ElementContent } HastElementContent
16
+ * @typedef {HastRoot|HastContent } HastNode
17
+ * @typedef {Extract<HastNode, import('unist').Parent> } HastParent
18
+ *
9
19
* @typedef {import('vfile').VFile } VFile
10
20
*
11
21
* @typedef {{
12
- * parse(nodes: UnistNode []): UnistNode
13
- * tokenizeSource(value: string): UnistNode
14
- * tokenizeWhiteSpace(value: string): UnistNode
15
- * tokenizeParagraph(nodes: UnistNode []): UnistNode
16
- * tokenize(value: string): UnistNode []
22
+ * parse(nodes: NlcstContent []): NlcstRoot
23
+ * tokenizeSource(value: string): NlcstSource
24
+ * tokenizeWhiteSpace(value: string): NlcstWhiteSpace
25
+ * tokenizeParagraph(nodes: NlcstSentenceContent []): NlcstParagraph
26
+ * tokenize(value: string): NlcstSentenceContent []
17
27
* }} ParserInstance
18
28
* @typedef {new () => ParserInstance } ParserConstructor
19
29
*/
@@ -64,15 +74,13 @@ const flowAccepting = convertElement([
64
74
'dialog'
65
75
] )
66
76
67
- // See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
68
- const unravelInParagraph = convertElement ( [ 'a' , 'ins' , 'del' , 'map' ] )
69
-
70
77
/**
71
78
* Transform `tree` to nlcst.
72
79
*
73
- * @param {Node } tree
80
+ * @param {HastNode } tree
74
81
* @param {VFile } file
75
82
* @param {ParserInstance|ParserConstructor } Parser
83
+ * @returns {NlcstRoot }
76
84
*/
77
85
export function toNlcst ( tree , file , Parser ) {
78
86
// Warn for invalid parameters.
@@ -96,7 +104,7 @@ export function toNlcst(tree, file, Parser) {
96
104
const doc = String ( file )
97
105
const loc = location ( doc )
98
106
const parser = 'parse' in Parser ? Parser : new Parser ( )
99
- /** @type {Array.<UnistNode> } */
107
+ /** @type {NlcstContent[] } */
100
108
const results = [ ]
101
109
102
110
find ( tree )
@@ -108,7 +116,7 @@ export function toNlcst(tree, file, Parser) {
108
116
}
109
117
110
118
/**
111
- * @param {Node } node
119
+ * @param {HastNode } node
112
120
*/
113
121
function find ( node ) {
114
122
if ( node . type === 'root' ) {
@@ -128,7 +136,7 @@ export function toNlcst(tree, file, Parser) {
128
136
}
129
137
130
138
/**
131
- * @param {Array.<Child> } children
139
+ * @param {HastContent[] } children
132
140
*/
133
141
function findAll ( children ) {
134
142
let index = - 1
@@ -139,41 +147,48 @@ export function toNlcst(tree, file, Parser) {
139
147
}
140
148
141
149
/**
142
- * @param {Array.<ElementChild> } children
143
- * @returns {Array.<ElementChild> }
150
+ * @param {HastElementContent[] } children
151
+ * @returns {HastElementContent[] }
144
152
*/
145
153
function flattenAll ( children ) {
146
- /** @type {Array.<ElementChild> } */
154
+ /** @type {HastElementContent[] } */
147
155
const results = [ ]
148
156
let index = - 1
149
157
150
158
while ( ++ index < children . length ) {
151
- if ( unravelInParagraph ( children [ index ] ) ) {
152
- // @ts -ignore Is element.
153
- results . push ( ...flattenAll ( children [ index ] . children ) )
159
+ const child = children [ index ]
160
+
161
+ // See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
162
+ if (
163
+ child . type === 'element' &&
164
+ ( child . tagName === 'a' ||
165
+ child . tagName === 'ins' ||
166
+ child . tagName === 'del' ||
167
+ child . tagName === 'map' )
168
+ ) {
169
+ results . push ( ...flattenAll ( child . children ) )
154
170
} else {
155
- results . push ( children [ index ] )
171
+ results . push ( child )
156
172
}
157
173
}
158
174
159
175
return results
160
176
}
161
177
162
178
/**
163
- * @param {Array.<Node>|Node } node
179
+ * @param {HastElementContent|HastElementContent[] } node
164
180
*/
165
181
function add ( node ) {
166
- /** @type {Array.<UnistNode> } */
167
- // @ts -ignore Assume child.
182
+ /** @type {NlcstSentenceContent[]|undefined } */
168
183
const result = Array . isArray ( node ) ? all ( node ) : one ( node )
169
184
170
- if ( result . length > 0 ) {
185
+ if ( result && result . length > 0 ) {
171
186
results . push ( parser . tokenizeParagraph ( result ) )
172
187
}
173
188
}
174
189
175
190
/**
176
- * @param {Array.<ElementChild> } children
191
+ * @param {HastElementContent[] } children
177
192
*/
178
193
function implicit ( children ) {
179
194
let index = - 1
@@ -209,11 +224,11 @@ export function toNlcst(tree, file, Parser) {
209
224
/**
210
225
* Convert `node` (hast) to nlcst.
211
226
*
212
- * @param {Node } node
213
- * @returns {Array.<UnistNode> |undefined }
227
+ * @param {HastContent } node
228
+ * @returns {NlcstSentenceContent[] |undefined }
214
229
*/
215
230
function one ( node ) {
216
- /** @type {Array.<UnistNode> |undefined } */
231
+ /** @type {NlcstSentenceContent[] |undefined } */
217
232
let replacement
218
233
/** @type {boolean|undefined } */
219
234
let change
@@ -244,11 +259,11 @@ export function toNlcst(tree, file, Parser) {
244
259
/**
245
260
* Convert all `children` (hast) to nlcst.
246
261
*
247
- * @param {Array.<Child> } children
248
- * @returns {Array.<UnistNode> }
262
+ * @param {HastContent[] } children
263
+ * @returns {NlcstSentenceContent[] }
249
264
*/
250
265
function all ( children ) {
251
- /** @type {Array.<UnistNode> } */
266
+ /** @type {NlcstSentenceContent[] } */
252
267
const results = [ ]
253
268
let index = - 1
254
269
@@ -266,7 +281,7 @@ export function toNlcst(tree, file, Parser) {
266
281
* Note that nlcst nodes are concrete, meaning that their starting and ending
267
282
* positions can be inferred from their content.
268
283
*
269
- * @template {Array.<UnistNode> } T
284
+ * @template {NlcstContent[] } T
270
285
* @param {T } nodes
271
286
* @param {ReturnType<location> } location
272
287
* @param {number } offset
@@ -280,7 +295,6 @@ export function toNlcst(tree, file, Parser) {
280
295
const node = nodes [ index ]
281
296
282
297
if ( 'children' in node ) {
283
- // @ts -ignore Looks like a parent.
284
298
patch ( node . children , location , start )
285
299
}
286
300
@@ -299,15 +313,15 @@ export function toNlcst(tree, file, Parser) {
299
313
}
300
314
301
315
/**
302
- * @param {Element } node
316
+ * @param {HastElement } node
303
317
* @returns {boolean }
304
318
*/
305
319
function dataNlcstSourced ( node ) {
306
320
return Boolean ( node . properties && node . properties . dataNlcst === 'source' )
307
321
}
308
322
309
323
/**
310
- * @param {Element } node
324
+ * @param {HastElement } node
311
325
* @returns {boolean }
312
326
*/
313
327
function dataNlcstIgnore ( node ) {
0 commit comments