11/**
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+ *
919 * @typedef {import('vfile').VFile } VFile
1020 *
1121 * @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 []
1727 * }} ParserInstance
1828 * @typedef {new () => ParserInstance } ParserConstructor
1929 */
@@ -64,15 +74,13 @@ const flowAccepting = convertElement([
6474 'dialog'
6575] )
6676
67- // See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
68- const unravelInParagraph = convertElement ( [ 'a' , 'ins' , 'del' , 'map' ] )
69-
7077/**
7178 * Transform `tree` to nlcst.
7279 *
73- * @param {Node } tree
80+ * @param {HastNode } tree
7481 * @param {VFile } file
7582 * @param {ParserInstance|ParserConstructor } Parser
83+ * @returns {NlcstRoot }
7684 */
7785export function toNlcst ( tree , file , Parser ) {
7886 // Warn for invalid parameters.
@@ -96,7 +104,7 @@ export function toNlcst(tree, file, Parser) {
96104 const doc = String ( file )
97105 const loc = location ( doc )
98106 const parser = 'parse' in Parser ? Parser : new Parser ( )
99- /** @type {Array.<UnistNode> } */
107+ /** @type {NlcstContent[] } */
100108 const results = [ ]
101109
102110 find ( tree )
@@ -108,7 +116,7 @@ export function toNlcst(tree, file, Parser) {
108116 }
109117
110118 /**
111- * @param {Node } node
119+ * @param {HastNode } node
112120 */
113121 function find ( node ) {
114122 if ( node . type === 'root' ) {
@@ -128,7 +136,7 @@ export function toNlcst(tree, file, Parser) {
128136 }
129137
130138 /**
131- * @param {Array.<Child> } children
139+ * @param {HastContent[] } children
132140 */
133141 function findAll ( children ) {
134142 let index = - 1
@@ -139,41 +147,48 @@ export function toNlcst(tree, file, Parser) {
139147 }
140148
141149 /**
142- * @param {Array.<ElementChild> } children
143- * @returns {Array.<ElementChild> }
150+ * @param {HastElementContent[] } children
151+ * @returns {HastElementContent[] }
144152 */
145153 function flattenAll ( children ) {
146- /** @type {Array.<ElementChild> } */
154+ /** @type {HastElementContent[] } */
147155 const results = [ ]
148156 let index = - 1
149157
150158 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 ) )
154170 } else {
155- results . push ( children [ index ] )
171+ results . push ( child )
156172 }
157173 }
158174
159175 return results
160176 }
161177
162178 /**
163- * @param {Array.<Node>|Node } node
179+ * @param {HastElementContent|HastElementContent[] } node
164180 */
165181 function add ( node ) {
166- /** @type {Array.<UnistNode> } */
167- // @ts -ignore Assume child.
182+ /** @type {NlcstSentenceContent[]|undefined } */
168183 const result = Array . isArray ( node ) ? all ( node ) : one ( node )
169184
170- if ( result . length > 0 ) {
185+ if ( result && result . length > 0 ) {
171186 results . push ( parser . tokenizeParagraph ( result ) )
172187 }
173188 }
174189
175190 /**
176- * @param {Array.<ElementChild> } children
191+ * @param {HastElementContent[] } children
177192 */
178193 function implicit ( children ) {
179194 let index = - 1
@@ -209,11 +224,11 @@ export function toNlcst(tree, file, Parser) {
209224 /**
210225 * Convert `node` (hast) to nlcst.
211226 *
212- * @param {Node } node
213- * @returns {Array.<UnistNode> |undefined }
227+ * @param {HastContent } node
228+ * @returns {NlcstSentenceContent[] |undefined }
214229 */
215230 function one ( node ) {
216- /** @type {Array.<UnistNode> |undefined } */
231+ /** @type {NlcstSentenceContent[] |undefined } */
217232 let replacement
218233 /** @type {boolean|undefined } */
219234 let change
@@ -244,11 +259,11 @@ export function toNlcst(tree, file, Parser) {
244259 /**
245260 * Convert all `children` (hast) to nlcst.
246261 *
247- * @param {Array.<Child> } children
248- * @returns {Array.<UnistNode> }
262+ * @param {HastContent[] } children
263+ * @returns {NlcstSentenceContent[] }
249264 */
250265 function all ( children ) {
251- /** @type {Array.<UnistNode> } */
266+ /** @type {NlcstSentenceContent[] } */
252267 const results = [ ]
253268 let index = - 1
254269
@@ -266,7 +281,7 @@ export function toNlcst(tree, file, Parser) {
266281 * Note that nlcst nodes are concrete, meaning that their starting and ending
267282 * positions can be inferred from their content.
268283 *
269- * @template {Array.<UnistNode> } T
284+ * @template {NlcstContent[] } T
270285 * @param {T } nodes
271286 * @param {ReturnType<location> } location
272287 * @param {number } offset
@@ -280,7 +295,6 @@ export function toNlcst(tree, file, Parser) {
280295 const node = nodes [ index ]
281296
282297 if ( 'children' in node ) {
283- // @ts -ignore Looks like a parent.
284298 patch ( node . children , location , start )
285299 }
286300
@@ -299,15 +313,15 @@ export function toNlcst(tree, file, Parser) {
299313}
300314
301315/**
302- * @param {Element } node
316+ * @param {HastElement } node
303317 * @returns {boolean }
304318 */
305319function dataNlcstSourced ( node ) {
306320 return Boolean ( node . properties && node . properties . dataNlcst === 'source' )
307321}
308322
309323/**
310- * @param {Element } node
324+ * @param {HastElement } node
311325 * @returns {boolean }
312326 */
313327function dataNlcstIgnore ( node ) {
0 commit comments