2022-11-27 10:52:48PM
This commit is contained in:
parent
f37b54ca24
commit
33b7fbaa9e
2 changed files with 12 additions and 2 deletions
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"oss.terrastruct.com/xdefer"
|
||||||
v8 "rogchap.com/v8go"
|
v8 "rogchap.com/v8go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -23,7 +24,8 @@ var mathjaxJS string
|
||||||
|
|
||||||
var svgRe = regexp.MustCompile(`<svg[^>]+width="([0-9\.]+)ex" height="([0-9\.]+)ex"[^>]+>`)
|
var svgRe = regexp.MustCompile(`<svg[^>]+width="([0-9\.]+)ex" height="([0-9\.]+)ex"[^>]+>`)
|
||||||
|
|
||||||
func Render(s string) (string, error) {
|
func Render(s string) (_ string, err error) {
|
||||||
|
defer xdefer.Errorf(&err, "latex failed to parse")
|
||||||
v8ctx := v8.NewContext()
|
v8ctx := v8.NewContext()
|
||||||
|
|
||||||
if _, err := v8ctx.RunScript(polyfillsJS, "polyfills.js"); err != nil {
|
if _, err := v8ctx.RunScript(polyfillsJS, "polyfills.js"); err != nil {
|
||||||
|
|
@ -49,7 +51,8 @@ func Render(s string) (string, error) {
|
||||||
return val.String(), nil
|
return val.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Measure(s string) (width, height int, _ error) {
|
func Measure(s string) (width, height int, err error) {
|
||||||
|
defer xdefer.Errorf(&err, "latex failed to parse")
|
||||||
svg, err := Render(s)
|
svg, err := Render(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
|
|
|
||||||
|
|
@ -21,3 +21,10 @@ func TestRender(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRenderError(t *testing.T) {
|
||||||
|
_, err := Render(`\frac{1}{2}`)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue