-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformText.ts
35 lines (33 loc) ยท 980 Bytes
/
transformText.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
import { NodeTypes } from '../ast'
import { isText } from '../utils'
export function transformText(node) {
if (node.type === NodeTypes.ELEMENT) {
return () => {
const { children } = node
let currentContainer
for (let i = 0; i < children.length; i++) {
const child = children[i]
if (isText(child)) {
for (let j = i + 1; j < children.length; j++) {
const next = children[j]
if (isText(next)) {
if (!currentContainer) {
currentContainer = children[i] = {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [child],
}
}
currentContainer.children.push('+ ')
currentContainer.children.push(next)
children.splice(j, 1)
j--
} else {
currentContainer = undefined
break
}
}
}
}
}
}
}