-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
80 lines (72 loc) · 1.8 KB
/
render.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
package render
import (
"bytes"
_ "embed"
"fmt"
"html/template"
embed "github.com/13rac1/goldmark-embed"
"github.com/Masterminds/sprig"
mathjax "github.com/litao91/goldmark-mathjax"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"go.abhg.dev/goldmark/mermaid"
)
//go:embed marx.min.css
var style string
//go:embed template.gohtml
var templateContent string
func New() *Renderer {
t := template.Must(template.New("").Funcs(sprig.HtmlFuncMap()).Parse(templateContent))
return &Renderer{
page: t,
engine: goldmark.New(goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
highlighting.Highlighting,
&mermaid.Extender{},
mathjax.MathJax,
embed.New(),
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
html.WithHardWraps(),
html.WithXHTML(),
html.WithUnsafe(), // required for <details>
)),
}
}
type Renderer struct {
page *template.Template
engine goldmark.Markdown
}
func (r *Renderer) Render(title, content string, author string, attachments []string) ([]byte, error) {
var out bytes.Buffer
if err := r.engine.Convert([]byte(content), &out); err != nil {
return nil, fmt.Errorf("render: %w", err)
}
vd := &viewData{
Title: title,
Author: author,
Style: template.CSS(style),
HTML: template.HTML(out.String()),
Attachments: attachments,
}
var page bytes.Buffer
err := r.page.Execute(&page, vd)
if err != nil {
return nil, fmt.Errorf("render HTML: %w", err)
}
return page.Bytes(), nil
}
type viewData struct {
Title string
Author string
Attachments []string
Style template.CSS
HTML template.HTML
}