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

adding mermaidjs shortcode with example #20434

Merged
merged 1 commit into from
Apr 22, 2020
Merged
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
55 changes: 55 additions & 0 deletions content/en/docs/test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Docs smoke test page
main_menu: false
mermaid: true
---

This page serves two purposes:
Expand Down Expand Up @@ -295,6 +296,60 @@ tables, use HTML instead.
</tbody>
</table>

## Visualizations with Mermaid

Add `mermaid: true` to the [front matter](https://gohugo.io/content-management/front-matter/) of any page to enable [Mermaid JS](https://mermaidjs.github.io) visualizations. The Mermaid JS version is specified in [/layouts/partials/head.html](https://github.com/kubernetes/website/blob/master/layouts/partials/head.html)

```
{{</* mermaid */>}}
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
{{</*/ mermaid */>}}
```

Produces:

{{< mermaid >}}
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
{{</ mermaid >}}

```
{{</* mermaid */>}}
sequenceDiagram
Alice ->> Bob: Hello Bob, how are you?
Bob-->>John: How about you John?
Bob--x Alice: I am good thanks!
Bob-x John: I am good thanks!
Note right of John: Bob thinks a long<br/>long time, so long<br/>that the text does<br/>not fit on a row.

Bob-->Alice: Checking with John...
Alice->John: Yes... John, how are you?
{{</*/ mermaid */>}}
```

Produces:

{{< mermaid >}}
sequenceDiagram
Alice ->> Bob: Hello Bob, how are you?
Bob-->>John: How about you John?
Bob--x Alice: I am good thanks!
Bob-x John: I am good thanks!
Note right of John: Bob thinks a long<br/>long time, so long<br/>that the text does<br/>not fit on a row.

Bob-->Alice: Checking with John...
Alice->John: Yes... John, how are you?
{{</ mermaid >}}

<br>More [examples](https://mermaid-js.github.io/mermaid/#/examples) from the offical docs.

## Sidebars and Admonitions

Sidebars and admonitions provide ways to add visual importance to text. Use
Expand Down
5 changes: 5 additions & 0 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<script src="{{ "js/bootstrap-4.3.1.min.js" | relURL }}"></script>
<script src="{{ "js/sweetalert-2.1.2.min.js" | relURL }}"></script>

{{ if eq .Params.mermaid true }}
<!-- Copied from https://unpkg.com/mermaid@8.5.0/dist/mermaid.min.js -->
<script async src="{{ "js/mermaid.min.js" | relURL }}"></script>
{{ end }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jimangel , Question:
Do you know how the field .Params.js is used?

# config.toml
js = [
  "custom-jekyll/tags",
  "script"
]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no clue. It looks like it does the same thing twice (explicitly first then in shortcode after:

<script src="{{ "js/script.js" | relURL }}"></script>
<script src="{{ "js/custom-jekyll/tags.js" | relURL }}"></script>
{{ with .Params.js }}{{ range (split . ",") }}<script src="{{ (trim . " ") | relURL }}"></script><!-- custom js added -->


<script src="{{ "js/script.js" | relURL }}"></script>
<script src="{{ "js/custom-jekyll/tags.js" | relURL }}"></script>
{{ with .Params.js }}{{ range (split . ",") }}<script src="{{ (trim . " ") | relURL }}"></script><!-- custom js added -->
Expand Down
10 changes: 10 additions & 0 deletions layouts/shortcodes/mermaid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="mermaid">
{{.Inner}}
</div>
<!-- Hide content and error if JS is disabled. -->
<noscript>
<style type="text/css">
.mermaid { display:none; }
</style>
<h4>[JavaScript must be <a href="https://www.enable-javascript.com/">enabled</a> to view content]</h4>
</noscript>
49 changes: 49 additions & 0 deletions static/js/mermaid.min.js

Large diffs are not rendered by default.