@affanhamid/markdown-renderer 2.3.3 → 2.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -920,6 +920,7 @@ function renderMarkdownToHtml(markdown, options) {
920
920
  }
921
921
  return `<div class="prose max-w-none">${parts.join("")}</div>`;
922
922
  }
923
+ var mermaidInstance = null;
923
924
  var MarkdownRenderer = ({
924
925
  markdown,
925
926
  className,
@@ -1068,40 +1069,37 @@ var MarkdownRenderer = ({
1068
1069
  );
1069
1070
  };
1070
1071
  }, [html, handleRun]);
1071
- const mermaidReady = (0, import_react.useRef)(null);
1072
1072
  (0, import_react.useEffect)(() => {
1073
1073
  const container = containerRef.current;
1074
1074
  if (!container) return;
1075
- const mermaidBlocks = container.querySelectorAll(".md-mermaid");
1076
- if (mermaidBlocks.length === 0) return;
1077
1075
  let alive = true;
1078
- if (!mermaidReady.current) {
1079
- mermaidReady.current = import("mermaid").then((mod) => {
1080
- const mermaid = mod.default;
1081
- mermaid.initialize({
1082
- startOnLoad: false,
1083
- securityLevel: "antiscript",
1084
- theme: "neutral",
1085
- fontFamily: "ui-sans-serif, system-ui, sans-serif",
1086
- fontSize: 13,
1087
- htmlLabels: false,
1088
- flowchart: { useMaxWidth: true, htmlLabels: false },
1089
- sequence: { useMaxWidth: true }
1090
- });
1091
- return mermaid;
1092
- });
1093
- }
1094
- void (async () => {
1076
+ let timerId;
1077
+ const renderMermaid = async () => {
1078
+ const blocks = container.querySelectorAll(".md-mermaid");
1079
+ if (blocks.length === 0 || !alive) return;
1095
1080
  try {
1096
- const mermaid = await mermaidReady.current;
1081
+ if (!mermaidInstance) {
1082
+ const mod = await import("mermaid");
1083
+ mermaidInstance = mod.default;
1084
+ mermaidInstance.initialize({
1085
+ startOnLoad: false,
1086
+ securityLevel: "antiscript",
1087
+ theme: "neutral",
1088
+ fontFamily: "ui-sans-serif, system-ui, sans-serif",
1089
+ fontSize: 13,
1090
+ htmlLabels: false,
1091
+ flowchart: { useMaxWidth: true, htmlLabels: false },
1092
+ sequence: { useMaxWidth: true }
1093
+ });
1094
+ }
1097
1095
  if (!alive) return;
1098
- for (const block of Array.from(mermaidBlocks)) {
1096
+ for (const block of Array.from(blocks)) {
1099
1097
  if (!alive) break;
1100
1098
  if (block.querySelector("svg")) continue;
1101
1099
  const code = block.getAttribute("data-mermaid-code") || "";
1102
1100
  const id = `mermaid-${Math.random().toString(36).slice(2, 9)}`;
1103
1101
  try {
1104
- const { svg } = await mermaid.render(id, code.trim());
1102
+ const { svg } = await mermaidInstance.render(id, code.trim());
1105
1103
  if (alive) {
1106
1104
  block.innerHTML = `<div style="display:flex;justify-content:center;overflow:auto;padding:1rem">${svg}</div>`;
1107
1105
  }
@@ -1110,9 +1108,11 @@ var MarkdownRenderer = ({
1110
1108
  }
1111
1109
  } catch {
1112
1110
  }
1113
- })();
1111
+ };
1112
+ timerId = setTimeout(renderMermaid, 0);
1114
1113
  return () => {
1115
1114
  alive = false;
1115
+ clearTimeout(timerId);
1116
1116
  };
1117
1117
  }, [html]);
1118
1118
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref: containerRef, className, dangerouslySetInnerHTML: { __html: html } });
package/dist/index.js CHANGED
@@ -881,6 +881,7 @@ function renderMarkdownToHtml(markdown, options) {
881
881
  }
882
882
  return `<div class="prose max-w-none">${parts.join("")}</div>`;
883
883
  }
884
+ var mermaidInstance = null;
884
885
  var MarkdownRenderer = ({
885
886
  markdown,
886
887
  className,
@@ -1029,40 +1030,37 @@ var MarkdownRenderer = ({
1029
1030
  );
1030
1031
  };
1031
1032
  }, [html, handleRun]);
1032
- const mermaidReady = useRef(null);
1033
1033
  useEffect(() => {
1034
1034
  const container = containerRef.current;
1035
1035
  if (!container) return;
1036
- const mermaidBlocks = container.querySelectorAll(".md-mermaid");
1037
- if (mermaidBlocks.length === 0) return;
1038
1036
  let alive = true;
1039
- if (!mermaidReady.current) {
1040
- mermaidReady.current = import("mermaid").then((mod) => {
1041
- const mermaid = mod.default;
1042
- mermaid.initialize({
1043
- startOnLoad: false,
1044
- securityLevel: "antiscript",
1045
- theme: "neutral",
1046
- fontFamily: "ui-sans-serif, system-ui, sans-serif",
1047
- fontSize: 13,
1048
- htmlLabels: false,
1049
- flowchart: { useMaxWidth: true, htmlLabels: false },
1050
- sequence: { useMaxWidth: true }
1051
- });
1052
- return mermaid;
1053
- });
1054
- }
1055
- void (async () => {
1037
+ let timerId;
1038
+ const renderMermaid = async () => {
1039
+ const blocks = container.querySelectorAll(".md-mermaid");
1040
+ if (blocks.length === 0 || !alive) return;
1056
1041
  try {
1057
- const mermaid = await mermaidReady.current;
1042
+ if (!mermaidInstance) {
1043
+ const mod = await import("mermaid");
1044
+ mermaidInstance = mod.default;
1045
+ mermaidInstance.initialize({
1046
+ startOnLoad: false,
1047
+ securityLevel: "antiscript",
1048
+ theme: "neutral",
1049
+ fontFamily: "ui-sans-serif, system-ui, sans-serif",
1050
+ fontSize: 13,
1051
+ htmlLabels: false,
1052
+ flowchart: { useMaxWidth: true, htmlLabels: false },
1053
+ sequence: { useMaxWidth: true }
1054
+ });
1055
+ }
1058
1056
  if (!alive) return;
1059
- for (const block of Array.from(mermaidBlocks)) {
1057
+ for (const block of Array.from(blocks)) {
1060
1058
  if (!alive) break;
1061
1059
  if (block.querySelector("svg")) continue;
1062
1060
  const code = block.getAttribute("data-mermaid-code") || "";
1063
1061
  const id = `mermaid-${Math.random().toString(36).slice(2, 9)}`;
1064
1062
  try {
1065
- const { svg } = await mermaid.render(id, code.trim());
1063
+ const { svg } = await mermaidInstance.render(id, code.trim());
1066
1064
  if (alive) {
1067
1065
  block.innerHTML = `<div style="display:flex;justify-content:center;overflow:auto;padding:1rem">${svg}</div>`;
1068
1066
  }
@@ -1071,9 +1069,11 @@ var MarkdownRenderer = ({
1071
1069
  }
1072
1070
  } catch {
1073
1071
  }
1074
- })();
1072
+ };
1073
+ timerId = setTimeout(renderMermaid, 0);
1075
1074
  return () => {
1076
1075
  alive = false;
1076
+ clearTimeout(timerId);
1077
1077
  };
1078
1078
  }, [html]);
1079
1079
  return /* @__PURE__ */ jsx("div", { ref: containerRef, className, dangerouslySetInnerHTML: { __html: html } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@affanhamid/markdown-renderer",
3
- "version": "2.3.3",
3
+ "version": "2.3.4",
4
4
  "description": "Custom markdown renderer with KaTeX support",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",