Skip to content

Commit 979d9a4

Browse files
tommoorclaude
andauthored
Mermaid improvements (outline#11874)
* fix: Upgrade mermaid to 11.13.0 Includes a fix for incorrect viewBox casing in Radar and Packet diagram renderers (mermaid-js/mermaid#7076) and other improvements. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: Use visibility:hidden for mermaid rendering element Instead of positioning the temporary render element offscreen at -9999px, use visibility:hidden with position:fixed so the browser computes correct bounding boxes for SVG elements. Offscreen elements can produce incorrect getBBox() results, leading to wrong viewBox dimensions and diagrams rendering too big or too small. Fixes outline#11782 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Add session storage for generated diagrams to reduce relayout * fix: Use LRU eviction for mermaid sessionStorage cache Track access order via a dedicated LRU index key so the cache evicts least-recently-used entries rather than arbitrary ones. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c2ccdb6 commit 979d9a4

5 files changed

Lines changed: 207 additions & 102 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
"markdown-it": "^14.1.0",
170170
"markdown-it-container": "^3.0.0",
171171
"markdown-it-emoji": "^3.0.0",
172-
"mermaid": "11.12.1",
172+
"mermaid": "11.13.0",
173173
"mime-types": "^3.0.1",
174174
"mobx": "^4.15.4",
175175
"mobx-react": "^6.3.1",

shared/editor/extensions/Mermaid.ts

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { findParentNode } from "../queries/findParentNode";
1515
import type { NodeWithPos } from "../types";
1616
import type { Editor } from "../../../app/editor";
1717
import { LightboxImageFactory } from "../lib/Lightbox";
18+
import { hashString } from "../../utils/string";
1819
import { sanitizeUrl } from "../../utils/urls";
1920

2021
export const pluginKey = new PluginKey("mermaid");
@@ -25,21 +26,72 @@ export type MermaidState = {
2526
editingId?: string;
2627
};
2728

29+
const STORAGE_PREFIX = "mermaid:";
30+
const MAX_STORAGE_ENTRIES = 20;
31+
2832
class Cache {
29-
static get(key: string) {
30-
return this.data.get(key);
33+
/** Get a cached SVG by diagram text and theme. */
34+
static get(key: string): string | undefined {
35+
try {
36+
const hash = hashString(key);
37+
const value = sessionStorage.getItem(STORAGE_PREFIX + hash);
38+
if (value) {
39+
this.touchLru(hash);
40+
return value;
41+
}
42+
} catch {
43+
// sessionStorage unavailable
44+
}
45+
return undefined;
3146
}
3247

48+
/** Cache a rendered SVG in sessionStorage. */
3349
static set(key: string, value: string) {
34-
this.data.set(key, value);
50+
try {
51+
const hash = hashString(key);
52+
this.touchLru(hash);
53+
this.pruneStorage();
54+
sessionStorage.setItem(STORAGE_PREFIX + hash, value);
55+
} catch {
56+
// sessionStorage full or unavailable
57+
}
58+
}
3559

36-
if (this.data.size > this.maxSize) {
37-
this.data.delete(this.data.keys().next().value);
60+
/** Move or append a hash to the end (most recent) of the LRU list. */
61+
private static touchLru(hash: string) {
62+
const lru = this.getLru();
63+
const idx = lru.indexOf(hash);
64+
if (idx !== -1) {
65+
lru.splice(idx, 1);
3866
}
67+
lru.push(hash);
68+
sessionStorage.setItem(STORAGE_PREFIX + "lru", JSON.stringify(lru));
3969
}
4070

41-
private static maxSize = 20;
42-
private static data: Map<string, string> = new Map();
71+
/** Evict least-recently-used entries when over the limit. */
72+
private static pruneStorage() {
73+
const lru = this.getLru();
74+
75+
while (lru.length > MAX_STORAGE_ENTRIES) {
76+
const evict = lru.shift()!;
77+
sessionStorage.removeItem(STORAGE_PREFIX + evict);
78+
}
79+
80+
sessionStorage.setItem(STORAGE_PREFIX + "lru", JSON.stringify(lru));
81+
}
82+
83+
/** Read the LRU order list from sessionStorage. */
84+
private static getLru(): string[] {
85+
try {
86+
const raw = sessionStorage.getItem(STORAGE_PREFIX + "lru");
87+
if (raw) {
88+
return JSON.parse(raw);
89+
}
90+
} catch {
91+
// corrupted or unavailable
92+
}
93+
return [];
94+
}
4395
}
4496

4597
let mermaid: typeof MermaidUnsafe;
@@ -104,16 +156,20 @@ class MermaidRenderer {
104156
return;
105157
}
106158

107-
// Create a temporary element that will render the diagram off-screen. This is necessary
108-
// as Mermaid will error if the element is not visible or the element is removed while the
109-
// diagram is being rendered.
159+
// Create a temporary element for rendering. We use visibility:hidden instead of
160+
// offscreen positioning so the browser computes correct bounding boxes for SVG
161+
// elements — offscreen elements can produce incorrect getBBox() results, leading
162+
// to wrong viewBox dimensions (see mermaid-js/mermaid#6146).
110163
const renderElement = document.createElement("div");
111164
const tempId =
112165
"offscreen-mermaid-" + Math.random().toString(36).substr(2, 9);
113166
renderElement.id = tempId;
114-
renderElement.style.position = "absolute";
115-
renderElement.style.left = "-9999px";
116-
renderElement.style.top = "-9999px";
167+
renderElement.style.position = "fixed";
168+
renderElement.style.visibility = "hidden";
169+
renderElement.style.top = "0";
170+
renderElement.style.left = "0";
171+
renderElement.style.width = "100%";
172+
renderElement.style.zIndex = "-1";
117173
document.body.appendChild(renderElement);
118174

119175
try {

shared/utils/string.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { hashString } from "./string";
2+
3+
describe("hashString", () => {
4+
it("returns a hex string", () => {
5+
expect(hashString("hello")).toMatch(/^[0-9a-f]+$/);
6+
});
7+
8+
it("returns consistent results for the same input", () => {
9+
expect(hashString("test")).toBe(hashString("test"));
10+
});
11+
12+
it("returns different hashes for different inputs", () => {
13+
expect(hashString("abc")).not.toBe(hashString("def"));
14+
});
15+
16+
it("handles empty string", () => {
17+
expect(hashString("")).toMatch(/^[0-9a-f]+$/);
18+
});
19+
});

shared/utils/string.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/**
2+
* Simple string hash using the djb2 algorithm, returns a hex string.
3+
*
4+
* @param str the string to hash.
5+
* @returns a hex-encoded 32-bit hash.
6+
*/
7+
export function hashString(str: string): string {
8+
let hash = 5381;
9+
for (let i = 0; i < str.length; i++) {
10+
hash = ((hash << 5) + hash + str.charCodeAt(i)) | 0;
11+
}
12+
return (hash >>> 0).toString(16);
13+
}
14+
115
/**
216
* Returns the index of the first occurrence of a substring in a string that matches a regular expression.
317
*

0 commit comments

Comments
 (0)