-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
markdown.go
56 lines (49 loc) · 1.42 KB
/
markdown.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
package renderer
import (
"bytes"
"fmt"
"html/template"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
gmhtml "github.com/yuin/goldmark/renderer/html"
)
var (
unsafeMarkdown = goldmark.New(
goldmark.WithExtensions(
extension.Table,
extension.Strikethrough,
extension.TaskList,
extension.Typographer,
extension.GFM,
emoji.Emoji,
highlighting.NewHighlighting(
// similar to the HTML rendering, we don't care about the style here
highlighting.WithCustomStyle(styles.Fallback),
highlighting.WithFormatOptions(
html.WithClasses(true),
html.WithAllClasses(true),
),
)),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
// this is ✨dangerous✨, but we're passing the output to bluemonday
gmhtml.WithUnsafe(),
),
)
)
func ToMarkdown(fileContent []byte) (template.HTML, error) {
unsafeMarkdownHTML := bytes.NewBuffer(nil)
if err := unsafeMarkdown.Convert(fileContent, unsafeMarkdownHTML); err != nil {
return "", err
}
sanitized := htmlSanitizer.Sanitize(unsafeMarkdownHTML.String())
wrapped := fmt.Sprintf("<div class=\"markdown\">%s</div>", sanitized)
return template.HTML(wrapped), nil
}