-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow mathematics in Markdown, retrieve KaTeX scripts files at build …
…time
- Loading branch information
Showing
4 changed files
with
80 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{{ $version := .Site.Params.katex.version | default "latest" -}} | ||
|
||
{{/* load stylesheet and scripts for KaTeX support */ -}} | ||
{{ $katex_css_url := printf "https://cdn.jsdelivr.net/npm/katex@%s/dist/katex.min.css" $version -}} | ||
{{ with resources.GetRemote $katex_css_url -}} | ||
{{ with .Err -}} | ||
{{ errorf "Could not retrieve KaTeX css file from CDN. Reason: %s." . -}} | ||
{{ else -}} | ||
{{ $secureCSS := . | resources.Fingerprint "sha512" -}} | ||
<link rel="stylesheet" href="{{- $katex_css_url -}}" integrity="{{- $secureCSS.Data.Integrity -}}" crossorigin="anonymous"> | ||
{{ end -}} | ||
{{ else -}} | ||
{{ errorf "Invalid KaTeX version %s, could not retrieve this version from CDN." $version -}} | ||
{{ end -}} | ||
|
||
{{/* The loading of KaTeX is deferred to speed up page rendering */ -}} | ||
{{ $katex_js_url := printf "https://cdn.jsdelivr.net/npm/katex@%s/dist/katex.min.js" $version -}} | ||
{{ with resources.GetRemote $katex_js_url -}} | ||
{{ with .Err -}} | ||
{{ errorf "Could not retrieve KaTeX script from CDN. Reason: %s." . -}} | ||
{{ else -}} | ||
{{ $secureJS := . | resources.Fingerprint "sha512" -}} | ||
<script defer src="{{- .RelPermalink -}}" integrity="{{- $secureJS.Data.Integrity -}}" crossorigin="anonymous" ></script> | ||
{{ end -}} | ||
{{ else -}} | ||
{{ errorf "Invalid KaTeX version %s, could not retrieve this version from CDN." $version -}} | ||
{{ end -}} | ||
|
||
{{/* Add support for displaying chemical equations and physical units by loading the mhchem extension: */ -}} | ||
{{ if .mhchem -}} | ||
{{ partial "scripts/mhchem.html" (dict "version" $version) -}} | ||
{{ end -}} | ||
|
||
{{/* To automatically render math in text elements, include the auto-render extension: */ -}} | ||
|
||
{{ $katex_autorender_url := printf "https://cdn.jsdelivr.net/npm/katex@%s/dist/contrib/auto-render.min.js" $version -}} | ||
{{ with resources.GetRemote $katex_autorender_url -}} | ||
{{ with .Err -}} | ||
{{ errorf "Could not retrieve KaTeX auto render extension from CDN. Reason: %s." . -}} | ||
{{ else -}} | ||
{{ $secureJS := . | resources.Fingerprint "sha512" -}} | ||
<script defer src="{{- .RelPermalink -}}" integrity="{{- $secureJS.Data.Integrity -}}" crossorigin="anonymous" | ||
{{ 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 -}} > | ||
</script> | ||
{{ end -}} | ||
{{ else -}} | ||
{{ errorf "Invalid KaTeX version %s, could not retrieve this version from CDN." $version -}} | ||
{{ end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{{/* Add support for displaying chemical equations and physical units by loading the mhchem extension: */ -}} | ||
{{ $mhchem_url := printf "https://cdn.jsdelivr.net/npm/katex@%s/dist/contrib/mhchem.min.js" .version -}} | ||
{{ with resources.GetRemote $mhchem_url -}} | ||
{{ with .Err -}} | ||
{{ errorf "Could not retrieve mhchem script from CDN. Reason: %s." . -}} | ||
{{ else -}} | ||
{{ $secureJS := . | resources.Fingerprint "sha512" -}} | ||
<script defer src="{{- .RelPermalink -}}" integrity="{{- $secureJS.Data.Integrity -}}" crossorigin="anonymous" ></script> | ||
{{ end -}} | ||
{{ else -}} | ||
{{ errorf "Invalid KaTeX version %s, could not retrieve this version from CDN." .version -}} | ||
{{ end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters