-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
57 lines (48 loc) · 1.29 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
package pikchr
import (
"bytes"
"fmt"
"github.com/gebv/pikchr"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
// Renderer renders pikchr diagrams as HTML, to be rendered into images
// client side.
type Renderer struct {
DarkMode bool
}
// RegisterFuncs registers the renderer for pikchr blocks with the provided
// Goldmark Registerer.
func (r *Renderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(Kind, r.Render)
}
// Render renders pikchr.Block nodes.
func (r *Renderer) Render(w util.BufWriter, src []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*Block)
if !entering {
raw := getLines(src, n)
opt := []pikchr.Option{}
opt = append(opt, pikchr.HTMLError())
if r.DarkMode {
opt = append(opt, pikchr.Dark())
}
res, _ := pikchr.Render(
string(raw),
opt...,
)
_, _ = fmt.Fprintf(w, `<div class="pikchr-svg" style="max-width:%dpx">`, res.Width)
_, _ = w.Write([]byte(res.Data))
_, _ = w.WriteString("</div>")
}
return ast.WalkContinue, nil
}
func getLines(source []byte, n ast.Node) []byte {
buf := bytes.NewBuffer([]byte{})
lines := n.Lines()
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
buf.Write(line.Value(source))
}
return buf.Bytes()
}