2022-11-27 10:52:48PM

This commit is contained in:
Alexander Wang 2022-11-27 22:52:48 -08:00
parent f37b54ca24
commit 33b7fbaa9e
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
2 changed files with 12 additions and 2 deletions

View file

@ -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

View file

@ -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)
}
}