Skip to content

Commit

Permalink
[#66229] prefer comparing hash instead of code of mermaid diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin committed Nov 19, 2024
1 parent 8db9d2f commit 06fa5f5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/hooks/markdownMermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const markdownItMermaid = (md, { lineMap, parent }) => {
const code = token.content.trim();
const lineNumber = getLineById(lineMap.current, token.attrGet("data-line-id"));
let cached = lineCache.get(lineNumber);
if (!cached || cached.code !== code) {
cached = contentCache.get(new IMurMurHash(code, hashSeed).result()) ?? cached;
const hash = new IMurMurHash(code, hashSeed).result();
if (!cached || cached.hash !== hash) {
cached = contentCache.get(hash) ?? cached;
}
const id = Math.random().toString().replace(".", "");
token.attrSet("id", `mermaid-${id}`);
Expand All @@ -53,9 +54,9 @@ const markdownItMermaid = (md, { lineMap, parent }) => {
mermaid
.render(`mermaid-${id}`, code, container)
.then(({ svg }) => {
const saved = { svg, code };
const saved = { svg, code, hash };
lineCache.set(lineNumber, saved);
contentCache.set(new IMurMurHash(code, hashSeed).result(), saved);
contentCache.set(hash, saved);
el.innerHTML = svg;
el.className = "mermaid";
})
Expand Down

0 comments on commit 06fa5f5

Please sign in to comment.