forked from litao91/goldmark-mathjax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_renderer.go
39 lines (33 loc) · 1.01 KB
/
block_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 (
gast "github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
type MathBlockRenderer struct {
startDelim string
endDelim string
}
func NewMathBlockRenderer(start, end string) renderer.NodeRenderer {
return &MathBlockRenderer{start, end}
}
func (r *MathBlockRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(KindMathBlock, r.renderMathBlock)
}
func (r *MathBlockRenderer) writeLines(w util.BufWriter, source []byte, n gast.Node) {
l := n.Lines().Len()
for i := 0; i < l; i++ {
line := n.Lines().At(i)
w.Write(line.Value(source))
}
}
func (r *MathBlockRenderer) renderMathBlock(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
n := node.(*MathBlock)
if entering {
_, _ = w.WriteString(`<p><span class="math display">` + r.startDelim)
r.writeLines(w, source, n)
} else {
_, _ = w.WriteString(r.endDelim + `</span></p>` + "\n")
}
return gast.WalkContinue, nil
}