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

Support for Mermaid JS in Markdown content #1393

Open
colinbruner opened this issue Jan 16, 2024 · 3 comments
Open

Support for Mermaid JS in Markdown content #1393

colinbruner opened this issue Jan 16, 2024 · 3 comments
Labels
ext-resources Uses external resources (js/css) from CDNs

Comments

@colinbruner
Copy link

Hi,

Hugo's documentation supports Mermaid Diagrams via Mermaid JS and I was hoping we could get this added to the theme.

I was able to get this working locally with the diff below. I'm pretty new to Hugo, so I'm unsure if this is the best place to add this functionality (specifically within single.html).

diff --git a/layouts/_default/_markup/render-codeblock-mermaid.html b/layouts/_default/_markup/render-codeblock-mermaid.html
new file mode 100644
index 0000000..7e12ad7
--- /dev/null
+++ b/layouts/_default/_markup/render-codeblock-mermaid.html
@@ -0,0 +1,4 @@
+<pre class="mermaid">
+    {{- .Inner | safeHTML }}
+</pre>
+  {{ .Page.Store.Set "hasMermaid" true }}
\ No newline at end of file
diff --git a/layouts/_default/single.html b/layouts/_default/single.html
index 19a624f..860fe56 100644
--- a/layouts/_default/single.html
+++ b/layouts/_default/single.html
@@ -42,6 +42,13 @@
   </div>
   {{- end }}
 
+  {{ if .Page.Store.Get "hasMermaid" }}
+  <script type="module">
+    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
+    mermaid.initialize({ startOnLoad: true });
+  </script>
+  {{ end }}
+
   <footer class="post-footer">
     {{- $tags := .Language.Params.Taxonomies.tag | default "tags" }}
     <ul class="post-tags">

I'll create a PR shortly for review, would appreciate any feedback or guidance!

@colinbruner
Copy link
Author

**NOTE**: PaperMod does not have any external dependencies fetched from 3rd party
CDN servers. However we do have custom Head/Footer extender templates which you can use
to add those to your website.
https://github.com/adityatelange/hugo-PaperMod/wiki/FAQs#custom-head--footer

I did see this note when opening a PR, I'm going to explore this avenue. I do appreciate the desire not to bake in fetching things from 3rd party CDN servers to the greater theme.

@colinbruner
Copy link
Author

Okay, I was able to get this working with the custom footer approach.. although I don't seem to have access to any values in .Page.Store.. will look for a different way of conditionally rendering it as I wouldn't want to pull this script from the CDN on Markdown pages that do not use Mermaid.js

@colinbruner
Copy link
Author

It looks like we might need the functionality described in #1356 to more selectively add code under .Content like Mermaid.js

@adityatelange adityatelange added the ext-resources Uses external resources (js/css) from CDNs label Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ext-resources Uses external resources (js/css) from CDNs
Projects
None yet
Development

No branches or pull requests

2 participants