-
Notifications
You must be signed in to change notification settings - Fork 3
/
html.go
51 lines (44 loc) · 1.43 KB
/
html.go
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
package goeditorjs
import (
"fmt"
)
// HTMLEngine is the engine that creates the HTML from EditorJS blocks
type HTMLEngine struct {
BlockHandlers map[string]HTMLBlockHandler
}
// HTMLBlockHandler is an interface for a plugable EditorJS HTML generator
type HTMLBlockHandler interface {
Type() string // Type returns the type the block handler supports as a string
GenerateHTML(editorJSBlock EditorJSBlock) (string, error)
}
// NewHTMLEngine creates a new HTMLEngine
func NewHTMLEngine() *HTMLEngine {
bhs := make(map[string]HTMLBlockHandler)
return &HTMLEngine{BlockHandlers: bhs}
}
// RegisterBlockHandlers registers or overrides a block handlers for blockType given by HTMLBlockHandler.Type()
func (htmlEngine *HTMLEngine) RegisterBlockHandlers(handlers ...HTMLBlockHandler) {
for _, bh := range handlers {
htmlEngine.BlockHandlers[bh.Type()] = bh
}
}
// GenerateHTML generates html from the editorJS using configured set of HTML handlers
func (htmlEngine *HTMLEngine) GenerateHTML(editorJSData string) (string, error) {
result := ""
ejs, err := parseEditorJSON(editorJSData)
if err != nil {
return "", err
}
for _, block := range ejs.Blocks {
if generator, ok := htmlEngine.BlockHandlers[block.Type]; ok {
html, err := generator.GenerateHTML(block)
if err != nil {
return result, err
}
result += html
} else {
return "", fmt.Errorf("%w, Block Type: %s", ErrBlockHandlerNotFound, block.Type)
}
}
return result, nil
}