fix: KaTeX fonts are not fetched from CDN (#656)

This commit is contained in:
Justin Chen 2025-04-04 07:07:12 +08:00 committed by GitHub
parent 83fda0109f
commit 1f88cff7d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -63,12 +63,17 @@
{{ $noop := .WordCount -}} {{ $noop := .WordCount -}}
{{ if .Page.Store.Get "hasMath" -}} {{ if .Page.Store.Get "hasMath" -}}
<!-- TODO: make url configurable --> <!-- TODO: make url configurable -->
{{ $katexCssUrl := printf "https://cdn.jsdelivr.net/npm/katex@latest/dist/katex%s.css" (cond hugo.IsProduction ".min" "") -}} {{ $katexBaseUrl := "https://cdn.jsdelivr.net/npm/katex@latest/dist" }}
{{ $katexCssUrl := printf "%s/katex%s.css" $katexBaseUrl (cond hugo.IsProduction ".min" "") -}}
{{ $katexFontPattern := "url(fonts/" }}
{{ $katexFontSubstituted := printf "url(%s/fonts/" $katexBaseUrl }}
{{ with try (resources.GetRemote $katexCssUrl) -}} {{ with try (resources.GetRemote $katexCssUrl) -}}
{{ with .Err -}} {{ with .Err -}}
{{ errorf "Could not retrieve KaTeX css file from %s. Reason: %s." $katexCssUrl . -}} {{ errorf "Could not retrieve KaTeX css file from %s. Reason: %s." $katexCssUrl . -}}
{{ else with.Value -}} {{ else with.Value -}}
{{ with resources.Copy (printf "css/katex%s.css" (cond hugo.IsProduction ".min" "")) . -}} {{ $katexCssContent := strings.Replace .Content $katexFontPattern $katexFontSubstituted }}
{{ with resources.FromString (printf "css/katex%s.css" (cond hugo.IsProduction ".min" "")) $katexCssContent -}}
<link rel="stylesheet" href="{{- .RelPermalink -}}" integrity="{{- . | fingerprint -}}" crossorigin="anonymous" /> <link rel="stylesheet" href="{{- .RelPermalink -}}" integrity="{{- . | fingerprint -}}" crossorigin="anonymous" />
{{ end -}} {{ end -}}
{{ end -}} {{ end -}}