-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclaude-mermaid-viewer.user.js
More file actions
688 lines (601 loc) · 28 KB
/
Copy pathclaude-mermaid-viewer.user.js
File metadata and controls
688 lines (601 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// ==UserScript==
// @name claude-mermaid-viewer
// @namespace https://github.com/sansan0/useful-userscripts
// @version 1.5
// @description 在 Claude 聊天界面中渲染和查看 Mermaid 图表的工具
// @author sansan
// @match https://claude.ai/*
// @grant GM.xmlHttpRequest
// @grant unsafeWindow
// @license GPL-3.0 License
// @icon data:image/png;base64,/9j/4AAQSkZJRgABAQEAqACoAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCACAAIADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTV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Jb1/UV2RzLFRVlUl97OKWV4OTu6UfuR5FF+xR4KRvmOtyD0a8A/korZ0f9ljwJozhl0FLpl73U0k36E4/SvRPJb/JFKYW/wAkUpZjipKzqS+9hDK8JF3jSj9yKWjeH7Hw3aeTp9naWEP9y3hWJT/3yKuBcUvlNn/64pfKb2/OuRtt3Z3JJKyG4704rigxNjt+dHlt/k0gAjihOH/A/wAqNjD/APWKVY23Z9j3oA//2Q==
// ==/UserScript==
(function () {
"use strict";
let initialScale = 1;
const styleSheet = document.createElement("style");
styleSheet.textContent = `
.mermaid-content-wrapper {
width: 100%;
height: calc(100% - 50px);
overflow: scroll;
padding: 20px;
box-sizing: border-box;
position: absolute;
top: 50px;
left: 0;
right: 0;
bottom: 0;
}
.mermaid-content-wrapper::-webkit-scrollbar {
width: 8px;
height: 8px;
display: block;
}
.mermaid-content-wrapper::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
.mermaid-content-wrapper::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
min-height: 40px;
}
.mermaid-content-wrapper::-webkit-scrollbar-thumb:hover {
background: #555;
}
.control-button {
width: 36px;
height: 36px;
padding: 6px;
border: none;
border-radius: 6px;
background-color: transparent;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s ease;
}
.control-button:hover {
background-color: #f3f4f6;
}
.control-button svg {
width: 24px;
height: 24px;
}
.control-button.active {
background-color: #4b5563;
}
.control-button.active svg {
stroke: #ffffff;
}
div.text-text-300.absolute.pl-3.pt-2\\.5.text-xs.mermaid-toggle {
opacity: 1 !important;
visibility: visible !important;
pointer-events: auto !important;
background-color: #334155 !important;
color: #ffffff !important;
display: flex !important;
align-items: center !important;
gap: 4px !important;
padding: 4px 8px !important;
border-radius: 4px !important;
font-size: 12px !important;
font-weight: 500 !important;
border: 1px solid transparent !important;
transition: all 0.2s ease-in-out !important;
cursor: pointer !important;
}
.mermaid-toggle svg {
width: 14px;
height: 14px;
stroke: currentColor;
stroke-width: 2;
}
div.text-text-500.text-xs.p-3\\.5.pb-0.mermaid-toggle {
display: flex !important;
align-items: center !important;
gap: 5px !important;
cursor: pointer !important;
}
div.mermaid-toggle span {
display: flex !important;
align-items: center !important;
justify-content: center !important;
}
div.mermaid-toggle svg {
width: 14px !important;
height: 14px !important;
stroke: currentColor !important;
stroke-width: 2 !important;
margin-right: 3px !important;
}
`;
document.head.appendChild(styleSheet);
/**
* 加载 Mermaid 库
* @param {function} callback - 加载完成后的回调函数
*/
function loadMermaidLibrary(callback) {
GM.xmlHttpRequest({
method: "GET",
url: "https://cdn.jsdelivr.net/npm/mermaid@11.4.1/dist/mermaid.min.js",
onload: function (response) {
const script = document.createElement("script");
script.textContent = response.responseText;
document.head.appendChild(script);
unsafeWindow.mermaid.initialize({
startOnLoad: false,
flowchart: {
htmlLabels: true,
wrappingWidth: 300,
padding: 20,
},
class: {
wrappingWidth: 300,
},
state: {
wrappingWidth: 300,
},
er: {
wrappingWidth: 300,
},
});
callback();
},
});
}
/**
* 在模态框中渲染 Mermaid 图
* @param {string} mermaidContent - Mermaid 图的内容
*/
function renderMermaidInModal(mermaidContent) {
const modal = document.createElement("div");
modal.style.position = "fixed";
modal.style.top = "0";
modal.style.left = "0";
modal.style.width = "100%";
modal.style.height = "100%";
modal.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
modal.style.zIndex = "9999";
modal.style.display = "flex";
modal.style.justifyContent = "center";
modal.style.alignItems = "center";
const container = document.createElement("div");
container.style.width = "90%";
container.style.height = "90%";
container.style.backgroundColor = "white";
container.style.boxSizing = "border-box";
container.style.position = "relative";
container.style.borderRadius = "8px";
container.style.boxShadow = "0 4px 6px rgba(0, 0, 0, 0.1)";
const contentWrapper = document.createElement("div");
contentWrapper.classList.add("mermaid-content-wrapper");
const buttonContainer = document.createElement("div");
buttonContainer.style.position = "absolute";
buttonContainer.style.top = "10px";
buttonContainer.style.right = "10px";
buttonContainer.style.height = "40px";
buttonContainer.style.display = "flex";
buttonContainer.style.gap = "4px";
buttonContainer.style.zIndex = "1";
const panButton = createControlButton(
"pan",
`
<path d="M20,14 L20,17 C20,19.209139 18.209139,21 16,21 L10.0216594,21 C8.75045497,21 7.55493392,20.3957659 6.80103128,19.3722467 L3.34541668,14.6808081 C2.81508416,13.9608139 2.94777982,12.950548 3.64605479,12.391928 C4.35756041,11.8227235 5.38335813,11.8798792 6.02722571,12.5246028 L8,14.5 L8,13 L8.00393081,13 L8,11 L8.0174523,6.5 C8.0174523,5.67157288 8.68902517,5 9.5174523,5 C10.3458794,5 11.0174523,5.67157288 11.0174523,6.5 L11.0174523,11 L11.0174523,4.5 C11.0174523,3.67157288 11.6890252,3 12.5174523,3 C13.3458794,3 14.0174523,3.67157288 14.0174523,4.5 L14.0174523,11 L14.0174523,5.5 C14.0174523,4.67157288 14.6890252,4 15.5174523,4 C16.3458794,4 17.0174523,4.67157288 17.0174523,5.5 L17.0174523,11 L17.0174523,7.5 C17.0174523,6.67157288 17.6890252,6 18.5174523,6 C19.3458794,6 20.0174523,6.67157288 20.0174523,7.5 L20.0058962,14 L20,14 Z"></path>
`
);
const zoomOutButton = createControlButton(
"zoom-out",
`
<path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
d="m21 21l-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607M13.5 10.5h-6"></path>
`
);
const resetButton = createControlButton("reset", "Reset", true);
const zoomInButton = createControlButton(
"zoom-in",
`
<path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
d="m21 21l-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607M10.5 7.5v6m3-3h-6"></path>
`
);
const downloadButton = createControlButton(
"download",
`
<path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
d="M12 10v6m0 0l-3-3m3 3l3-3M3 17v3a2 2 0 002 2h14a2 2 0 002-2v-3M14 3H8.5C7.67157 3 7 3.67157 7 4.5V6"></path>
`
);
const closeButton = createControlButton(
"close",
`
<path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
d="M6 18L18 6M6 6l12 12"></path>
`
);
closeButton.style.marginLeft = "8px";
let currentScale = 1;
const scaleStep = 0.2;
let isPanning = false;
let isEnabled = false;
panButton.addEventListener("click", togglePanMode);
togglePanMode();
let startX, startY, scrollLeft, scrollTop;
contentWrapper.addEventListener("mousedown", handleMouseDown);
contentWrapper.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", handleMouseUp);
contentWrapper.addEventListener("selectstart", handleSelectStart);
zoomInButton.addEventListener("click", zoomIn);
zoomOutButton.addEventListener("click", zoomOut);
resetButton.addEventListener("click", resetZoom);
closeButton.addEventListener("click", closeModal);
downloadButton.addEventListener("click", downloadAsPng);
buttonContainer.appendChild(panButton);
buttonContainer.appendChild(zoomOutButton);
buttonContainer.appendChild(resetButton);
buttonContainer.appendChild(zoomInButton);
buttonContainer.appendChild(downloadButton);
buttonContainer.appendChild(closeButton);
const mermaidElement = document.createElement("pre");
mermaidElement.classList.add("mermaid");
mermaidElement.textContent = mermaidContent;
mermaidElement.style.margin = "0";
mermaidElement.style.display = "inline-block";
mermaidElement.style.position = "relative";
mermaidElement.style.minWidth = "100%";
contentWrapper.appendChild(mermaidElement);
container.appendChild(buttonContainer);
container.appendChild(contentWrapper);
modal.appendChild(container);
document.body.appendChild(modal);
const observer = new MutationObserver(handleMutations);
observer.observe(mermaidElement, { childList: true });
unsafeWindow.mermaid.init(undefined, mermaidElement).then(() => {
resetZoom();
});
modal.addEventListener("click", handleModalClick);
function downloadAsPng() {
const contentWrapper = mermaidElement.closest(".mermaid-content-wrapper");
const scrollLeft = contentWrapper.scrollLeft;
const scrollTop = contentWrapper.scrollTop;
const svg = mermaidElement.querySelector("svg");
if (!svg) return;
const originalTransform = svg.style.transform;
const originalTransformOrigin = svg.style.transformOrigin;
svg.style.transform = "scale(1)";
const svgClone = svg.cloneNode(true);
if (
!svgClone.hasAttribute("viewBox") &&
svgClone.hasAttribute("width") &&
svgClone.hasAttribute("height")
) {
svgClone.setAttribute(
"viewBox",
`0 0 ${svgClone.getAttribute("width")} ${svgClone.getAttribute(
"height"
)}`
);
}
const padding = 20;
const bbox = svg.getBBox();
const width = Math.ceil(bbox.width + padding * 2);
const height = Math.ceil(bbox.height + padding * 2);
svgClone.setAttribute("width", width);
svgClone.setAttribute("height", height);
svgClone.setAttribute(
"viewBox",
`${bbox.x - padding} ${bbox.y - padding} ${width} ${height}`
);
const serializer = new XMLSerializer();
let source = serializer.serializeToString(svgClone);
if (
!source.match(/^<svg[^>]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/)
) {
source = source.replace(
/^<svg/,
'<svg xmlns="http://www.w3.org/2000/svg"'
);
}
source = '<?xml version="1.0" standalone="no"?>\r\n' + source;
const url =
"data:image/svg+xml;charset=utf-8," + encodeURIComponent(source);
const img = new Image();
img.onload = function () {
const scale = 2;
const canvas = document.createElement("canvas");
canvas.width = width * scale;
canvas.height = height * scale;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#ffffff"; // 白色背景
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.scale(scale, scale);
ctx.drawImage(img, 0, 0);
const pngUrl = canvas.toDataURL("image/png");
const link = document.createElement("a");
link.href = pngUrl;
link.download = `mermaid-diagram-${Date.now()}.png`;
link.style.display = "none";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
svg.style.transform = originalTransform;
svg.style.transformOrigin = originalTransformOrigin;
contentWrapper.scrollLeft = scrollLeft;
contentWrapper.scrollTop = scrollTop;
};
img.src = url;
}
/**
* 创建控制按钮
* @param {string} className - 按钮的类名
* @param {string} content - 按钮的内容(SVG 或文本)
* @param {boolean} isText - 是否为文本内容
* @returns {HTMLButtonElement} - 创建的按钮元素
*/
function createControlButton(className, content, isText = false) {
const button = document.createElement("button");
button.className = `control-button ${className}`;
if (isText) {
button.textContent = content;
button.style.fontSize = "12px";
} else {
button.innerHTML = `<svg viewBox="0 0 24 24" role="img" xmlns="http://www.w3.org/2000/svg" style="display: inline-block;" width="24" height="24" fill="none" stroke="currentColor">${content}</svg>`;
}
return button;
}
/**
* 切换平移模式
*/
function togglePanMode() {
isEnabled = !isEnabled;
panButton.classList.toggle("active", isEnabled);
contentWrapper.style.cursor = isEnabled ? "grab" : "default";
}
/**
* 处理鼠标按下事件
* @param {MouseEvent} e - 鼠标事件对象
*/
function handleMouseDown(e) {
if (!isEnabled) return;
isPanning = true;
contentWrapper.style.cursor = "grabbing";
startX = e.pageX - contentWrapper.offsetLeft;
startY = e.pageY - contentWrapper.offsetTop;
scrollLeft = contentWrapper.scrollLeft;
scrollTop = contentWrapper.scrollTop;
}
/**
* 处理鼠标移动事件
* @param {MouseEvent} e - 鼠标事件对象
*/
function handleMouseMove(e) {
if (!isPanning) return;
e.preventDefault();
const x = e.pageX - contentWrapper.offsetLeft;
const y = e.pageY - contentWrapper.offsetTop;
const walkX = (x - startX) * 1.5;
const walkY = (y - startY) * 1.5;
contentWrapper.scrollLeft = scrollLeft - walkX;
contentWrapper.scrollTop = scrollTop - walkY;
}
/**
* 处理鼠标松开事件
*/
function handleMouseUp() {
isPanning = false;
if (isEnabled) {
contentWrapper.style.cursor = "grab";
}
}
/**
* 处理选择开始事件
* @param {Event} e - 事件对象
*/
function handleSelectStart(e) {
if (isEnabled) {
e.preventDefault();
}
}
/**
* 更新 SVG 缩放
*/
function updateSvgScale() {
const svg = mermaidElement.querySelector("svg");
if (svg) {
svg.style.transform = `scale(${currentScale})`;
svg.style.transformOrigin = "top left";
const boundingRect = svg.getBoundingClientRect();
const scaledWidth = boundingRect.width;
const scaledHeight = boundingRect.height;
mermaidElement.style.width = `${scaledWidth}px`;
mermaidElement.style.height = `${scaledHeight}px`;
mermaidElement.style.margin = "20px";
const wrapperHeight = contentWrapper.clientHeight;
const centerY = (wrapperHeight - scaledHeight) / 2;
contentWrapper.scrollTop = centerY;
}
}
/**
* 放大
*/
function zoomIn() {
currentScale += scaleStep;
updateSvgScale();
}
/**
* 缩小
*/
function zoomOut() {
currentScale = Math.max(0.2, currentScale - scaleStep);
updateSvgScale();
}
/**
* 重置缩放
*/
function resetZoom() {
currentScale = initialScale;
const svg = mermaidElement.querySelector("svg");
svg.style.transform = "";
mermaidElement.style.width = "";
mermaidElement.style.height = "";
setTimeout(() => {
updateSvgScale();
setTimeout(() => {
const centerX =
(contentWrapper.scrollWidth - contentWrapper.clientWidth) / 2;
const centerY =
(contentWrapper.scrollHeight - contentWrapper.clientHeight) / 2;
contentWrapper.scrollLeft = centerX;
contentWrapper.scrollTop = centerY;
}, 0);
}, 0);
}
/**
* 关闭模态框
*/
function closeModal() {
modal.remove();
}
/**
* 处理 Mutation
* @param {MutationRecord[]} mutations - Mutation 记录数组
*/
function handleMutations(mutations) {
mutations.forEach((mutation) => {
if (mutation.type === "childList") {
const addedNodes = mutation.addedNodes;
for (const node of addedNodes) {
if (node.nodeName === "svg") {
const svg = node;
const boundingRect = svg.getBoundingClientRect();
const svgWidth = boundingRect.width;
const svgHeight = boundingRect.height;
const wrapperWidth = contentWrapper.clientWidth;
const wrapperHeight = contentWrapper.clientHeight;
const scaleX = wrapperWidth / svgWidth;
const scaleY = wrapperHeight / svgHeight;
initialScale = Math.min(scaleX, scaleY) * 0.95;
updateSvgScale();
const centerX =
(contentWrapper.scrollWidth - contentWrapper.clientWidth) / 2;
const centerY =
(contentWrapper.scrollHeight - contentWrapper.clientHeight) / 2;
contentWrapper.scrollLeft = centerX;
contentWrapper.scrollTop = centerY;
observer.disconnect();
break;
}
}
}
});
}
/**
* 处理模态框点击事件
* @param {MouseEvent} event - 鼠标事件对象
*/
function handleModalClick(event) {
if (event.target === modal) {
modal.remove();
}
}
}
/**
* 获取 Mermaid 内容
* @param {HTMLElement} element - 元素
* @returns {string} - Mermaid 内容
*/
function getMermaidContent(element) {
const codeElement = element
.closest("div")
.nextElementSibling.querySelector("code.language-mermaid");
if (!codeElement) {
console.error("Mermaid code element not found");
return "";
}
return extractMermaidSyntax(codeElement);
}
/**
* 提取 Mermaid 语法
* @param {HTMLElement} codeElement - 代码元素
* @returns {string} - Mermaid 语法
*/
function extractMermaidSyntax(codeElement) {
if (!codeElement) {
console.error("Code element not found");
return "";
}
try {
let rawText = "";
rawText = codeElement.textContent;
const lines = rawText.split("\n");
// 处理每一行,移除多余空白字符和特殊Unicode字符
const cleanedLines = lines
.map(
(line) =>
line
.replace(/[\u200B-\u200D\uFEFF]/g, "") // 移除零宽字符
.replace(/\t/g, " ") // 制表符替换为空格
.trimEnd() // 移除行尾空格
)
.filter((line) => line.trim() !== ""); // 过滤空行
const mermaidSyntax = cleanedLines.join("\n");
console.log("Extracted Mermaid Syntax:");
console.log(mermaidSyntax);
return mermaidSyntax;
} catch (error) {
console.error("Error extracting Mermaid syntax:", error);
return "";
}
}
/**
* 处理元素
*/
function processElements() {
const observer = new MutationObserver((mutations) => {
mutations.forEach(() => {
const elements = document.querySelectorAll(
"div.text-text-500.text-xs.p-3\\.5.pb-0[style*='margin-bottom: 20px;']"
);
elements.forEach((element) => {
if (
element.textContent.trim() === "mermaid" &&
!element.classList.contains("mermaid-toggle")
) {
element.classList.add("mermaid-toggle");
const icon = document.createElement("span");
icon.style.display = "inline-flex";
icon.style.alignItems = "center";
icon.innerHTML = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4M10 17l5-5-5-5M13.8 12H3"/>
</svg>`;
element.innerHTML = "";
element.appendChild(icon);
element.appendChild(document.createTextNode("mermaid"));
element.style.cursor = "pointer";
element.style.transition = "opacity 0.2s ease";
element.addEventListener("mouseenter", () => {
element.style.opacity = "0.8";
});
element.addEventListener("mouseleave", () => {
element.style.opacity = "1";
});
element.addEventListener("click", () => {
const mermaidContent = getMermaidContent(element);
renderMermaidInModal(mermaidContent);
});
}
});
});
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
}
loadMermaidLibrary(() => {
processElements();
window.addEventListener("load", processElements);
});
})();