2022-11-27 09:17:29AM

This commit is contained in:
Alexander Wang 2022-11-27 09:17:29 -08:00
parent 93adc4f474
commit b90ca0a166
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
6 changed files with 3402 additions and 0 deletions

View file

@ -0,0 +1,43 @@
package d2latex
import (
_ "embed"
"fmt"
v8 "rogchap.com/v8go"
)
//go:embed polyfills.js
var polyfillsJS string
//go:embed setup.js
var setupJS string
//go:embed mathjax.js
var mathjaxJS string
func SVG(s string) (string, error) {
v8ctx := v8.NewContext()
if _, err := v8ctx.RunScript(polyfillsJS, "polyfills.js"); err != nil {
return "", err
}
if _, err := v8ctx.RunScript(mathjaxJS, "mathjax.js"); err != nil {
return "", err
}
if _, err := v8ctx.RunScript(setupJS, "setup.js"); err != nil {
return "", err
}
val, err := v8ctx.RunScript(fmt.Sprintf(`adaptor.innerHTML(html.convert("%s", {
em: 16,
ex: 8,
}))`, s), "value.js")
if err != nil {
return "", err
}
return val.String(), nil
}

View file

@ -0,0 +1,17 @@
package d2latex
import (
"encoding/xml"
"testing"
)
func TestSVG(t *testing.T) {
svg, err := SVG("$$a + b = c$$")
if err != nil {
t.Fatal(err)
}
var xmlParsed interface{}
if err := xml.Unmarshal([]byte(svg), &xmlParsed); err != nil {
t.Fatalf("invalid SVG: %v", err)
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
const adaptor = MathJax._.adaptors.liteAdaptor.liteAdaptor();
MathJax._.handlers.html_ts.RegisterHTMLHandler(adaptor)
const html = MathJax._.mathjax.mathjax.document('', {
InputJax: new MathJax._.input.tex_ts.TeX(),
OutputJax: new MathJax._.output.svg_ts.SVG({ fontCache: "none" }),
});

File diff suppressed because one or more lines are too long