-
Notifications
You must be signed in to change notification settings - Fork 13
/
inline_tex_renderer.go
46 lines (41 loc) · 1.21 KB
/
inline_tex_renderer.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
package mathjax
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
type InlineTexMathRenderer struct {
texRenderer *TexRenderer
}
func (r *InlineTexMathRenderer) renderInlineMath(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
var buf bytes.Buffer
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
segment := c.(*ast.Text).Segment
value := segment.Value(source)
if bytes.HasSuffix(value, []byte("\n")) {
buf.Write(value[:len(value)-1])
if c != n.LastChild() {
buf.Write([]byte(" "))
}
} else {
buf.Write(value)
}
}
fmt.Println(buf.String())
svg := r.texRenderer.RunInline(buf.String())
_, _ = w.WriteString(`<span class="latex-svg inline">`)
if svg != nil {
_, _ = w.WriteString(`<img alt="" src="data:image/svg+xml;base64, ` + base64.StdEncoding.EncodeToString(svg) + `">`)
}
return ast.WalkSkipChildren, nil
}
_, _ = w.WriteString(`</span>`)
return ast.WalkContinue, nil
}
func (r *InlineTexMathRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(KindInlineMath, r.renderInlineMath)
}