gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.61k stars 7.52k forks source link

Improve error message thrown by transform.ToMath #12990

Open jmooring opened 3 days ago

jmooring commented 3 days ago

markdown (note the erroneous double-escape before "frac"):

\[\\frac 1 2\]

executing "_default/_markup/render-passthrough.html" at <_html_template_htmlescaper>: error calling _html_template_htmlescaper: not a function

This works fine:

\[\frac 1 2\]

Another example that triggers the error:

$$I = \frac{ 12 \text{V} }{ 4 \text{Ω} }$$

In this case, the text{Ω} bit is the the problem. This works fine:

$$I = \frac{ 12 \text{V} }{ 4 \Omega }$$

Note that the KaTeX engine renders both of the above without error.

\[\\frac 1 2\]

$$I = \frac{ 12 \text{V} }{ 4 \text{Ω} }$$

image

test case (the last three assertions need some work) ```go func TestFoo(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- disableKinds = ['home','rss','section','sitemap','taxonomy','term'] [markup.goldmark.extensions.passthrough] enable = true [markup.goldmark.extensions.passthrough.delimiters] block = [['\[', '\]']] inline = [['\(', '\)']] -- layouts/_default/single.html -- {{ .Content }} -- layouts/_default/_markup/render-passthrough.html -- {{- transform.ToMath .Inner (dict "displayMode" (eq .Type "block")) -}} -- content/p1.md -- --- title: p1 --- \(\\frac 1 2\) -- content/p2.md -- --- title: p2 --- \[\\frac 1 2\] -- content/p3.md -- --- title: p3 --- \(I = \frac{ 12 \text{V} }{ 4 \text{Ω} }\) -- content/p43.md -- --- title: p4 --- \[I = \frac{ 12 \text{V} }{ 4 \text{Ω} }\] ` b := hugolib.Test(t, files) b.AssertFileContent("public/p1/index.html", `

frac12\\frac 1 2

`) b.AssertFileContent("public/p2/index.html", `TBD`) b.AssertFileContent("public/p3/index.html", `TBD`) b.AssertFileContent("public/p4/index.html", `TBD`) } ```
bep commented 3 days ago

{{- transform.ToMath .Inner (dict "displayMode" (eq .Type "block")) -}}

We should fix this, but you can do:

{{ $result := transform.ToMath .Inner (dict "displayMode" (eq .Type "block")) }}
{{ with $result.Error }}
// Print error.
{{ with else  $result.Value }}
{{ end }}

... I'm not totally sure I got the with else syntax correct.

If I remember correctly, there's also an option to print syntax errors in the rendered output.

jmooring commented 1 day ago

This:

{{ with transform.ToMath `\\frac 1 2` (dict "displayMode" true) }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ . }}
  {{ end }}
{{ end }}

Throws this error:

ERROR not a function

So... that one's not much help either.

bep commented 1 day ago

So... that one's not much help either.

Well, it's more ... to the point.

bep commented 15 hours ago

I have done some investigating, and the very short not a function error message is thrown by katex.renderToString, which smells like an upstream parser bug.

at useStrictBehavior (function.mjs:407:51)\n    at handler (function.mjs:6942:70)\n    at callFunction (function.mjs:13933:21)\n    at parseFunction (function.mjs:13919:19)\n    at parseGroup (function.mjs:14206:23)\n    at parseAtom (function.mjs:13796:23)\n    at parseExpression (function.mjs:13686:25)\n    at parse (function.mjs:13635:26)\n    at parseTree2 (function.mjs:14376:23)\n    at renderToDomTree2 (function.mjs:14425:18)\n    at renderToString2 (function.mjs:14406:18)\n    at render3 (function.mjs:14532:26)\n    at readInput (function.mjs:41:13)\n    at <anonymous> (function.mjs:14535:3)\n    at <anonymous> (function.mjs:14536:1)\n