katex.html 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. {{ $version := .Site.Params.katex.version | default "latest" -}}
  2. {{/* load stylesheet and scripts for KaTeX support */ -}}
  3. {{ $katex_css_url := printf "https://cdn.jsdelivr.net/npm/katex@%s/dist/katex%s.css" $version (cond hugo.IsProduction ".min" "") -}}
  4. {{ with try (resources.GetRemote $katex_css_url) -}}
  5. {{ with .Err -}}
  6. {{ errorf "Could not retrieve KaTeX css file from CDN. Reason: %s." . -}}
  7. {{ else with.Value -}}
  8. {{ with resources.Copy (printf "css/katex%s.css" (cond hugo.IsProduction ".min" "")) . }}
  9. {{ $secureCSS := . | resources.Fingerprint "sha512" -}}
  10. <link rel="stylesheet" href="{{- .RelPermalink -}}" integrity="{{- $secureCSS.Data.Integrity -}}" crossorigin="anonymous">
  11. {{ end -}}
  12. {{ end -}}
  13. {{ else -}}
  14. {{ errorf "Invalid KaTeX version %s, could not retrieve this version from CDN." $version -}}
  15. {{ end -}}
  16. {{/* The loading of KaTeX is deferred to speed up page rendering */ -}}
  17. {{ $katex_js_url := printf "https://cdn.jsdelivr.net/npm/katex@%s/dist/katex%s.js" $version (cond hugo.IsProduction ".min" "") -}}
  18. {{ with try (resources.GetRemote $katex_js_url) -}}
  19. {{ with .Err -}}
  20. {{ errorf "Could not retrieve KaTeX script from CDN. Reason: %s." . -}}
  21. {{ else with.Value -}}
  22. {{ with resources.Copy (printf "js/katex%s.js" (cond hugo.IsProduction ".min" "")) . }}
  23. {{ $secureJS := . | resources.Fingerprint "sha512" -}}
  24. <script defer src="{{- .RelPermalink -}}" integrity="{{- $secureJS.Data.Integrity -}}" crossorigin="anonymous" ></script>
  25. {{ end -}}
  26. {{ end -}}
  27. {{ else -}}
  28. {{ errorf "Invalid KaTeX version %s, could not retrieve this version from CDN." $version -}}
  29. {{ end -}}
  30. {{/* Add support for displaying chemical equations and physical units by loading the mhchem extension: */ -}}
  31. {{ if .mhchem -}}
  32. {{ partial "scripts/mhchem.html" (dict "version" $version) -}}
  33. {{ end -}}
  34. {{/* To automatically render math in text elements, include the auto-render extension: */ -}}
  35. {{ $katex_autorender_url := printf "https://cdn.jsdelivr.net/npm/katex@%s/dist/contrib/auto-render%s.js" $version (cond hugo.IsProduction ".min" "") -}}
  36. {{ with try (resources.GetRemote $katex_autorender_url) -}}
  37. {{ with .Err -}}
  38. {{ errorf "Could not retrieve KaTeX auto render extension from CDN. Reason: %s." . -}}
  39. {{ else with .Value -}}
  40. {{ with resources.Copy ( printf "js/auto-render%s.js" (cond hugo.IsProduction ".min" "")) . }}
  41. {{ $secureJS := . | resources.Fingerprint "sha512" -}}
  42. <script defer src="{{- .RelPermalink -}}" integrity="{{- $secureJS.Data.Integrity -}}" crossorigin="anonymous"
  43. {{ printf "onload='renderMathInElement(%s, %s);'" (( $.Page.Site.Params.katex.html_dom_element | default "document.body" ) | safeJS ) ( printf "%s" ( $.Page.Site.Params.katex.options | jsonify )) | safeHTMLAttr -}} >
  44. </script>
  45. {{ end -}}
  46. {{ end -}}
  47. {{ else -}}
  48. {{ errorf "Invalid KaTeX version %s, could not retrieve this version from CDN." $version -}}
  49. {{ end -}}