-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmathjax.go
81 lines (68 loc) · 1.66 KB
/
mathjax.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
81
package mathjax
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)
type mathjax struct {
inlineStartDelim string
inlineEndDelim string
blockStartDelim string
blockEndDelim string
}
type Option interface {
SetOption(e *mathjax)
}
type withInlineDelim struct {
start string
end string
}
type withBlockDelim struct {
start string
end string
}
func WithInlineDelim(start string, end string) Option {
return &withInlineDelim{start, end}
}
func (o *withInlineDelim) SetOption(e *mathjax) {
e.inlineStartDelim = o.start
e.inlineEndDelim = o.end
}
func WithBlockDelim(start string, end string) Option {
return &withBlockDelim{start, end}
}
func (o *withBlockDelim) SetOption(e *mathjax) {
e.blockStartDelim = o.start
e.blockEndDelim = o.end
}
var MathJax = &mathjax{
inlineStartDelim: `\(`,
inlineEndDelim: `\)`,
blockStartDelim: `\[`,
blockEndDelim: `\]`,
}
func NewMathJax(opts ...Option) *mathjax {
r := &mathjax{
inlineStartDelim: `\(`,
inlineEndDelim: `\)`,
blockStartDelim: `\[`,
blockEndDelim: `\]`,
}
for _, o := range opts {
o.SetOption(r)
}
return r
}
func (e *mathjax) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithBlockParsers(
util.Prioritized(NewMathJaxBlockParser(), 701),
))
m.Parser().AddOptions(parser.WithInlineParsers(
util.Prioritized(NewInlineMathParser(), 501),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(NewMathBlockRenderer(e.blockStartDelim, e.blockEndDelim), 501),
util.Prioritized(NewInlineMathRenderer(e.inlineStartDelim, e.inlineEndDelim), 502),
))
}