refactor(search): support local/mirrored FlexSearch assets (#956)
* feat(search): support local and mirrored FlexSearch assets Add params.search.flexsearch.base/js overrides in search script loading. Document offline/local and mirror-based script asset configuration examples. * refactor(scripts): normalize remote asset URL joins * docs(config): clarify local asset examples * docs(i18n): add local asset config guidance
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
{{- /* CSS retrieval: get raw CSS from either local asset or remote, then process */ -}}
|
||||
{{- if $isRemoteBase -}}
|
||||
{{- $cssPath := cond (ne $asciinemaCssAsset "") $asciinemaCssAsset "asciinema-player.css" -}}
|
||||
{{- $asciinemaCssUrl := printf "%s/%s" $asciinemaBase $cssPath -}}
|
||||
{{- $asciinemaCssUrl := urls.JoinPath $asciinemaBase $cssPath -}}
|
||||
{{- with try (resources.GetRemote $asciinemaCssUrl) -}}
|
||||
{{- with .Err -}}
|
||||
{{- errorf "Could not retrieve Asciinema css file from %s. Reason: %s." $asciinemaCssUrl . -}}
|
||||
@@ -56,7 +56,7 @@
|
||||
{{- /* JS retrieval: get raw JS from either local asset or remote, then process */ -}}
|
||||
{{- if $isRemoteBase -}}
|
||||
{{- $jsPath := cond (ne $asciinemaJsAsset "") $asciinemaJsAsset (printf "asciinema-player%s.js" $minSuffix) -}}
|
||||
{{- $asciinemaJsUrl := printf "%s/%s" $asciinemaBase $jsPath -}}
|
||||
{{- $asciinemaJsUrl := urls.JoinPath $asciinemaBase $jsPath -}}
|
||||
{{- with try (resources.GetRemote $asciinemaJsUrl) -}}
|
||||
{{- with .Err -}}
|
||||
{{- errorf "Could not retrieve Asciinema js file from %s. Reason: %s." $asciinemaJsUrl . -}}
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
{{- $minSuffix := cond hugo.IsProduction ".min" "" -}}
|
||||
{{- if $isRemoteBase -}}
|
||||
{{- $cssPath := cond (ne $katexCssAsset "") $katexCssAsset (printf "katex%s.css" $minSuffix) -}}
|
||||
{{- $katexCssUrl := printf "%s/%s" $katexBase $cssPath -}}
|
||||
{{- $katexCssUrl := urls.JoinPath $katexBase $cssPath -}}
|
||||
{{- with try (resources.GetRemote $katexCssUrl) -}}
|
||||
{{- with .Err -}}
|
||||
{{- errorf "Could not retrieve KaTeX css file from %s. Reason: %s." $katexCssUrl . -}}
|
||||
@@ -59,7 +59,7 @@
|
||||
{{- $cssContent := . -}}
|
||||
{{- if $isRemoteBase -}}
|
||||
{{- $fontPattern := "url(fonts/" -}}
|
||||
{{- $fontSub := printf "url(%s/fonts/" $katexBase -}}
|
||||
{{- $fontSub := printf "url(%s/" (urls.JoinPath $katexBase "fonts") -}}
|
||||
{{- $cssContent = strings.Replace $cssContent $fontPattern $fontSub -}}
|
||||
{{- end -}}
|
||||
{{- with resources.FromString (printf "css/katex%s.css" $minSuffix) $cssContent -}}
|
||||
@@ -69,7 +69,7 @@
|
||||
{{- else -}}
|
||||
{{- if not $isRemoteBase -}}
|
||||
{{- $cssPath := cond (ne $katexCssAsset "") $katexCssAsset (printf "katex%s.css" $minSuffix) -}}
|
||||
<link rel="stylesheet" href="{{ printf "%s/%s" $katexBase $cssPath }}" />
|
||||
<link rel="stylesheet" href="{{ urls.JoinPath $katexBase $cssPath }}" />
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
{{- /* JS retrieval: get raw JS from either local asset or remote, then process */ -}}
|
||||
{{- if $isRemoteBase -}}
|
||||
{{- $jsPath := cond (ne $zoomJsAsset "") $zoomJsAsset (printf "medium-zoom%s.js" $minSuffix) -}}
|
||||
{{- $zoomJsUrl := printf "%s/%s" $zoomBase $jsPath -}}
|
||||
{{- $zoomJsUrl := urls.JoinPath $zoomBase $jsPath -}}
|
||||
{{- with try (resources.GetRemote $zoomJsUrl) -}}
|
||||
{{- with .Err -}}
|
||||
{{- errorf "Could not retrieve Medium Zoom js file from %s. Reason: %s." $zoomJsUrl . -}}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
{{- /* JS retrieval: get raw JS from either local asset or remote, then process */ -}}
|
||||
{{- if $isRemoteBase -}}
|
||||
{{- $jsPath := cond (ne $mermaidJsAsset "") $mermaidJsAsset (printf "mermaid%s.js" $minSuffix) -}}
|
||||
{{- $mermaidJsUrl := printf "%s/%s" $mermaidBase $jsPath -}}
|
||||
{{- $mermaidJsUrl := urls.JoinPath $mermaidBase $jsPath -}}
|
||||
{{- with try (resources.GetRemote $mermaidJsUrl) -}}
|
||||
{{- with .Err -}}
|
||||
{{- errorf "Could not retrieve Mermaid js file from %s. Reason: %s." $mermaidJsUrl . -}}
|
||||
|
||||
@@ -7,18 +7,55 @@
|
||||
{{- if hugo.IsProduction -}}
|
||||
{{- $jsSearch = $jsSearch | minify | fingerprint -}}
|
||||
{{- end -}}
|
||||
{{- $flexSearchVersion := site.Params.search.flexsearch.version | default "0.8.143" -}}
|
||||
{{- $flexSearchJsUrl := printf "https://cdn.jsdelivr.net/npm/flexsearch@%s/dist/flexsearch.bundle%s.js" $flexSearchVersion (cond hugo.IsProduction ".min" ".debug") -}}
|
||||
{{ with try (resources.GetRemote $flexSearchJsUrl) -}}
|
||||
{{ with .Err -}}
|
||||
{{ errorf "Could not retrieve FlexSearch js file from %s. Reason: %s." $flexSearchJsUrl . -}}
|
||||
{{ else with.Value -}}
|
||||
{{ with resources.Copy (printf "js/flexsearch.js") . -}}
|
||||
{{ $flexSearchJs := . | fingerprint -}}
|
||||
<script defer src="{{ $flexSearchJs.RelPermalink }}" integrity="{{ $flexSearchJs.Data.Integrity }}" crossorigin="anonymous"></script>
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
|
||||
{{- $flexSearchBase := "" -}}
|
||||
{{- $useDefaultCdn := true -}}
|
||||
{{- with site.Params.search.flexsearch.base -}}
|
||||
{{- $flexSearchBase = . -}}
|
||||
{{- $useDefaultCdn = false -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $flexSearchJsAsset := "" -}}
|
||||
{{- with site.Params.search.flexsearch.js -}}
|
||||
{{- $flexSearchJsAsset = . -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- /* If only js is set without base, use local asset loading. */ -}}
|
||||
{{- if and $useDefaultCdn (ne $flexSearchJsAsset "") -}}
|
||||
{{- $useDefaultCdn = false -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $bundleSuffix := cond hugo.IsProduction ".min" ".debug" -}}
|
||||
{{- if $useDefaultCdn -}}
|
||||
{{- $flexSearchVersion := site.Params.search.flexsearch.version | default "0.8.143" -}}
|
||||
{{- $flexSearchBase = printf "https://cdn.jsdelivr.net/npm/flexsearch@%s/dist" $flexSearchVersion -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $isRemoteBase := or (strings.HasPrefix $flexSearchBase "http://") (strings.HasPrefix $flexSearchBase "https://") -}}
|
||||
{{- if $isRemoteBase -}}
|
||||
{{- $jsPath := cond (ne $flexSearchJsAsset "") $flexSearchJsAsset (printf "flexsearch.bundle%s.js" $bundleSuffix) -}}
|
||||
{{- $flexSearchJsUrl := urls.JoinPath $flexSearchBase $jsPath -}}
|
||||
{{- with try (resources.GetRemote $flexSearchJsUrl) -}}
|
||||
{{- with .Err -}}
|
||||
{{- errorf "Could not retrieve FlexSearch js file from %s. Reason: %s." $flexSearchJsUrl . -}}
|
||||
{{- else with .Value -}}
|
||||
{{- with resources.Copy "js/flexsearch.js" . -}}
|
||||
{{- $flexSearchJs := . | fingerprint -}}
|
||||
<script defer src="{{ $flexSearchJs.RelPermalink }}" integrity="{{ $flexSearchJs.Data.Integrity }}" crossorigin="anonymous"></script>
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- else if $flexSearchJsAsset -}}
|
||||
{{- with resources.Get $flexSearchJsAsset -}}
|
||||
{{- $flexSearchJs := . | fingerprint -}}
|
||||
<script defer src="{{ $flexSearchJs.RelPermalink }}" integrity="{{ $flexSearchJs.Data.Integrity }}" crossorigin="anonymous"></script>
|
||||
{{- else -}}
|
||||
{{- errorf "FlexSearch js asset not found at %q" $flexSearchJsAsset -}}
|
||||
{{- end -}}
|
||||
{{- else if not $useDefaultCdn -}}
|
||||
{{- errorf "FlexSearch local loading requires params.search.flexsearch.js when using non-remote base %q" $flexSearchBase -}}
|
||||
{{- end -}}
|
||||
|
||||
<script defer src="{{ $jsSearch.RelPermalink }}" integrity="{{ $jsSearch.Data.Integrity }}"></script>
|
||||
{{- else -}}
|
||||
{{- warnf `search type "%s" is not supported` $searchType -}}
|
||||
|
||||
Reference in New Issue
Block a user