Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Assets #1275

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ assets/lib
*.md
**/public
main.css
*.json
16 changes: 0 additions & 16 deletions assets/js/lib/mathjax.js

This file was deleted.

15 changes: 15 additions & 0 deletions assets/js/lib/mathjax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const delimiters = window.config?.math?.delimiters;
if (delimiters) {
const displayMath = delimiters
.filter((d) => d.display)
.map((d) => [d.left, d.right]);
const inlineMath = delimiters
.filter((d) => !d.display)
.map((d) => [d.left, d.right]);
window.MathJax = {
tex: {
displayMath,
inlineMath,
},
};
}
3 changes: 0 additions & 3 deletions assets/js/lib/twemoji.js

This file was deleted.

2 changes: 2 additions & 0 deletions assets/js/lib/twemoji.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import twemoji from "twemoji";
twemoji.parse(document.body);
232 changes: 116 additions & 116 deletions layouts/partials/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,117 +26,6 @@
{{- end -}}
{{- end -}}

{{- /* tablesort */ -}}
{{- if $params.table | and $params.table.sort | and (.Scratch.Get "this").tablesort -}}
{{- $source := $cdn.tablesort | default "lib/tablesort/tablesort.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $config = dict "sort" true | dict "table" | merge $config -}}
{{- end -}}

{{- /* twemoji */ -}}
{{- if $params.twemoji -}}
{{- $config = dict "twemoji" true | merge $config -}}
{{- $options := dict -}}

{{- $source := $cdn.twemojiJS | default "lib/twemoji/twemoji.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $shims := dict "twemoji" "js/shims/twemoji.js" -}}
{{- $options = dict "shims" $shims | merge $options -}}

{{- $options = dict "targetPath" "js/twemoji.min.js" | merge $options -}}
{{- $options = dict "minify" true | merge $options -}}
{{- $js := resources.Get "js/lib/twemoji.js" | js.Build $options -}}
{{- $_ := $js.RelPermalink -}}
{{- dict "Link" $js.RelPermalink "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- end -}}

{{- /* lightgallery.js */ -}}
{{- if $params.lightgallery -}}
{{- $source := $cdn.lightgalleryCSS | default "lib/lightgallery/lightgallery.min.css" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}}
{{- $source := $cdn.lightgalleryJS | default "lib/lightgallery/lightgallery.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $source := $cdn.lightgalleryThumbnailJS | default "lib/lightgallery/lg-thumbnail.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $source := $cdn.lightgalleryZoomJS | default "lib/lightgallery/lg-zoom.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $config = dict "selector" ".lightgallery" "speed" 400 "hideBarsDelay" 2000 "thumbnail" true "exThumbImage" "data-thumbnail" "thumbWidth" 80 "thumbContHeight" 80 "actualSize" false | dict "lightGallery" | merge $config -}}
{{- end -}}

{{- /* Sharer.js */ -}}
{{- if $params.share.enable | and (.Scratch.Get "this").sharer -}}
{{- $source := $cdn.sharerJS | default "lib/sharer/sharer.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $config = dict "sharerjs" true | merge $config -}}
{{- end -}}

{{- /* TypeIt */ -}}
{{- with (.Scratch.Get "this").typeitMap -}}
{{- $typeit := $.Site.Params.typeit -}}
{{- $source := $cdn.typeitJS | default "lib/typeit/typeit.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" $.Scratch "Data" | partial "scratch/script.html" -}}
{{- $config = dict "speed" $typeit.speed "cursorSpeed" $typeit.cursorSpeed "cursorChar" $typeit.cursorChar "duration" $typeit.duration "data" . | dict "typeit" | merge $config -}}
{{- end -}}

{{- /* KaTeX */ -}}
{{- $math := $params.math -}}
{{- if eq $math true -}}
{{- $math = .Site.Params.page.math | default dict -}}
{{- else if eq $math false -}}
{{- $math = dict "enable" false -}}
{{- end -}}
{{- if $math.enable -}}
{{- if $math.mathjax -}}
{{- $source := "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $options := dict -}}
{{- $options = dict "targetPath" "js/mathjax.min.js" | merge $options -}}
{{- $options = dict "minify" true | merge $options -}}
{{- $js := resources.Get "js/lib/mathjax.js" | js.Build $options -}}
{{- $_ := $js.RelPermalink -}}
{{- dict "Link" $js.RelPermalink "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}

{{- else -}}

{{- $source := $cdn.katexCSS | default "lib/katex/katex.min.css" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}}
{{- $source := $cdn.katexJS | default "lib/katex/katex.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $source := $cdn.katexAutoRenderJS | default "lib/katex/auto-render.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- if $math.copyTex -}}
{{- $source := $cdn.katexCopyTexCSS | default "lib/katex/copy-tex.min.css" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}}
{{- $source := $cdn.katexCopyTexJS | default "lib/katex/copy-tex.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- end -}}
{{- if $math.mhchem -}}
{{- $source := $cdn.katexMhchemJS | default "lib/katex/mhchem.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- end -}}
{{- $shims := dict "katex/dist/contrib/auto-render" "js/shims/katex.js" -}}
{{- $options := dict -}}
{{- $options = dict "targetPath" "js/katex.min.js" | merge $options -}}
{{- $options = dict "minify" true | merge $options -}}
{{- $options = dict "shims" $shims | merge $options -}}
{{- $js := resources.Get "js/lib/katex.js" | js.Build $options -}}
{{- $_ := $js.RelPermalink -}}
{{- dict "Link" $js.RelPermalink "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}

{{- end -}}

{{- $blockDelimiters := slice (dict "left" "$$" "right" "$$" "display" true) (dict "left" "\\[" "right" "\\]" "display" true) -}}
{{- if and $math.blockLeftDelimiter $math.blockRightDelimiter -}}
{{- $blockDelimiters = slice (dict "left" $math.blockLeftDelimiter "right" $math.blockRightDelimiter "display" true) -}}
{{- end -}}
{{- $inlineDelimiters := slice (dict "left" "$" "right" "$" "display" false) (dict "left" "\\(" "right" "\\)" "display" false) -}}
{{- if and $math.inlineLeftDelimiter $math.inlineRightDelimiter -}}
{{- $inlineDelimiters = slice (dict "left" $math.inlineLeftDelimiter "right" $math.inlineRightDelimiter "display" false) -}}
{{- end -}}
{{- $delimiters := $blockDelimiters | append $inlineDelimiters -}}
{{- $config = dict "strict" false "delimiters" $delimiters | dict "math" | merge $config -}}
{{- end -}}

{{- /* mermaid */ -}}
{{- if (.Scratch.Get "this").mermaid -}}
<script type="module">
Expand Down Expand Up @@ -285,16 +174,106 @@
{{- delimit . "\n" | dict "Content" | dict "Scratch" $.Scratch "Data" | partial "scratch/script.html" -}}
{{- end -}}

{{- range (.Scratch.Get "this").style -}}
{{- partial "plugin/style.html" . -}}
{{- end -}}

{{- /* tablesort */ -}}
{{/* Run only when table sort is enabled and there are actual tables on this page */}}
{{- if $params.table | and $params.table.sort | and (.Scratch.Get "this").tablesort -}}
{{- dict "cdn" $cdn.tablesort "default" "lib/tablesort/tablesort.min.js" | partial "partials/assets/deferScript.html" -}}
{{- $config = dict "sort" true | dict "table" | merge $config -}}
{{- end -}}

{{- /* twemoji */ -}}
{{/* Run only when twemoji is enabled */}}
{{- if $params.twemoji -}}
{{- dict "cdn" $cdn.twemojiJS "default" "lib/twemoji/twemoji.min.js" | partial "partials/assets/deferScript.html" -}}

{{- $options := dict "shims" (dict "twemoji" "js/shims/twemoji.js") "targetPath" "js/twemoji.min.js" "minify" true -}}
{{- $js := resources.Get "js/lib/twemoji.ts" | js.Build $options -}}
<script src="{{ $js.RelPermalink }}" defer></script>
{{- end -}}

{{- /* lightgallery.js */ -}}
{{/* Run only when lightgallary is enabled */}}
{{- if $params.lightgallery -}}
{{- dict "cdn" $cdn.lightgalleryCSS "default" "lib/lightgallery/lightgallery.min.css" | partial "partials/assets/deferStyle.html" -}}
{{- dict "cdn" $cdn.lightgalleryJS "default" "lib/lightgallery/lightgallery.min.js" | partial "partials/assets/deferScript.html" -}}
{{- dict "cdn" $cdn.lightgalleryThumbnailJS "default" "lib/lightgallery/lg-thumbnail.min.js" | partial "partials/assets/deferScript.html" -}}
{{- dict "cdn" $cdn.lightgalleryZoomJS "default" "lib/lightgallery/lg-zoom.min.js" | partial "partials/assets/deferScript.html" -}}

{{- $config = dict "selector" ".lightgallery" "speed" 400 "hideBarsDelay" 2000 "thumbnail" true "exThumbImage" "data-thumbnail" "thumbWidth" 80 "thumbContHeight" 80 "actualSize" false | dict "lightGallery" | merge $config -}}
{{- end -}}

{{- /* Sharer.js */ -}}
{{/* Run only when enabled and on page that's sharable */}}
{{- if $params.share.enable | and (.Scratch.Get "this").sharer -}}
{{- $src := dict "cdn" $cdn.sharerJS "default" "lib/sharer/sharer.min.js" | partial "partials/assets/deferScript.html" -}}

{{- $config = dict "sharerjs" true | merge $config -}}
{{- end -}}

{{- /* TypeIt */ -}}
{{- with (.Scratch.Get "this").typeitMap -}}
{{- $typeit := $.Site.Params.typeit -}}
{{- dict "cdn" $cdn.typeitJS "default" "lib/typeit/typeit.min.js" | partial "partials/assets/deferScript.html" -}}

{{- $config = dict "speed" $typeit.speed "cursorSpeed" $typeit.cursorSpeed "cursorChar" $typeit.cursorChar "duration" $typeit.duration "data" . | dict "typeit" | merge $config -}}
{{- end -}}

{{- /* Math (KaTeX or mathjax) */ -}}
{{- $math := $params.math -}}
{{- if eq $math true -}}
{{- $math = .Site.Params.page.math | default dict -}}
{{- else if eq $math false -}}
{{- $math = dict "enable" false -}}
{{- end -}}
{{- if $math.enable -}}
{{- if $math.mathjax -}}
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

{{- $options := dict "targetPath" "js/mathjax.min.js" "minify" true -}}
{{- $js := resources.Get "js/lib/mathjax.ts" | js.Build $options -}}
<script defer src="{{ $js.RelPermalink }}"></script>
{{- else -}}
{{- dict "cdn" $cdn.katexCSS "default" "lib/katex/katex.min.css" | partial "partials/assets/deferStyle.html" -}}
{{- dict "cdn" $cdn.katexJS "default" "lib/katex/katex.min.js" | partial "partials/assets/deferScript.html" -}}

{{- dict "cdn" $cdn.katexAutoRenderJS "default" "lib/katex/auto-render.min.js" | partial "partials/assets/deferScript.html" -}}

{{- if $math.copyTex -}}
{{- dict "cdn" $cdn.katexCopyTexCSS "default" "lib/katex/copy-tex.min.css" | partial "partials/assets/deferStyle.html" -}}
{{- dict "cdn" $cdn.katexCopyTexJS "default" "lib/katex/copy-tex.min.js" | partial "partials/assets/deferScript.html" -}}
{{- end -}}

{{- if $math.mhchem -}}
{{- dict "cdn" $cdn.katexMhchemJS "default" "lib/katex/mhchem.min.js" | partial "partials/assets/deferScript.html" -}}
{{- end -}}

{{- $options := dict "targetPath" "js/katex.min.js" "minify" true "shims" (dict "katex/dist/contrib/auto-render" "js/shims/katex.js") -}}
{{- $js := resources.Get "js/lib/katex.js" | js.Build $options -}}
<script defer src="{{ $js.RelPermalink }}"></script>
{{- end -}}

{{- $blockDelimiters := slice (dict "left" "$$" "right" "$$" "display" true) (dict "left" "\\[" "right" "\\]" "display" true) -}}
{{- if and $math.blockLeftDelimiter $math.blockRightDelimiter -}}
{{- $blockDelimiters = slice (dict "left" $math.blockLeftDelimiter "right" $math.blockRightDelimiter "display" true) -}}
{{- end -}}
{{- $inlineDelimiters := slice (dict "left" "$" "right" "$" "display" false) (dict "left" "\\(" "right" "\\)" "display" false) -}}
{{- if and $math.inlineLeftDelimiter $math.inlineRightDelimiter -}}
{{- $inlineDelimiters = slice (dict "left" $math.inlineLeftDelimiter "right" $math.inlineRightDelimiter "display" false) -}}
{{- end -}}
{{- $delimiters := $blockDelimiters | append $inlineDelimiters -}}
{{- $config = dict "strict" false "delimiters" $delimiters | dict "math" | merge $config -}}
{{- end -}}

{{/* Instant page */}}
{{- if $params.instantpage.enable -}}
{{- $js := resources.Get "/lib/instant.page/instantpage.min.js" -}}
{{- $js := resources.Get "/lib/instant.page/instantpage.min.js" -}}
<script src="{{ $js.RelPermalink }}" type="module"></script>
{{- end -}}

{{- range (.Scratch.Get "this").style -}}
{{- partial "plugin/style.html" . -}}
{{- end -}}

{{/* Config script */}}
<script>
{{- $config | jsonify | printf "window.config=%s;" | safeJS -}}
Expand All @@ -309,3 +288,24 @@
{{- end -}}

{{- partial "plugin/analytics.html" . -}}

{{/* This function get the relative permalink of an asset */}}
{{/* cdn: the link to the cdn */}}
{{/* default: the path to the local asset */}}
{{- define "partials/assets/RelPermalink.html" -}}
{{- $src := .cdn | default .default -}}
{{- $value := resources.Get $src -}}
{{- return $value.RelPermalink -}}
{{- end -}}

{{- define "partials/assets/deferScript.html" -}}
{{- $src := .cdn | default .default -}}
{{- $value := resources.Get $src -}}
<script defer src="{{ $value.RelPermalink }}"></script>
{{- end -}}

{{- define "partials/assets/deferStyle.html" -}}
{{- $src := .cdn | default .default -}}
{{- $value := resources.Get $src -}}
<link rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'" href="{{ $value.RelPermalink }}"><noscript><link rel="stylesheet" href="{{ $value.RelPermalink }}"></noscript>
{{- end -}}
Empty file.
Loading