-
Notifications
You must be signed in to change notification settings - Fork 14
/
content.go
75 lines (59 loc) · 1.41 KB
/
content.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package service
import (
"bytes"
"github.com/EmissarySocial/emissary/model"
"github.com/benpate/derp"
"github.com/davidscottmills/goeditorjs"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
)
type Content struct {
editorJS *goeditorjs.HTMLEngine
}
func NewContent(editorJS *goeditorjs.HTMLEngine) Content {
return Content{
editorJS: editorJS,
}
}
func (service *Content) New(format string, raw string) model.Content {
var err error
var html string
// Convert raw formats into HTML
switch format {
case "EDITORJS":
html, err = service.editorJS.GenerateHTML(raw)
if err != nil {
derp.Report(err)
}
case "HTML":
html = raw
case "MARKDOWN":
// TODO: Enable markdown plugins (tables, etc)
// https://github.com/yuin/goldmark#built-in-extensions
var buffer bytes.Buffer
md := goldmark.New(
goldmark.WithExtensions(
extension.Table,
extension.Linkify,
extension.Typographer,
extension.DefinitionList,
highlighting.NewHighlighting(
highlighting.WithStyle("github"),
),
),
)
if err := md.Convert([]byte(raw), &buffer); err != nil {
derp.Report(err)
}
html = buffer.String()
}
// Sanitize all HTML, no matter what source format
// html = bluemonday.UGCPolicy().Sanitize(html)
// Create the result object
return model.Content{
Format: format,
Raw: raw,
HTML: html,
}
}