Skip to content

Commit

Permalink
migrate latex
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Jan 16, 2025
1 parent 9c19637 commit 397f505
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
9 changes: 9 additions & 0 deletions d2js/js/test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ describe("D2 Unit Tests", () => {
await d2.worker.terminate();
}, 20000);

test("latex works", async () => {
const d2 = new D2();
const result = await d2.compile("x: |latex \\frac{f(x+h)-f(x)}{h} |");
const svg = await d2.render(result.diagram);
expect(svg).toContain("<svg");
expect(svg).toContain("</svg>");
await d2.worker.terminate();
}, 20000);

test("handles syntax errors correctly", async () => {
const d2 = new D2();
try {
Expand Down
18 changes: 10 additions & 8 deletions d2renderers/d2latex/latex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"regexp"
"strconv"

"github.com/dop251/goja"

"oss.terrastruct.com/d2/lib/jsrunner"
"oss.terrastruct.com/util-go/xdefer"
)

Expand All @@ -29,21 +28,24 @@ var svgRe = regexp.MustCompile(`<svg[^>]+width="([0-9\.]+)ex" height="([0-9\.]+)

func Render(s string) (_ string, err error) {
defer xdefer.Errorf(&err, "latex failed to parse")
vm := goja.New()
runner := jsrunner.NewJSRunner()

if _, err := vm.RunString(polyfillsJS); err != nil {
if _, err := runner.RunString(polyfillsJS); err != nil {
return "", err
}

if _, err := vm.RunString(mathjaxJS); err != nil {
return "", err
if _, err := runner.RunString(mathjaxJS); err != nil {
// Known issue that a harmless error occurs in JS: https://github.com/mathjax/MathJax/issues/3289
if runner.Engine() == jsrunner.Goja {
return "", err
}
}

if _, err := vm.RunString(setupJS); err != nil {
if _, err := runner.RunString(setupJS); err != nil {
return "", err
}

val, err := vm.RunString(fmt.Sprintf(`adaptor.innerHTML(html.convert(`+"`"+"%s`"+`, {
val, err := runner.RunString(fmt.Sprintf(`adaptor.innerHTML(html.convert(`+"`"+"%s`"+`, {
em: %d,
ex: %d,
}))`, s, pxPerEx*2, pxPerEx))
Expand Down
5 changes: 5 additions & 0 deletions d2renderers/d2latex/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ const html = MathJax._.mathjax.mathjax.document('', {
InputJax: new MathJax._.input.tex_ts.TeX({ packages: ['base', 'mathtools', 'ams', 'amscd', 'braket', 'cancel', 'cases', 'color', 'gensymb', 'mhchem', 'physics'] }),
OutputJax: new MathJax._.output.svg_ts.SVG(),
});

if (typeof globalThis !== 'undefined') {
globalThis.adaptor = adaptor;
globalThis.html = html;
}
7 changes: 6 additions & 1 deletion lib/jsrunner/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ func (j *jsRunner) MustGet(key string) (JSValue, error) {
return &jsValue{val: result}, nil
}

func (j *jsRunner) RunString(code string) (JSValue, error) {
func (j *jsRunner) RunString(code string) (_ JSValue, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
result := j.global.Call("eval", code)
return &jsValue{val: result}, nil
}
Expand Down

0 comments on commit 397f505

Please sign in to comment.