Skip to content

Instantly share code, notes, and snippets.

@ftes
Created July 11, 2025 16:57
Show Gist options
  • Save ftes/50d60107043093ba3624cae38c5dffaf to your computer and use it in GitHub Desktop.
Save ftes/50d60107043093ba3624cae38c5dffaf to your computer and use it in GitHub Desktop.
reveal.js + mermaid.js: Fix diagram size, text clipping, lazy loading
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>Let it Crash with Elixir</title>
<link rel="stylesheet" href="dist/reset.css" />
<link rel="stylesheet" href="dist/reveal.css" />
<link rel="stylesheet" href="dist/theme/night.css" />
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css" />
<style>
.mermaid svg {
max-width: initial !important;
max-height: 100%;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>Diagram on next slide</section>
<section>
<pre class="mermaid r-stretch">
sequenceDiagram
create participant c1
main->>c1: spawn
create participant c2
main->>c2: spawn
main->>c1: {:increment, main}
main->>c2: :crash
destroy c2
main--xc2: crashed
c1->>main: {:count, 2}
</pre>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<!-- Allow caching (type="module" doesn't cache?) -->
<script src="https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js"></script>
<script>
Reveal.initialize({
startOnLoad: false,
hash: true,
plugins: [RevealMarkdown, RevealHighlight, RevealNotes],
});
mermaid.initialize({
startOnLoad: false,
theme: "dark",
securityLevel: "loose",
});
Reveal.on("slidechanged", async (event) => {
const selector = `.present .mermaid`;
const els = document.querySelectorAll(selector);
const scale = Reveal.getScale();
const invertScale = 1 / scale;
// Temporarily reset diagram scale so that mermaid layout alg runs normally
els.forEach((el) => {
el.style.transform = `scale(${invertScale})`;
});
await mermaid.run({ querySelector: selector });
els.forEach((el) => {
el.style.transform = "";
});
Reveal.layout();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment