-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathblocknote.go
More file actions
396 lines (343 loc) · 10.7 KB
/
blocknote.go
File metadata and controls
396 lines (343 loc) · 10.7 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package malak
import (
"fmt"
"html"
"strings"
"github.com/google/uuid"
"github.com/microcosm-cc/bluemonday"
)
type Block struct {
ID string `json:"id"`
Type string `json:"type"`
Props map[string]interface{} `json:"props"`
Content interface{} `json:"content"`
Children []Block `json:"children"`
}
type DefaultProps struct {
BackgroundColor string `json:"backgroundColor"`
TextColor string `json:"textColor"`
TextAlignment string `json:"textAlignment"`
}
type ParagraphBlock struct {
Block
Content []InlineContent `json:"content"`
}
type HeadingBlock struct {
Block
Props struct {
DefaultProps
Level int `json:"level"`
} `json:"props"`
Content []InlineContent `json:"content"`
}
type BulletListItemBlock struct {
Block
Content []InlineContent `json:"content"`
}
type NumberedListItemBlock struct {
Block
Content []InlineContent `json:"content"`
}
type ImageBlock struct {
Block
Props struct {
DefaultProps
URL string `json:"url"`
Caption string `json:"caption"`
PreviewWidth int `json:"previewWidth"`
} `json:"props"`
}
type AlertBlock struct {
Block
Props struct {
DefaultProps
Type string `json:"type"` // warning, error, info, success
} `json:"props"`
Content []InlineContent `json:"content"`
}
type DashboardBlock struct {
Block
Props struct {
DefaultProps
SelectedItem string `json:"selectedItem"`
} `json:"props"`
Content []InlineContent `json:"content"`
}
type ChartBlock struct {
Block
Props struct {
DefaultProps
ChartType string `json:"chartType"`
Data interface{} `json:"data"`
SelectedChart string `json:"selectedChart"` // Integration chart ID
} `json:"props"`
Content []InlineContent `json:"content"`
}
type TableBlock struct {
Block
Content TableContent `json:"content"`
}
type TableContent struct {
Type string `json:"type"`
Rows []struct {
Cells [][]InlineContent `json:"cells"`
} `json:"rows"`
}
type InlineContent interface {
inlineContent()
}
type StyledText struct {
Type string `json:"type"`
Text string `json:"text"`
Styles Styles `json:"styles"`
}
func (StyledText) inlineContent() {}
type Link struct {
Type string `json:"type"`
Content []StyledText `json:"content"`
Href string `json:"href"`
}
func (Link) inlineContent() {}
type Styles struct {
Bold bool `json:"bold"`
Italic bool `json:"italic"`
Underline bool `json:"underline"`
Strikethrough bool `json:"strikethrough"`
TextColor string `json:"textColor"`
BackgroundColor string `json:"backgroundColor"`
}
type BlockContents []Block
func (bc BlockContents) HTML(workspaceID uuid.UUID, renderer ChartRenderer) string {
var html strings.Builder
for _, block := range bc {
html.WriteString(convertBlockToHTML(block, workspaceID, renderer))
}
return html.String()
}
func convertBlockToHTML(block Block, workspaceID uuid.UUID, renderer ChartRenderer) string {
var s strings.Builder
style := getStyleString(block.Props)
switch block.Type {
case "heading":
level, _ := block.Props["level"].(float64)
content := getSimpleContent(block.Content)
s.WriteString(fmt.Sprintf("<h%d style='%s'>%s</h%d>", int(level), style, content, int(level)))
case "paragraph":
extra := []string{
"font-size:100%;",
"line-height: 24px;",
"margin: 16px;",
}
content := getSimpleContent(block.Content)
s.WriteString(fmt.Sprintf("<p style='%s'>%s</p>", strings.Join(append(extra, style), " "), content))
case "alert":
alertType, _ := block.Props["type"].(string)
content := getSimpleContent(block.Content)
s.WriteString(fmt.Sprintf(`<div class="alert" data-alert-type="%s">%s</div>`, alertType, content))
case "dashboard":
selectedItem, _ := block.Props["selectedItem"].(string)
content := getSimpleContent(block.Content)
s.WriteString(fmt.Sprintf(`<div class="dashboard" data-selected-item="%s">%s</div>`, selectedItem, content))
case "chart":
selectedChart, _ := block.Props["selectedChart"].(string)
if selectedChart == "" {
s.WriteString(`<div class="chart-error">No chart selected</div>`)
return s.String()
}
chartKey, err := renderer.RenderChart(workspaceID, selectedChart)
if err != nil {
s.WriteString(fmt.Sprintf(`<div class="chart-error">Failed to render chart: %s</div>`, err.Error()))
} else {
s.WriteString(fmt.Sprintf(`<a href='%s' target="_blank"><img src='%s' alt='%s' style="display: block; width: %s; max-width: 600px; height: auto;"></a>`, chartKey, chartKey, "Chart image", "100%"))
}
case "numberedListItem", "bulletListItem":
content := getSimpleContent(block.Content)
s.WriteString(fmt.Sprintf("<li style='%s'>%s</li>", style, content))
case "checkListItem":
checked := ""
if isChecked, ok := block.Props["checked"].(bool); ok && isChecked {
checked = " checked"
}
content := getSimpleContent(block.Content)
s.WriteString(fmt.Sprintf("<li style='%s'><input type='checkbox'%s>%s</li>", style, checked, content))
case "image":
url, _ := block.Props["url"].(string)
name, _ := block.Props["name"].(string)
s.WriteString(fmt.Sprintf(`<a href='%s' target="_blank"><img src='%s' alt='%s' style="display: block; width: %s; max-width: 600px; height: auto;"></a>`, url, url, name, "100%"))
_ = style
if caption, ok := block.Props["caption"].(string); ok && caption != "" {
s.WriteString(fmt.Sprintf("<figcaption>%s</figcaption>", html.EscapeString(caption)))
}
default:
}
if len(block.Children) > 0 {
for _, child := range block.Children {
s.WriteString(convertBlockToHTML(child, workspaceID, renderer))
}
}
return s.String()
}
func getSimpleContent(content interface{}) string {
var result strings.Builder
switch v := content.(type) {
case []interface{}:
for _, item := range v {
if m, ok := item.(map[string]interface{}); ok {
text, _ := m["text"].(string)
styles, _ := m["styles"].(map[string]interface{})
result.WriteString(applyInlineStyles(text, styles))
}
}
case string:
result.WriteString(html.EscapeString(v))
default:
}
return result.String()
}
func applyInlineStyles(text string, styles map[string]interface{}) string {
if len(styles) == 0 {
return html.EscapeString(text)
}
var styleStrings []string
if bold, ok := styles["bold"].(bool); ok && bold {
styleStrings = append(styleStrings, "font-weight: bold;")
}
if italic, ok := styles["italic"].(bool); ok && italic {
styleStrings = append(styleStrings, "font-style: italic;")
}
if underline, ok := styles["underline"].(bool); ok && underline {
styleStrings = append(styleStrings, "text-decoration: underline;")
}
if strikethrough, ok := styles["strikethrough"].(bool); ok && strikethrough {
styleStrings = append(styleStrings, "text-decoration: line-through;")
}
if textColor, ok := styles["textColor"].(string); ok && textColor != "" {
styleStrings = append(styleStrings, fmt.Sprintf("color: %s;", textColor))
}
if backgroundColor, ok := styles["backgroundColor"].(string); ok && backgroundColor != "" {
styleStrings = append(styleStrings, fmt.Sprintf("background-color: %s;", backgroundColor))
}
if len(styleStrings) > 0 {
return fmt.Sprintf("<span style='%s'>%s</span>", strings.Join(styleStrings, " "), html.EscapeString(text))
}
return html.EscapeString(text)
}
func getStyleString(props map[string]interface{}) string {
var styles []string
if textColor, ok := props["textColor"].(string); ok && textColor != "default" {
styles = append(styles, fmt.Sprintf("color:%s;", textColor))
}
if bgColor, ok := props["backgroundColor"].(string); ok && bgColor != "default" {
styles = append(styles, fmt.Sprintf("background-color:%s;", bgColor))
}
if textAlign, ok := props["textAlignment"].(string); ok && textAlign != "left" {
styles = append(styles, fmt.Sprintf("text-align:%s;", textAlign))
}
return strings.Join(styles, " ")
}
func SanitizeBlocks(blocks BlockContents) (BlockContents, error) {
policy := bluemonday.UGCPolicy()
for i := range blocks {
sanitizeBlock(&blocks[i], policy)
}
return blocks, nil
}
func sanitizeBlock(block *Block, policy *bluemonday.Policy) {
// Sanitize Props
for key, value := range block.Props {
if strValue, ok := value.(string); ok {
block.Props[key] = policy.Sanitize(strValue)
}
}
// Sanitize Content based on block type
switch block.Type {
case "alert":
if alertType, ok := block.Props["type"].(string); ok {
// Only allow valid alert types
validTypes := map[string]bool{
"warning": true,
"error": true,
"info": true,
"success": true,
}
if !validTypes[alertType] {
block.Props["type"] = "warning" // default to warning if invalid
}
}
fallthrough // continue to sanitize content
case "chart":
if chartType, ok := block.Props["chartType"].(string); ok {
// Sanitize chart type
validTypes := map[string]bool{
"bar": true,
"line": true,
"pie": true,
"area": true,
}
if !validTypes[chartType] {
block.Props["chartType"] = "bar" // default to bar if invalid
}
}
fallthrough // continue to sanitize content
case "dashboard":
if content, ok := block.Content.([]interface{}); ok {
for i := range content {
if item, ok := content[i].(map[string]interface{}); ok {
if text, ok := item["text"].(string); ok {
item["text"] = policy.Sanitize(text)
}
if styles, ok := item["styles"].(map[string]interface{}); ok {
for key, value := range styles {
if strValue, ok := value.(string); ok {
styles[key] = policy.Sanitize(strValue)
}
}
}
}
}
}
default:
switch content := block.Content.(type) {
case []InlineContent:
for j := range content {
sanitizeInlineContent(content[j], policy)
}
case TableContent:
sanitizeTableContent(&content, policy)
block.Content = content
}
}
// Recursively sanitize children
for i := range block.Children {
sanitizeBlock(&block.Children[i], policy)
}
}
func sanitizeInlineContent(content InlineContent, policy *bluemonday.Policy) {
switch v := content.(type) {
case *StyledText:
v.Text = policy.Sanitize(v.Text)
sanitizeStyles(&v.Styles, policy)
case *Link:
v.Href = policy.Sanitize(v.Href)
for i := range v.Content {
v.Content[i].Text = policy.Sanitize(v.Content[i].Text)
sanitizeStyles(&v.Content[i].Styles, policy)
}
}
}
func sanitizeStyles(styles *Styles, policy *bluemonday.Policy) {
styles.TextColor = policy.Sanitize(styles.TextColor)
styles.BackgroundColor = policy.Sanitize(styles.BackgroundColor)
}
func sanitizeTableContent(table *TableContent, policy *bluemonday.Policy) {
for i := range table.Rows {
for j := range table.Rows[i].Cells {
for k := range table.Rows[i].Cells[j] {
sanitizeInlineContent(table.Rows[i].Cells[j][k], policy)
}
}
}
}
type ChartRenderer interface {
RenderChart(workspaceID uuid.UUID, chartID string) (string, error)
}