mirror of
https://github.com/McShelby/hugo-theme-relearn.git
synced 2024-11-27 01:33:04 +00:00
dependencies: make loader more versatile #820
This commit is contained in:
parent
bd49d54aa2
commit
8f135335f3
7 changed files with 84 additions and 52 deletions
|
@ -174,13 +174,13 @@ If you are unhappy with the default, you can define the following CSS variable i
|
||||||
|
|
||||||
## Own Shortcodes with JavaScript Dependencies
|
## Own Shortcodes with JavaScript Dependencies
|
||||||
|
|
||||||
Certain shortcodes make use of additional JavaScript files. The theme only loads these dependencies if the shortcode is used. To do so correctly the theme adds management code in various files.
|
Certain shortcodes make use of additional dependencies like JavaScript and CSS files. The theme only loads these dependencies if the shortcode is used. To do so correctly the theme adds management code in various files.
|
||||||
|
|
||||||
You can you use this mechanism in your own shortcodes. Say you want to add a shortcode `myshortcode` that also requires the `jquery` JavaScript library.
|
You can you use this mechanism in your own shortcodes. Say you want to add a shortcode `myshortcode` that also requires the `jquery` JavaScript library.
|
||||||
|
|
||||||
1. Write the shortcode file `layouts/shortcodes/myshortcode.html` and add the following line
|
1. Write the shortcode file `layouts/shortcodes/myshortcode.html` and add the following line
|
||||||
|
|
||||||
````go
|
````go {title="layouts/shortcodes/myshortcode.html"}
|
||||||
{{- .Page.Store.Set "hasMyShortcode" true }}
|
{{- .Page.Store.Set "hasMyShortcode" true }}
|
||||||
````
|
````
|
||||||
|
|
||||||
|
@ -190,13 +190,17 @@ You can you use this mechanism in your own shortcodes. Say you want to add a sho
|
||||||
[params.relearn.dependencies]
|
[params.relearn.dependencies]
|
||||||
[params.relearn.dependencies.myshortcode]
|
[params.relearn.dependencies.myshortcode]
|
||||||
name = "MyShortcode"
|
name = "MyShortcode"
|
||||||
location = "footer"
|
|
||||||
{{< /multiconfig >}}
|
{{< /multiconfig >}}
|
||||||
|
|
||||||
1. Add the dependency loader file `layouts/partials/dependencies/myshortcode.html`. The loader file will be appended to your header or footer, dependend on the `location` setting in your `hugo.toml`.
|
1. Add the dependency loader file `layouts/partials/dependencies/myshortcode.html`. The loader file will be called from multiple locations inside of the theme with the parameter `page` containing the current [page](https://gohugo.io/methods/page/) variable and `location` with one of the currently defined locations
|
||||||
|
|
||||||
````html
|
* `header`: if called at the end of the HTML `head` element
|
||||||
<script src="https://www.unpkg.com/jquery/dist/jquery.js"></script>
|
* `footer`: if called at the end of the HTML `body` element
|
||||||
|
|
||||||
|
````go {title="layouts/partials/dependencies/myshortcode.html"}
|
||||||
|
{{- if eq .location "footer" }}
|
||||||
|
<script src="https://www.unpkg.com/jquery/dist/jquery.js"></script>
|
||||||
|
{{- end }}
|
||||||
````
|
````
|
||||||
|
|
||||||
Character casing is relevant!
|
Character casing is relevant!
|
||||||
|
@ -206,6 +210,15 @@ Character casing is relevant!
|
||||||
|
|
||||||
See the `math`, `mermaid` and `openapi` shortcodes for examples.
|
See the `math`, `mermaid` and `openapi` shortcodes for examples.
|
||||||
|
|
||||||
|
{{% notice note %}}
|
||||||
|
If you are really into customization of the theme and want to use the dependency loader for your own locations, you can do this by simply calling it from inside of your overriden partials
|
||||||
|
|
||||||
|
````go
|
||||||
|
{{- partial "dependencies.html" (dict "page" . "location" "mylocation") }}
|
||||||
|
````
|
||||||
|
|
||||||
|
{{% /notice %}}
|
||||||
|
|
||||||
## Output Formats
|
## Output Formats
|
||||||
|
|
||||||
Certain parts of the theme can be changed for support of your own [output formats](https://gohugo.io/templates/output-formats/). Eg. if you define a new output format `PLAINTEXT` in your `hugo.toml`, you can add a file `layouts/partials/header.plaintext.html` to change the way, the page header should look like for that output format.
|
Certain parts of the theme can be changed for support of your own [output formats](https://gohugo.io/templates/output-formats/). Eg. if you define a new output format `PLAINTEXT` in your `hugo.toml`, you can add a file `layouts/partials/header.plaintext.html` to change the way, the page header should look like for that output format.
|
||||||
|
|
|
@ -20,6 +20,14 @@ This document shows you what's new in the latest release and flags it with one o
|
||||||
|
|
||||||
## 5.27.0.beta (XXXX-XX-XX) {#5270}
|
## 5.27.0.beta (XXXX-XX-XX) {#5270}
|
||||||
|
|
||||||
|
- {{% badge style="note" title=" " %}}Change{{% /badge %}} The [dependency loader](basics/customization#own-shortcodes-with-javascript-dependencies) was made more versatile.
|
||||||
|
|
||||||
|
The configuration in your `hugo.toml` does not require the `location` parameter anymore. If you still use it, the theme will work as before but will generate a warning. So you don't need to change anything, yet.
|
||||||
|
|
||||||
|
With the new mechanism, your dependency loader now receives an additional `location` parameter instead that you can query to inject your dependencies in the desired location.
|
||||||
|
|
||||||
|
By that you can now call the dependency mechanism in your own overriden partials by giving it a distinct `location` parameter. In addition your injected files can now be spread to multiple locations which wasn't previously possible.
|
||||||
|
|
||||||
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} You now can scroll forward and backward thru all headings of a page by using `ALT + 🡑` and `ALT + 🡓`. This also works for the `PRINT` output format
|
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} You now can scroll forward and backward thru all headings of a page by using `ALT + 🡑` and `ALT + 🡓`. This also works for the `PRINT` output format
|
||||||
|
|
||||||
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The breadcrumbs used in the topbar, search results and the taxonomy term lists are now using the pages frontmatter `linktitle` instead of `title` if set.
|
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The breadcrumbs used in the topbar, search results and the taxonomy term lists are now using the pages frontmatter `linktitle` instead of `title` if set.
|
||||||
|
|
|
@ -34,10 +34,7 @@
|
||||||
[params.relearn.dependencies]
|
[params.relearn.dependencies]
|
||||||
[params.relearn.dependencies.mathjax]
|
[params.relearn.dependencies.mathjax]
|
||||||
name = "MathJax"
|
name = "MathJax"
|
||||||
location = "footer"
|
|
||||||
[params.relearn.dependencies.mermaid]
|
[params.relearn.dependencies.mermaid]
|
||||||
name = "Mermaid"
|
name = "Mermaid"
|
||||||
location = "footer"
|
|
||||||
[params.relearn.dependencies.openapi]
|
[params.relearn.dependencies.openapi]
|
||||||
name = "OpenApi"
|
name = "OpenApi"
|
||||||
location = "footer"
|
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
{{- $page := .page }}
|
{{- $page := .page }}
|
||||||
{{- $location := .location }}
|
{{- $location := .location }}
|
||||||
{{- $outputFormat := .outputFormat }}
|
{{- $outputFormat := .outputFormat | default (partial "output-format.hugo" $page) }}
|
||||||
{{- range $k, $v := $page.Site.Params.relearn.dependencies }}
|
{{- range $k, $v := $page.Site.Params.relearn.dependencies }}
|
||||||
{{- $has := printf "has%s" $v.name }}
|
{{- $has := printf "has%s" $v.name }}
|
||||||
{{- $nestedhas := printf "nestedHas%s" $v.name }}
|
{{- $nestedhas := printf "nestedHas%s" $v.name }}
|
||||||
{{- $wants := or ($page.Page.Store.Get $has) (and ($page.Page.Store.Get (printf "%sIsNested" $outputFormat)) ($page.Page.Store.Get $nestedhas)) }}
|
{{- $wants := or ($page.Page.Store.Get $has) (and ($page.Page.Store.Get (printf "%sIsNested" $outputFormat)) ($page.Page.Store.Get $nestedhas)) }}
|
||||||
{{- if and $wants (eq $location $v.location) }}
|
{{- if and $wants }}
|
||||||
{{- $dep := printf "dependencies/%s.html" $k }}
|
{{- if $v.location }}
|
||||||
{{- partial $dep (dict "page" $page) }}
|
{{- warnf "DEPRECATED parameter 'location' for dependency '%s' configured in your hugo.toml, query the 'location' parameter inside your dependency loader instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5270" $k}}
|
||||||
|
{{- end }}
|
||||||
|
{{- if or (not $v.location) (eq $location $v.location) }}
|
||||||
|
{{- $dep := printf "dependencies/%s.html" $k }}
|
||||||
|
{{- partial $dep (dict "page" $page "location" $location) }}
|
||||||
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
|
@ -1,11 +1,13 @@
|
||||||
{{- $page := .page }}
|
{{- $page := .page }}
|
||||||
{{- with $page }}
|
{{- $location := .location }}
|
||||||
{{- $init := "{}" }}
|
{{- if eq $location "footer" }}
|
||||||
{{- if isset .Params "mathjaxinitialize" }}
|
{{- with $page }}
|
||||||
{{- $init = .Params.mathJaxInitialize }}
|
{{- $init := "{}" }}
|
||||||
{{- else if isset .Site.Params "mathjaxinitialize" }}
|
{{- if isset .Params "mathjaxinitialize" }}
|
||||||
{{- $init = .Site.Params.mathJaxInitialize }}
|
{{- $init = .Params.mathJaxInitialize }}
|
||||||
{{- end }}
|
{{- else if isset .Site.Params "mathjaxinitialize" }}
|
||||||
|
{{- $init = .Site.Params.mathJaxInitialize }}
|
||||||
|
{{- end }}
|
||||||
<script>
|
<script>
|
||||||
function useMathJax( config ){
|
function useMathJax( config ){
|
||||||
if( !Object.assign ){
|
if( !Object.assign ){
|
||||||
|
@ -34,11 +36,12 @@
|
||||||
}
|
}
|
||||||
useMathJax( JSON.parse({{ $init }}) );
|
useMathJax( JSON.parse({{ $init }}) );
|
||||||
</script>
|
</script>
|
||||||
{{- if and (isset .Params "custommathjaxurl") .Params.customMathJaxURL }}
|
{{- if and (isset .Params "custommathjaxurl") .Params.customMathJaxURL }}
|
||||||
<script id="MathJax-script" async src="{{ .Params.customMathJaxURL }}"></script>
|
<script id="MathJax-script" async src="{{ .Params.customMathJaxURL }}"></script>
|
||||||
{{- else if and (isset .Site.Params "custommathjaxurl") .Site.Params.customMathJaxURL }}
|
{{- else if and (isset .Site.Params "custommathjaxurl") .Site.Params.customMathJaxURL }}
|
||||||
<script id="MathJax-script" async src="{{ .Site.Params.customMathJaxURL }}"></script>
|
<script id="MathJax-script" async src="{{ .Site.Params.customMathJaxURL }}"></script>
|
||||||
{{- else }}
|
{{- else }}
|
||||||
<script id="MathJax-script" async src="{{"js/mathjax/tex-mml-chtml.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
|
<script id="MathJax-script" async src="{{"js/mathjax/tex-mml-chtml.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
|
||||||
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
|
@ -1,5 +1,7 @@
|
||||||
{{- $page := .page }}
|
{{- $page := .page }}
|
||||||
{{- with $page }}
|
{{- $location := .location }}
|
||||||
|
{{- if eq $location "footer" }}
|
||||||
|
{{- with $page }}
|
||||||
<script src="{{"js/d3/d3-color.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/d3/d3-color.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
<script src="{{"js/d3/d3-dispatch.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/d3/d3-dispatch.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
<script src="{{"js/d3/d3-drag.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/d3/d3-drag.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
|
@ -10,20 +12,21 @@
|
||||||
<script src="{{"js/d3/d3-transition.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/d3/d3-transition.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
<script src="{{"js/d3/d3-zoom.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/d3/d3-zoom.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
<script src="{{"js/js-yaml.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/js-yaml.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
{{- if and (isset .Params "custommermaidurl") .Params.customMermaidURL }}
|
{{- if and (isset .Params "custommermaidurl") .Params.customMermaidURL }}
|
||||||
<script src="{{ .Params.customMermaidURL }}" defer></script>
|
<script src="{{ .Params.customMermaidURL }}" defer></script>
|
||||||
{{- else if and (isset .Site.Params "custommermaidurl") .Site.Params.customMermaidURL }}
|
{{- else if and (isset .Site.Params "custommermaidurl") .Site.Params.customMermaidURL }}
|
||||||
<script src="{{ .Site.Params.customMermaidURL }}" defer></script>
|
<script src="{{ .Site.Params.customMermaidURL }}" defer></script>
|
||||||
{{- else }}
|
{{- else }}
|
||||||
<script src="{{"js/mermaid.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/mermaid.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- $init := "{}" }}
|
{{- $init := "{}" }}
|
||||||
{{- if isset .Params "mermaidinitialize" }}
|
{{- if isset .Params "mermaidinitialize" }}
|
||||||
{{- $init = .Params.mermaidInitialize }}
|
{{- $init = .Params.mermaidInitialize }}
|
||||||
{{- else if isset .Site.Params "mermaidinitialize" }}
|
{{- else if isset .Site.Params "mermaidinitialize" }}
|
||||||
{{- $init = .Site.Params.mermaidInitialize }}
|
{{- $init = .Site.Params.mermaidInitialize }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
<script>
|
<script>
|
||||||
window.themeUseMermaid = JSON.parse({{ $init }});
|
window.themeUseMermaid = JSON.parse({{ $init }});
|
||||||
</script>
|
</script>
|
||||||
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
|
@ -1,27 +1,30 @@
|
||||||
{{- $page := .page }}
|
{{- $page := .page }}
|
||||||
{{- with $page }}
|
{{- $location := .location }}
|
||||||
|
{{- if eq $location "footer" }}
|
||||||
|
{{- with $page }}
|
||||||
<script src="{{"js/js-yaml.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
<script src="{{"js/js-yaml.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
|
||||||
{{- $urlOpenapi := "" }}
|
{{- $urlOpenapi := "" }}
|
||||||
{{- $relOpenapi := "" }}
|
{{- $relOpenapi := "" }}
|
||||||
{{- $cssInProject := false }}
|
{{- $cssInProject := false }}
|
||||||
{{- if and (isset .Params "customopenapiurl") .Params.customOpenapiURL }}
|
{{- if and (isset .Params "customopenapiurl") .Params.customOpenapiURL }}
|
||||||
{{- $urlOpenapi = .Params.customOpenapiURL }}
|
{{- $urlOpenapi = .Params.customOpenapiURL }}
|
||||||
{{- $relOpenapi = .Params.customOpenapiURL }}
|
{{- $relOpenapi = .Params.customOpenapiURL }}
|
||||||
{{- else if and (isset .Site.Params "customopenapiurl") .Site.Params.customOpenapiURL }}
|
{{- else if and (isset .Site.Params "customopenapiurl") .Site.Params.customOpenapiURL }}
|
||||||
{{- $urlOpenapi = .Site.Params.customOpenapiURL }}
|
{{- $urlOpenapi = .Site.Params.customOpenapiURL }}
|
||||||
{{- $relOpenapi = .Site.Params.customOpenapiURL }}
|
{{- $relOpenapi = .Site.Params.customOpenapiURL }}
|
||||||
{{- else }}
|
{{- else }}
|
||||||
{{- $urlOpenapi = printf "%s%s" ("js/swagger-ui/swagger-ui-bundle.js" | relURL) (cond .Site.Params.disableAssetsBusting "" (printf "?%d" now.Unix)) }}
|
{{- $urlOpenapi = printf "%s%s" ("js/swagger-ui/swagger-ui-bundle.js" | relURL) (cond .Site.Params.disableAssetsBusting "" (printf "?%d" now.Unix)) }}
|
||||||
{{- $relOpenapi = printf "%s%s" ("/js/swagger-ui/swagger-ui-bundle.js") (cond .Site.Params.disableAssetsBusting "" (printf "?%d" now.Unix)) }}
|
{{- $relOpenapi = printf "%s%s" ("/js/swagger-ui/swagger-ui-bundle.js") (cond .Site.Params.disableAssetsBusting "" (printf "?%d" now.Unix)) }}
|
||||||
{{- $cssInProject = true }}
|
{{- $cssInProject = true }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
<script>window.noZensmooth = true;</script>
|
<script>window.noZensmooth = true;</script>
|
||||||
<script src="{{ $urlOpenapi }}" defer></script>
|
<script src="{{ $urlOpenapi }}" defer></script>
|
||||||
{{- $urlOpenapi := replace $urlOpenapi "swagger-ui-bundle" "swagger-ui-standalone-preset" }}
|
{{- $urlOpenapi := replace $urlOpenapi "swagger-ui-bundle" "swagger-ui-standalone-preset" }}
|
||||||
<script src="{{ $urlOpenapi }}" defer></script>
|
<script src="{{ $urlOpenapi }}" defer></script>
|
||||||
{{- $relOpenapi := replace $relOpenapi "swagger-ui-bundle" "swagger-ui" }}
|
{{- $relOpenapi := replace $relOpenapi "swagger-ui-bundle" "swagger-ui" }}
|
||||||
{{- $relOpenapi := replace $relOpenapi ".js" ".css" }}
|
{{- $relOpenapi := replace $relOpenapi ".js" ".css" }}
|
||||||
<script>
|
<script>
|
||||||
window.themeUseOpenapi = { css: {{ $relOpenapi }}, cssInProject: {{ $cssInProject | safeJS }}, assetsBuster: {{ cond (not .Site.Params.disableAssetsBusting) now.Unix 0 }} };
|
window.themeUseOpenapi = { css: {{ $relOpenapi }}, cssInProject: {{ $cssInProject | safeJS }}, assetsBuster: {{ cond (not .Site.Params.disableAssetsBusting) now.Unix 0 }} };
|
||||||
</script>
|
</script>
|
||||||
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
Loading…
Reference in a new issue