-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtext.ts
60 lines (59 loc) · 1.64 KB
/
text.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
51
52
53
54
55
56
57
58
59
60
import type { SvelteLiteral, SvelteText } from "../../ast"
import type { Context } from "../../context"
import type * as SvAST from "../svelte-ast-types"
/** Convert for Text */
export function convertText(
node: SvAST.Text,
parent: SvelteText["parent"],
ctx: Context,
): SvelteText {
const text: SvelteText = {
type: "SvelteText",
value: node.data,
parent,
...ctx.getConvertLocation(node),
}
let start = node.start
let word = false
for (let index = node.start; index < node.end; index++) {
if (word !== Boolean(ctx.code[index].trim())) {
if (start < index) {
ctx.addToken("HTMLText", { start, end: index })
}
word = !word
start = index
}
}
if (start < node.end) {
ctx.addToken("HTMLText", { start, end: node.end })
}
return text
}
/** Convert for Text to Literal */
export function convertTextToLiteral(
node: SvAST.Text,
parent: SvelteLiteral["parent"],
ctx: Context,
): SvelteLiteral {
const text: SvelteLiteral = {
type: "SvelteLiteral",
value: node.data,
parent,
...ctx.getConvertLocation(node),
}
let start = node.start
let word = false
for (let index = node.start; index < node.end; index++) {
if (word !== Boolean(ctx.code[index].trim())) {
if (start < index) {
ctx.addToken("HTMLText", { start, end: index })
}
word = !word
start = index
}
}
if (start < node.end) {
ctx.addToken("HTMLText", { start, end: node.end })
}
return text
}