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

Mermaid uses ESM modules now. Update so mermaid loads again. #775

Merged
merged 1 commit into from
Mar 7, 2023
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
35 changes: 15 additions & 20 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,26 @@ body.dark, :root[data-theme="dark"] {
*/
function createScript(version: string): string {
return String.raw`
<script src="https://unpkg.com/mermaid@${version}/dist/mermaid.min.js"></script>
<script>
(function() {
if (typeof mermaid === "undefined") {
return;
}

document.documentElement.classList.add("mermaid-enabled");
<script type="module">
import mermaid from "https://unpkg.com/mermaid@${version}/dist/mermaid.esm.min.mjs";

mermaid.initialize({startOnLoad:true});
document.documentElement.classList.add("mermaid-enabled");

requestAnimationFrame(function check() {
let some = false;
document.querySelectorAll("div.mermaid:not([data-inserted])").forEach(div => {
some = true;
if (div.querySelector("svg")) {
div.dataset.inserted = true;
}
});
mermaid.initialize({startOnLoad:true});

if (some) {
requestAnimationFrame(check);
requestAnimationFrame(function check() {
let some = false;
document.querySelectorAll("div.mermaid:not([data-inserted])").forEach(div => {
some = true;
if (div.querySelector("svg")) {
div.dataset.inserted = true;
}
});
})();

if (some) {
requestAnimationFrame(check);
}
});
</script>
`;
}
Expand Down
35 changes: 15 additions & 20 deletions test/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,26 @@ body.dark, :root[data-theme=\\"dark\\"] {
}
</style>
</head><body><div class=\\"mermaid-block\\"></div>
<script src=\\"https://unpkg.com/mermaid@latest/dist/mermaid.min.js\\"></script>
<script>
(function() {
if (typeof mermaid === \\"undefined\\") {
return;
}

document.documentElement.classList.add(\\"mermaid-enabled\\");
<script type=\\"module\\">
import mermaid from \\"https://unpkg.com/mermaid@latest/dist/mermaid.esm.min.mjs\\";

mermaid.initialize({startOnLoad:true});
document.documentElement.classList.add(\\"mermaid-enabled\\");

requestAnimationFrame(function check() {
let some = false;
document.querySelectorAll(\\"div.mermaid:not([data-inserted])\\").forEach(div => {
some = true;
if (div.querySelector(\\"svg\\")) {
div.dataset.inserted = true;
}
});
mermaid.initialize({startOnLoad:true});

if (some) {
requestAnimationFrame(check);
requestAnimationFrame(function check() {
let some = false;
document.querySelectorAll(\\"div.mermaid:not([data-inserted])\\").forEach(div => {
some = true;
if (div.querySelector(\\"svg\\")) {
div.dataset.inserted = true;
}
});
})();

if (some) {
requestAnimationFrame(check);
}
});
</script>
</body>"
`;
2 changes: 1 addition & 1 deletion test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('MermaidPlugin', () => {
const result = plugin.insertMermaidScript(input);
expect(result).toMatch('</body>');
expect(result).toMatch('mermaid.initialize({');
expect(result).toMatch(/src="https:\/\/unpkg.com\/mermaid\@latest\/dist\/mermaid.min.js"/);
expect(result).toMatch(/import mermaid from "https:\/\/unpkg.com\/mermaid\@latest\/dist\/mermaid.esm.min.mjs";/);
expect(result).toMatchSnapshot();
});

Expand Down