-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester <steven.silvester@ieee.org>
- Loading branch information
1 parent
4a9d589
commit 3dd3a67
Showing
7 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
{%- macro mermaid_js( | ||
url="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.0.2/mermaid.esm.min.mjs" | ||
) -%} | ||
<script type="module"> | ||
document.addEventListener("DOMContentLoaded", async () => { | ||
const diagrams = document.querySelectorAll(".jp-Mermaid > pre.mermaid"); | ||
// do not load mermaidjs if not needed | ||
if (!diagrams.length) { | ||
return; | ||
} | ||
const mermaid = (await import("{{ url }}")).default; | ||
mermaid.initialize({ | ||
maxTextSize: 100000, | ||
startOnLoad: false, | ||
fontFamily: window | ||
.getComputedStyle(document.body) | ||
.getPropertyValue("--jp-ui-font-family"), | ||
theme: document.querySelector("body[data-jp-theme-light='true']") | ||
? "default" | ||
: "dark", | ||
}); | ||
let _nextMermaidId = 0; | ||
function makeMermaidImage(svg) { | ||
const img = document.createElement('img'); | ||
const maxWidth = svg.match(/max-width: (\d+)/); | ||
if (maxWidth && maxWidth[1]) { | ||
const width = parseInt(maxWidth[1]); | ||
if (width && !Number.isNaN(width) && Number.isFinite(width)) { | ||
img.width = width; | ||
} | ||
} | ||
img.setAttribute('src', `data:image/svg+xml,${encodeURIComponent(svg)}`); | ||
return img; | ||
} | ||
async function makeMermaidError(text) { | ||
let errorMessage = ''; | ||
try { | ||
await mermaid.parse(text); | ||
} catch (err) { | ||
errorMessage = `${err}`; | ||
} | ||
const result = document.createElement('details'); | ||
const summary = document.createElement('summary'); | ||
const pre = document.createElement('pre'); | ||
const code = document.createElement('code'); | ||
code.innerText = text; | ||
pre.appendChild(code); | ||
summary.appendChild(pre); | ||
result.appendChild(summary); | ||
const warning = document.createElement('pre'); | ||
warning.innerText = errorMessage; | ||
result.appendChild(warning); | ||
return result; | ||
} | ||
async function renderOneMarmaid(src) { | ||
const id = `jp-mermaid-${_nextMermaidId++}`; | ||
const parent = src.parentNode; | ||
let raw = src.textContent.trim(); | ||
const el = document.createElement("div"); | ||
el.style.visibility = "hidden"; | ||
document.body.appendChild(el); | ||
let result = null; | ||
try { | ||
const { svg } = await mermaid.render(id, raw, el); | ||
result = makeMermaidImage(svg); | ||
} catch (err) { | ||
parent.classList.add("jp-mod-warning"); | ||
result = await makeMermaidError(raw); | ||
} finally { | ||
el.remove(); | ||
} | ||
parent.classList.add("jp-RenderedMermaid"); | ||
parent.appendChild(result); | ||
} | ||
void Promise.all([...diagrams].map(renderOneMarmaid)); | ||
}); | ||
</script> | ||
<style> | ||
.jp-RenderedMarkdown .jp-Mermaid:not(.jp-RenderedMermaid) { | ||
display: none; | ||
} | ||
.jp-RenderedMarkdown .jp-RenderedMermaid.jp-mod-warning { | ||
width: auto; | ||
padding: 10px; | ||
border: var(--jp-border-width) solid var(--jp-warn-color2); | ||
border-radius: var(--jp-border-radius); | ||
color: var(--jp-ui-font-color1); | ||
font-size: var(--jp-ui-font-size1); | ||
white-space: pre-wrap; | ||
word-wrap: break-word; | ||
} | ||
.jp-RenderedMarkdown .jp-RenderedMermaid.jp-mod-warning details > pre { | ||
margin-top: 1em; | ||
} | ||
.jp-RenderedMarkdown .jp-RenderedMermaid.jp-mod-warning summary { | ||
color: var(--jp-warn-color2); | ||
} | ||
.jp-RenderedMarkdown .jp-RenderedMermaid.jp-mod-warning summary > pre { | ||
display: inline-block; | ||
} | ||
.jp-RenderedMermaid > .mermaid { | ||
display: none; | ||
} | ||
</style> | ||
<!-- End of mermaid configuration --> | ||
{%- endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters