-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all.ts
executable file
·50 lines (43 loc) · 1.24 KB
/
all.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { one } from './one.js'
import { J, TexastContent, Node, Parent, Handle } from './types.js'
/**
* Convert all nodes in tree using j
* @param j jast constructor function
* @param parent
* @returns
*/
export function all(j: J, parent: Node): Array<TexastContent> {
// @ts-expect-error Assume `parent` is a parent.
const nodes: Array<Node> = parent.children || []
const values: Array<TexastContent> = []
let index = -1
const length = nodes.length
// let child = nodes[index + 1]
// Trim initial and final `<br>`s.
// They’re not semantic per HTML, and they can’t be made in markdown things
// like paragraphs or headings.
// while (child && child.type === 'element' && child.name === 'br') {
// index++
// child = nodes[index + 1]
// }
// child = nodes[length - 1]
// while (
// length - 1 > index &&
// child &&
// child.type === 'element' &&
// child.name === 'br'
// ) {
// length--
// child = nodes[length - 1]
// }
while (++index < length) {
// @ts-expect-error assume `parent` is a parent.
const result = one(j, nodes[index], parent)
if (Array.isArray(result)) {
values.push(...result)
} else if (result) {
values.push(result)
}
}
return values
}