-
Notifications
You must be signed in to change notification settings - Fork 13
/
inline_renderer.go
39 lines (34 loc) · 991 Bytes
/
inline_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
package mathjax
import (
"bytes"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
type InlineMathRenderer struct {
startDelim string
endDelim string
}
func (r *InlineMathRenderer) renderInlineMath(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
_, _ = w.WriteString(`<span class="math inline">` + r.startDelim)
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
segment := c.(*ast.Text).Segment
value := segment.Value(source)
if bytes.HasSuffix(value, []byte("\n")) {
w.Write(value[:len(value)-1])
if c != n.LastChild() {
w.Write([]byte(" "))
}
} else {
w.Write(value)
}
}
return ast.WalkSkipChildren, nil
}
_, _ = w.WriteString(r.endDelim + `</span>`)
return ast.WalkContinue, nil
}
func (r *InlineMathRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(KindInlineMath, r.renderInlineMath)
}