/
content.go
135 lines (101 loc) · 3 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
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
package service
import (
"bytes"
"regexp"
"strings"
"github.com/EmissarySocial/emissary/model"
"github.com/benpate/derp"
"github.com/davidscottmills/goeditorjs"
"github.com/microcosm-cc/bluemonday"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)
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 {
result := model.NewContent()
result.Format = format
result.Raw = raw
service.Format(&result)
return result
}
func (service *Content) Format(content *model.Content) {
const location = "service.Content.Format"
// Convert raw formats into HTML
switch content.Format {
case model.ContentFormatHTML:
content.HTML = content.Raw
case model.ContentFormatEditorJS:
resultHTML, err := service.editorJS.GenerateHTML(content.Raw)
if err != nil {
derp.Report(derp.Wrap(err, location, "Error converting EditorJS to HTML"))
}
content.HTML = resultHTML
case model.ContentFormatMarkdown:
// 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"),
),
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
if err := md.Convert([]byte(content.Raw), &buffer); err != nil {
derp.Report(derp.Wrap(err, location, "Error converting Markdown to HTML"))
}
content.HTML = buffer.String()
default:
content.HTML = ""
}
// Sanitize all HTML, no matter what source format
policy := bluemonday.UGCPolicy()
policy.AllowStyling()
content.HTML = policy.Sanitize(content.HTML)
}
func (service *Content) NewByExtension(extension string, raw string) model.Content {
format := service.FormatByExtension(extension)
return service.New(format, raw)
}
func (service *Content) FormatByExtension(extension string) string {
switch extension {
case "md":
return model.ContentFormatMarkdown
case "json":
return model.ContentFormatEditorJS
default:
return model.ContentFormatHTML
}
}
func (service *Content) ApplyLinks(content *model.Content) {
x := regexp.MustCompile(`https?://[^\s]+`)
newHTML := x.ReplaceAllStringFunc(content.HTML, func(input string) string {
return `<a href="` + string(input) + `" target="_blank">` + string(input) + `</a>`
})
content.HTML = string(newHTML)
}
func (service *Content) ApplyTags(content *model.Content, tags []model.Tag) {
// Last, apply tags back into the Content as links
for _, tag := range tags {
if tag.Href == "" {
continue
}
content.HTML = strings.ReplaceAll(content.HTML, tag.Name, `<a href="`+tag.Href+`" target="_blank">`+tag.Name+`</a>`)
}
}