Skip to content

Commit

Permalink
mermaid: load javascript on demand matcornic#218
Browse files Browse the repository at this point in the history
  • Loading branch information
McShelby committed Mar 16, 2022
1 parent 7836d31 commit dde1fe5
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[module]
[module.hugoVersion]
min = "0.93.0"
1 change: 0 additions & 1 deletion exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ title = "Hugo Relearn Documentation"
disableBreadcrumb = false
disableNextPrev = false
disableLandingPageButton = true
disableMermaid = false
titleSeparator = "::"
themeVariant = [ "relearn-light", "relearn-dark", "learn", "neon", "blue", "green", "red" ]
disableSeoHiddenPages = true
Expand Down
2 changes: 1 addition & 1 deletion exampleSite/content/basics/configuration/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Note that some of these parameters are explained in details in other sections of
disableBreadcrumb = true
# If set to true, hide table of contents menu in the header of all pages
disableToc = false
# If set to true, prevents Hugo from including the Mermaid module if not needed (will reduce load times and traffic)
# If set to false, load the Mermaid module on every page regardless if a Mermaid shortcode or Mermaid codefence is present
disableMermaid = false
# Specifies the remote location of the Mermaid js
customMermaidURL = "https://unpkg.com/[email protected]/dist/mermaid.min.js"
Expand Down
1 change: 1 addition & 0 deletions exampleSite/content/basics/generator/_index.en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
+++
disableMermaid = false
title = "Stylesheet generator"
weight = 26
+++
Expand Down
9 changes: 9 additions & 0 deletions exampleSite/content/basics/migration/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ This document shows you what's new in the latest release. For a detailed list of

---

## 3.2.0

- **Change**: In this release the Mermaid JavaScript library will only be loaded on demand if the page contains a Mermaid shortcode or is using Mermaid codefences. This changes the behavior of `disableMermaid` config option as follows: If a Mermaid shortcode or codefence is found, the option will be ignored and Mermaid will be loaded regardlessly.

The option is still useful in case you are using scripting to set up your graph. In this case no shortcode or codefence is involved and the library is not loaded by default. In this case you can set `disableMermaid=false` in your frontmatter to force the library to be loaded. See the [theme variant generator]({{%relref "basics/generator" %}}) of the exampleSite for an example.

**This change requires at least Hugo 0.93.0 to be used**. The minimum requirement will be reported during build on the console if not met.
---

## 3.1.0

- **New**: [`attachment`]({{% relref "shortcodes/attachments" %}}) and [`notice`]({{% relref "shortcodes/notice" %}}) shortcodes have a new parameter to override the default icon. Allowed values are all [Font Awesome 5 Free](https://fontawesome.com/v5/search?m=free) icons.
Expand Down
4 changes: 4 additions & 0 deletions layouts/_default/_markup/render-codeblock-mermaid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="mermaid">
{{- safeHTML .Inner -}}
</div>
{{- .Page.Store.Set "hasMermaid" true }}
10 changes: 5 additions & 5 deletions layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
{{- partial "menu.html" . }}
<script src="{{"js/clipboard.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/perfect-scrollbar.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/jquery.svg.pan.zoom.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/featherlight.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
{{- if (or (and (ne .Params.disableMermaid nil) (not .Params.disableMermaid)) (not .Site.Params.disableMermaid)) }}
{{- if (or (.Page.Store.Get "hasMermaid") (and (ne .Params.disableMermaid nil) (not .Params.disableMermaid)) (and (ne .Site.Params.disableMermaid nil) (not .Site.Params.disableMermaid)) ) }}
<script src="{{"js/jquery.svg.pan.zoom.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
{{- if isset .Params "custommermaidurl" }}
<script src="{{ .Params.customMermaidURL }}"></script>
{{- else if isset .Site.Params "custommermaidurl" }}
Expand All @@ -21,11 +21,11 @@
<script src="{{"js/mermaid.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
{{- end }}
{{- if isset .Params "mermaidinitialize" }}
{{- $.Scratch.Set "mermaidInitialize" .Params.mermaidInitialize }}
{{- $.Scratch.Set "mermaidInitialize" .Params.mermaidInitialize }}
{{- else if isset .Site.Params "mermaidinitialize" }}
{{- $.Scratch.Set "mermaidInitialize" .Site.Params.mermaidInitialize }}
{{- $.Scratch.Set "mermaidInitialize" .Site.Params.mermaidInitialize }}
{{- else }}
{{- $.Scratch.Set "mermaidInitialize" "{ \"startOnLoad\": true }" }}
{{- $.Scratch.Set "mermaidInitialize" "{ \"startOnLoad\": true }" }}
{{- end }}
<script>
if (typeof mermaid != 'undefined' && typeof mermaid.mermaidAPI != 'undefined') {
Expand Down
1 change: 1 addition & 0 deletions layouts/shortcodes/mermaid.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
<div class="mermaid" align="{{ if .Get "align" }}{{ .Get "align" }}{{ else }}center{{ end }}">
{{- safeHTML .Inner -}}
</div>
{{- .Page.Store.Set "hasMermaid" true }}
4 changes: 4 additions & 0 deletions theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ demosite = "https://mcshelby.github.io/hugo-theme-relearn"
tags = ["dark", "dark mode", "docs", "light", "multilingual", "responsive"]
features = ["dark mode", "documentation", "expand", "include", "light mode", "menu", "mermaid", "multilingual", "nested sections", "notice", "search", "tabs", "themeable"]

[module]
[module.hugoVersion]
min = "0.93.0"

[author]
name = "Sören Weber"
homepage = "https://github.com/McShelby"
Expand Down

0 comments on commit dde1fe5

Please sign in to comment.