-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrender.go
More file actions
137 lines (119 loc) · 2.9 KB
/
render.go
File metadata and controls
137 lines (119 loc) · 2.9 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"strings"
)
// 渲染block type的json结构
func RenderBlock(n *NodeTree, child string, versionName string, pageUID string) string {
pageInfo := []AttrStringStruct{{
Key: "version",
Value: versionName,
}}
pageInfo = append(pageInfo, AttrStringStruct{
Key: "pageUID",
Value: pageUID,
})
attrWithType := []AttrStringStruct{{
Key: "type",
Value: n.Type,
}}
attrWithData := []AttrInterfaceStruct{{
Key: "data",
Value: n.Data,
}}
attrWithKey := []AttrInterfaceStruct{{
Key: "key",
Value: n.Key,
}}
return H("Block", child, AttrString(pageInfo)+AttrString(attrWithType)+AttrInterface(attrWithData)+AttrInterface(attrWithKey))
}
// 渲染document type的json结构
func RenderDocument(n *NodeTree, child string) string {
attr := []AttrStringStruct{{
Key: "type",
Value: n.Type,
}}
attrWithKey := []AttrInterfaceStruct{{
Key: "key",
Value: n.Key,
}}
return H("Document", child, AttrString(attr)+AttrInterface(attrWithKey))
}
// 渲染inline type的json结构
func RenderInline(n *NodeTree, child string) string {
typeAttr := []AttrStringStruct{{
Key: "type",
Value: n.Type,
}}
attrWithKey := []AttrInterfaceStruct{{
Key: "key",
Value: n.Key,
}}
text := ""
if (n.Data != Data{}) {
attr := []AttrInterfaceStruct{{
Key: "data",
Value: n.Data,
}}
text += H("Inline", child, AttrString(typeAttr)+AttrInterface(attr)+AttrInterface(attrWithKey))
} else {
text += H("Inline", child, AttrString(typeAttr)+AttrInterface(attrWithKey))
}
return text
}
// 渲染mark type的json结构
func RenderMark(n *NodeTree, child string) string {
attrWithKey := []AttrInterfaceStruct{{
Key: "key",
Value: n.Key,
}}
return H("Mark", child, ""+AttrInterface(attrWithKey))
}
// 渲染text type的json结构
func RenderText(n *NodeTree, child string) string {
text := ""
if len(n.Ranges) > 0 {
for i := range n.Ranges {
attrWithKey := []AttrInterfaceStruct{{
Key: "key",
Value: n.Key + string(i),
}}
content := n.Ranges[i].Text
if strings.ContainsAny(content, "{&}&<&>&`&\n") {
content = transfer(content, '{', '}', '<', '>', '`')
}
if len(n.Ranges[i].Marks) > 0 {
attr := []AttrInterfaceStruct{{
Key: "marks",
Value: n.Ranges[i].Marks,
}}
text += H("Text", content, AttrInterface(attr)+AttrInterface(attrWithKey))
} else {
text += H("Text", content, ""+AttrInterface(attrWithKey))
}
}
}
return text
}
// 解析element tag的props部分,
func transfer(s string, char ...byte) string {
for i := 0; i < len(s); i++ {
for _, c := range char {
if s[i] == c {
s = s[:i] + `{"` + string(c) + `"}` + s[i+1:]
i += 4
break
}
if s[i] == '\n' {
if i == len(s)-1 && s != string('\n') {
s = s[:i] + `<span style={{display: "block"}}><br/></span>` + s[i+1:]
return s
} else {
s = s[:i] + "<br/>" + s[i+1:]
i += 4
}
break
}
}
}
return s
}