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
|
package builder
import (
"encoding/json"
"fmt"
"html/template"
"strings"
)
// PageData is passed to HTML templates when rendering posts.
type PageData struct {
Title string
Content template.HTML
ComponentScripts []string
Date string
Tags []string
Path string
}
type EditorDocument struct {
Version string `json:"version"`
Time int64 `json:"time"`
Blocks []EditorBlock `json:"blocks"`
}
type EditorBlock struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}
// RenderEditorJS converts EditorJS block JSON to HTML and extracts script URLs.
// Returns the rendered HTML body, script URLs, and any error.
func RenderEditorJS(blocksJSON string) (html string, scripts []string, err error) {
if blocksJSON == "" || blocksJSON == "[]" {
return "", nil, nil
}
var doc EditorDocument
if err := json.Unmarshal([]byte(blocksJSON), &doc); err != nil {
return "", nil, err
}
var buf strings.Builder
var scriptURLs []string
for _, block := range doc.Blocks {
blockHTML, blockScripts, err := renderBlock(block)
if err != nil {
return "", nil, err
}
if blockHTML != "" {
buf.WriteString(blockHTML)
}
scriptURLs = append(scriptURLs, blockScripts...)
}
return buf.String(), scriptURLs, nil
}
func renderBlock(block EditorBlock) (html string, scripts []string, err error) {
switch block.Type {
case "paragraph":
return renderParagraph(block.Data)
case "header":
return renderHeader(block.Data)
case "image":
return renderImage(block.Data)
case "list":
return renderList(block.Data)
case "code":
return renderCode(block.Data)
case "quote":
return renderQuote(block.Data)
case "script":
return renderScript(block.Data)
case "component":
return renderComponent(block.Data)
default:
return "", nil, nil
}
}
func renderParagraph(data json.RawMessage) (string, []string, error) {
var d struct {
Text string `json:"text"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if strings.TrimSpace(d.Text) == "" {
return "", nil, nil
}
return fmt.Sprintf("<p>%s</p>\n", template.HTML(d.Text)), nil, nil
}
func renderHeader(data json.RawMessage) (string, []string, error) {
var d struct {
Text string `json:"text"`
Level int `json:"level"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if d.Level < 1 || d.Level > 6 {
d.Level = 2
}
if strings.TrimSpace(d.Text) == "" {
return "", nil, nil
}
return fmt.Sprintf("<h%d>%s</h%d>\n", d.Level, template.HTML(d.Text), d.Level), nil, nil
}
// renderImage renders an EditorJS image block.
// The thumbhash (if present) is placed in data-thumbhash on the <figure> so
// the client can decode it and use it as a blurry placeholder via JS.
func renderImage(data json.RawMessage) (string, []string, error) {
var d struct {
File struct {
URL string `json:"url"`
Thumbhash string `json:"thumbhash"`
} `json:"file"`
Caption string `json:"caption"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if d.File.URL == "" {
return "", nil, nil
}
alt := d.Caption
if alt == "" {
alt = "image"
}
var figAttrs string
if d.File.Thumbhash != "" {
figAttrs = fmt.Sprintf(` data-thumbhash="%s"`, template.HTMLEscapeString(d.File.Thumbhash))
}
var buf strings.Builder
fmt.Fprintf(&buf, "<figure%s>", figAttrs)
fmt.Fprintf(&buf, `<img src="%s" alt="%s" loading="lazy" decoding="async">`,
template.HTMLEscapeString(d.File.URL),
template.HTMLEscapeString(alt),
)
if d.Caption != "" {
fmt.Fprintf(&buf, "<figcaption>%s</figcaption>", template.HTML(d.Caption))
}
buf.WriteString("</figure>\n")
return buf.String(), nil, nil
}
type listItem struct {
Content string `json:"content"`
Items []listItem `json:"items"`
}
func renderList(data json.RawMessage) (string, []string, error) {
var d struct {
Style string `json:"style"`
Items []listItem `json:"items"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if len(d.Items) == 0 {
return "", nil, nil
}
tag := "ul"
if d.Style == "ordered" {
tag = "ol"
}
var buf strings.Builder
renderListItems(&buf, d.Items, tag)
return buf.String(), nil, nil
}
func renderListItems(buf *strings.Builder, items []listItem, tag string) {
fmt.Fprintf(buf, "<%s>\n", tag)
for _, item := range items {
fmt.Fprintf(buf, "<li>%s", template.HTML(item.Content))
if len(item.Items) > 0 {
renderListItems(buf, item.Items, tag)
}
buf.WriteString("</li>\n")
}
fmt.Fprintf(buf, "</%s>\n", tag)
}
func renderCode(data json.RawMessage) (string, []string, error) {
var d struct {
Code string `json:"code"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if strings.TrimSpace(d.Code) == "" {
return "", nil, nil
}
return fmt.Sprintf("<pre><code>%s</code></pre>\n", template.HTMLEscapeString(d.Code)), nil, nil
}
func renderQuote(data json.RawMessage) (string, []string, error) {
var d struct {
Text string `json:"text"`
Caption string `json:"caption"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if strings.TrimSpace(d.Text) == "" {
return "", nil, nil
}
var buf strings.Builder
fmt.Fprintf(&buf, "<blockquote>\n<p>%s</p>\n", template.HTML(d.Text))
if d.Caption != "" {
fmt.Fprintf(&buf, "<cite>%s</cite>\n", template.HTML(d.Caption))
}
buf.WriteString("</blockquote>\n")
return buf.String(), nil, nil
}
func renderScript(data json.RawMessage) (string, []string, error) {
var d struct {
Src string `json:"src"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if d.Src == "" {
return "", nil, nil
}
return "", []string{d.Src}, nil
}
func renderComponent(data json.RawMessage) (string, []string, error) {
var d struct {
Name string `json:"name"`
Props map[string]string `json:"props"`
}
if err := json.Unmarshal(data, &d); err != nil {
return "", nil, err
}
if d.Name == "" {
return "", nil, nil
}
var buf strings.Builder
fmt.Fprintf(&buf, "<%s", template.HTMLEscapeString(d.Name))
for k, v := range d.Props {
fmt.Fprintf(&buf, " %s=\"%s\"", template.HTMLEscapeString(k), template.HTMLEscapeString(v))
}
fmt.Fprintf(&buf, "></%s>\n", template.HTMLEscapeString(d.Name))
return buf.String(), []string{"/assets/components/" + d.Name + ".js"}, nil
}
|