diff --git a/.gitattributes b/.gitattributes index 6b5766b0c8..72400003ff 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,5 +3,3 @@ go.mod linguist-generated go.sum linguist-generated d2renderers/d2svg/github-markdown.css linguist-vendored -d2renderers/d2latex/mathjax.js linguist-vendored -d2renderers/d2latex/polyfills.js linguist-vendored diff --git a/.prettierignore b/.prettierignore index d87b1d9297..30ef852776 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,5 +1,2 @@ -d2renderers/d2latex/mathjax.js -d2renderers/d2latex/polyfills.js -d2renderers/d2latex/setup.js lib/png/generate_png.js d2js diff --git a/README.md b/README.md index 07681ca701..28b35e6fc6 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,9 @@ you like and works best for the diagram you're making. Sketch-mode rendering uses [rough-go](https://github.com/d2lang/rough-go), a native Go port of Rough.js 4.0.4. +LaTeX labels use [mathjax-go](https://github.com/d2lang/mathjax-go), a native Go port of +the MathJax 3.2.2 TeX-to-SVG pipeline. + ## Comparison For a comparison against other popular text-to-diagram tools, see diff --git a/THIRD_PARTY_NOTICES.txt b/THIRD_PARTY_NOTICES.txt index cac36eb9aa..a4e00c69aa 100644 --- a/THIRD_PARTY_NOTICES.txt +++ b/THIRD_PARTY_NOTICES.txt @@ -69,6 +69,59 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +mathjax-go +https://github.com/d2lang/mathjax-go +Copyright 2026 D2 contributors + +mathjax-go is a native Go translation and modification of the TeX-to-SVG +pipeline used by D2's custom MathJax 3.2.2 component. The port is based on: + + MathJax 3.2.2 + https://github.com/mathjax/MathJax-src + commit ad8f5c21cb810236551da8c6512ba733e67357ee + Copyright (c) 2009-2022 The MathJax Consortium + +The translated TeX color support retains its additional upstream attribution: + + Copyright (c) 2018-2022 Omar Al-Ithawi and The MathJax Consortium + + mhchemParser 4.1.1 + https://github.com/mhchem/mhchemParser + Copyright (c) 2015-2021 Martin Hensel + +MathJax, mhchemParser, and mathjax-go are made available under the Apache +License 2.0. The complete Apache License 2.0 text appears below. + +Portions of mathjax-go's mhchem implementation adapt the initial Go translation +structure from: + + Luo-Studio/go-tex + https://github.com/Luo-Studio/go-tex + commit 60635a3613c51e710fd5cb57e1d7eeb7b3163a1f + +MIT License + +Copyright (c) 2026 luo-studio +Copyright (c) erweixin (upstream RaTeX) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + elk-go https://github.com/d2lang/elk-go diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 6522546130..c43060a032 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -7,6 +7,7 @@ - d2dagre: replace the embedded JavaScript runtime with the native Go Dagro port without changing layout output - d2sketch: replace the embedded Rough.js runtime with the native Go rough-go port without changing sketch output - d2elk: replace the embedded JavaScript runtime with the native Go elk-go port, preserving layouts apart from sub-pixel floating-point rounding +- d2latex: replace the embedded MathJax JavaScript runtime with the native Go mathjax-go port without changing rendered formulas - d2ascii: - sql_table and uml class shapes are supported [#2623](https://github.com/d2lang/d2/pull/2623) - newlines are handled [#2626](https://github.com/d2lang/d2/pull/2626) diff --git a/d2js/js/THIRD_PARTY_NOTICES.txt b/d2js/js/THIRD_PARTY_NOTICES.txt index cac36eb9aa..a4e00c69aa 100644 --- a/d2js/js/THIRD_PARTY_NOTICES.txt +++ b/d2js/js/THIRD_PARTY_NOTICES.txt @@ -69,6 +69,59 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +mathjax-go +https://github.com/d2lang/mathjax-go +Copyright 2026 D2 contributors + +mathjax-go is a native Go translation and modification of the TeX-to-SVG +pipeline used by D2's custom MathJax 3.2.2 component. The port is based on: + + MathJax 3.2.2 + https://github.com/mathjax/MathJax-src + commit ad8f5c21cb810236551da8c6512ba733e67357ee + Copyright (c) 2009-2022 The MathJax Consortium + +The translated TeX color support retains its additional upstream attribution: + + Copyright (c) 2018-2022 Omar Al-Ithawi and The MathJax Consortium + + mhchemParser 4.1.1 + https://github.com/mhchem/mhchemParser + Copyright (c) 2015-2021 Martin Hensel + +MathJax, mhchemParser, and mathjax-go are made available under the Apache +License 2.0. The complete Apache License 2.0 text appears below. + +Portions of mathjax-go's mhchem implementation adapt the initial Go translation +structure from: + + Luo-Studio/go-tex + https://github.com/Luo-Studio/go-tex + commit 60635a3613c51e710fd5cb57e1d7eeb7b3163a1f + +MIT License + +Copyright (c) 2026 luo-studio +Copyright (c) erweixin (upstream RaTeX) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + elk-go https://github.com/d2lang/elk-go diff --git a/d2renderers/d2latex/latex_common.go b/d2renderers/d2latex/latex_common.go index aba5270c4a..949caef26b 100644 --- a/d2renderers/d2latex/latex_common.go +++ b/d2renderers/d2latex/latex_common.go @@ -1,90 +1,16 @@ package d2latex import ( - "fmt" - "math" - "regexp" - "strconv" - "strings" - - "github.com/d2lang/d2/lib/jsrunner" + mathjax "github.com/d2lang/mathjax-go" "github.com/d2lang/util-go/xdefer" ) -var pxPerEx = 8 - -// Matches this -//