Browse Source

removed inline onload call for CSP compatability

master
Alexander Wong 1 month ago
parent
commit
532e7b3f34
Signed by: alex <alex@udia.ca> GPG Key ID: E90E5D6448C2C663
2 changed files with 23 additions and 9 deletions
  1. +17
    -0
      assets/js/katex-onload-template.js
  2. +6
    -9
      layouts/partials/client-katex.html

+ 17
- 0
assets/js/katex-onload-template.js View File

@@ -0,0 +1,17 @@
document.addEventListener("DOMContentLoaded", function(event) {
// HUGO VARIABLE MAGIC START
{{ $rawDelims := slice }}
{{ range $delim_val := .Site.Params.katexDelimiters }}
{{ $ld := index $delim_val 0 | replaceRE "([\\\\])(.*)" "\\\\${2}" }}{{ $rd := index $delim_val 1 | replaceRE "([\\\\])(.*)" "\\\\${2}" }}{{ $delim_p_str := "" }}
{{ if eq "display" (index $delim_val 2) }}{{ $delim_p_str = printf "{left:'%s',right:'%s',display:%s}" $ld $rd "true" }}{{ else }}{{ $delim_p_str = printf "{left:'%s',right:'%s',display:%s}" $ld $rd "false" }}{{ end }}
{{ $rawDelims = $rawDelims | append $delim_p_str}}
{{ else }}
{{ $rawDelims = slice "{left:'$$',right:'$$',display:true}" "{left:'\\\\(',right:'\\\\)',display:false}" }}
{{ end }}
const rawDelims = [{{ delimit $rawDelims "," | safeJS }}];
// HUGO VARIABLE MAGIC END
renderMathInElement(
document.getElementById('markdownBody'),
{delimiters:rawDelims}
);
});

+ 6
- 9
layouts/partials/client-katex.html View File

@@ -1,11 +1,8 @@
{{ with resources.Get "js/katex.js" | minify | fingerprint }}<script type="text/javascript" src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" defer></script>{{ end }}
{{ $autoJs := resources.Get "js/katex-auto-render.js" }}{{ $copyJs := resources.Get "js/katex-copy-tex.js" }}
{{ $rawDelims := slice }}
{{ range $delim_val := .Site.Params.katexDelimiters }}
{{ $ld := index $delim_val 0 | replaceRE "([\\\\])(.*)" "\\\\${2}" }}{{ $rd := index $delim_val 1 | replaceRE "([\\\\])(.*)" "\\\\${2}" }}{{ $delim_p_str := "" }}
{{ if eq "display" (index $delim_val 2) }}{{ $delim_p_str = printf "{left:'%s',right:'%s',display:%s}" $ld $rd "true" }}{{ else }}{{ $delim_p_str = printf "{left:'%s',right:'%s',display:%s}" $ld $rd "false" }}{{ end }}
{{ $rawDelims = $rawDelims | append $delim_p_str}}
{{ else }}
{{ $rawDelims = slice "{left:'$$',right:'$$',display:true}" "{left:'\\\\(',right:'\\\\)',display:false}" }}
{{ end }}
{{ with slice $autoJs $copyJs | resources.Concat "js/tex-contrib.js" | minify | fingerprint }}<script type="text/javascript" src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" defer onload="renderMathInElement(document.getElementById('markdownBody'),{delimiters:[{{ delimit $rawDelims "," | safeJS }}]});"></script>{{ end }}
{{ with slice $autoJs $copyJs | resources.Concat "js/tex-contrib.js" | minify | fingerprint }}<script type="text/javascript" src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" defer></script>{{ end }}

{{ $loadKatexJs := resources.Get "js/katex-onload-template.js" | resources.ExecuteAsTemplate "js/load-katex.js" . }}
{{ with $loadKatexJs | minify | fingerprint }}
<script type="text/javascript" src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}"></script>
{{ end }}

Loading…
Cancel
Save