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

feat: add support for extended mermaid syntax #215

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion assets/js/theme.min.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,10 @@ enableEmoji = true
Skype = false
Trello = false
Mix = false
# Mermaid config
# Mermaid 设置
[params.page.mermaid]
enable = false
# Comment config
# 评论系统设置
[params.page.comment]
Expand Down
8 changes: 8 additions & 0 deletions exampleSite/content/posts/tests/mermaid-tests/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ hiddenFromSearch: true
C-->D;
{{< /mermaid >}}

```mermaid
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
```

{{< mermaid >}}sequenceDiagram
participant Alice
participant Bob
Expand Down
4 changes: 2 additions & 2 deletions layouts/shortcodes/mermaid.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{- $id := dict "Content" (trim .Inner "\n") "Scratch" .Page.Scratch | partial "function/id.html" -}}
<div class="mermaid" id="{{ $id }}"></div>
{{- $id := dict "Scratch" .Page.Scratch | partial "function/id.html" -}}
<div class="mermaid" id="{{ $id }}">{{ .Inner }}</div>
{{- .Page.Scratch.SetInMap "this" "mermaid" true -}}
32 changes: 30 additions & 2 deletions src/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,41 @@ function initMath() {
}

function initMermaid() {
var elementId = 1;
while (document.getElementById(`id-${elementId}`)) elementId++;

const mermaidKeywords = [
"graph TD",
"graph TB",
"graph BT",
"graph RL",
"graph LR",
"sequenceDiagram",
"gantt",
"classDiagram",
"gitGraph",
"erDiagram",
"journey",
];
Array.from(document.getElementsByClassName("language-fallback")).forEach((e) => {
let graphDefinition = e.innerText;
if (mermaidKeywords.some((keyword) => graphDefinition.startsWith(keyword))) {
let eN = e.parentElement.parentElement.parentElement.parentElement;
eN.parentElement.outerHTML = `<div class="mermaid" id="id-${elementId}">${e.innerText}</div>`;
}
});

Array.from(document.getElementsByClassName("language-mermaid")).forEach((e) => {
e.parentElement.outerHTML = `<div class="mermaid" id="id-${elementId}">${e.innerText}</div>`;
});

const $mermaidElements = document.getElementsByClassName('mermaid');
if ($mermaidElements.length) {
mermaid.initialize({ startOnLoad: false, theme: 'default' });
forEach($mermaidElements, $mermaid => {
mermaid.mermaidAPI.render('svg-' + $mermaid.id, window.data[$mermaid.id], svgCode => {
mermaid.mermaidAPI.render('svg-' + $mermaid.id, $mermaid.textContent, svgCode => {
$mermaid.insertAdjacentHTML('afterbegin', svgCode);
document.getElementById('svg-' + $mermaid.id).children[0].remove();
document.getElementById('svg-' + $mermaid.id).firstElementChild.remove();
}, $mermaid);
});
}
Expand Down