nested-markdown 0.0.9 → 0.0.10

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.js CHANGED
@@ -28174,6 +28174,47 @@ async function expandNestedMarkdownInternal(markdown, depth, recursionLimit, def
28174
28174
  }
28175
28175
  return result;
28176
28176
  }
28177
+ function expandNestedMarkdownInternalSync(markdown, depth, recursionLimit, defaultShow) {
28178
+ if (depth > recursionLimit) return markdown;
28179
+ let result = markdown;
28180
+ const blocks = [...findFencedBlocks(result), ...findCommentBlocks(result)];
28181
+ const reversedBlocks = blocks.sort((a, b) => b.startIndex - a.startIndex);
28182
+ for (const block2 of reversedBlocks) {
28183
+ const expandedBody = expandNestedMarkdownInternalSync(
28184
+ block2.nestedMarkdown,
28185
+ depth + 1,
28186
+ recursionLimit,
28187
+ defaultShow
28188
+ );
28189
+ const isInlineBlock = !isAtLineStart(result, block2.startIndex);
28190
+ const resolvedShow = defaultShow || block2.attributes.show || "preview";
28191
+ const canInlineRender = isInlineBlock && resolvedShow === "preview";
28192
+ const renderedHTML = canInlineRender ? marked.parseInline(expandedBody) : marked.parse(expandedBody);
28193
+ let wrapperHTML = generateWrapperHTML({
28194
+ attributes: block2.attributes,
28195
+ nestedMarkdown: block2.nestedMarkdown,
28196
+ renderedHTML,
28197
+ inline: canInlineRender,
28198
+ defaultShow
28199
+ });
28200
+ if (!isInlineBlock) {
28201
+ const currentBlock = result.slice(block2.startIndex, block2.endIndex);
28202
+ const indentation = currentBlock.match(/^[ \t]*/)?.[0] || "";
28203
+ if (indentation) {
28204
+ wrapperHTML = indentation + wrapperHTML;
28205
+ }
28206
+ }
28207
+ const after = result.slice(block2.endIndex);
28208
+ if (isAtLineStart(result, block2.startIndex)) {
28209
+ const trailing = countTrailingNewlines(wrapperHTML);
28210
+ const leading = countLeadingNewlines(after);
28211
+ const needed = Math.max(0, 2 - (trailing + leading));
28212
+ if (needed > 0) wrapperHTML += "\n".repeat(needed);
28213
+ }
28214
+ result = result.slice(0, block2.startIndex) + wrapperHTML + after;
28215
+ }
28216
+ return result;
28217
+ }
28177
28218
  async function expandNestedMarkdown(markdown, options2) {
28178
28219
  const recursionLimit = options2?.recursionLimit ?? DEFAULT_RECURSION_LIMIT;
28179
28220
  return expandNestedMarkdownInternal(
@@ -28183,29 +28224,38 @@ async function expandNestedMarkdown(markdown, options2) {
28183
28224
  options2?.defaultShow
28184
28225
  );
28185
28226
  }
28227
+ function expandNestedMarkdownSync(markdown, options2) {
28228
+ const recursionLimit = options2?.recursionLimit ?? DEFAULT_RECURSION_LIMIT;
28229
+ return expandNestedMarkdownInternalSync(
28230
+ markdown,
28231
+ 0,
28232
+ recursionLimit,
28233
+ options2?.defaultShow
28234
+ );
28235
+ }
28186
28236
 
28187
28237
  // src/Mermaid.tsx
28188
28238
  import { useEffect as useEffect2, useState as useState2, useId } from "react";
28189
- import mermaid from "mermaid";
28190
28239
  import { jsx as jsx2 } from "react/jsx-runtime";
28191
- mermaid.initialize({
28192
- startOnLoad: false,
28193
- theme: "default",
28194
- securityLevel: "loose"
28195
- });
28196
28240
  var Mermaid = ({ chart, theme }) => {
28197
28241
  const id = useId().replace(/:/g, "");
28198
28242
  const [svg5, setSvg] = useState2("");
28243
+ const [hydrated, setHydrated] = useState2(false);
28199
28244
  useEffect2(() => {
28245
+ let cancelled = false;
28246
+ setHydrated(true);
28200
28247
  const renderChart = async () => {
28201
28248
  try {
28249
+ const { default: mermaid } = await import("mermaid");
28202
28250
  let mermaidTheme = "default";
28203
28251
  if (theme === "dark") {
28204
28252
  mermaidTheme = "dark";
28205
28253
  } else if (theme === "light") {
28206
28254
  mermaidTheme = "default";
28207
28255
  } else {
28208
- mermaidTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "default";
28256
+ if (typeof window !== "undefined") {
28257
+ mermaidTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "default";
28258
+ }
28209
28259
  }
28210
28260
  mermaid.initialize({
28211
28261
  startOnLoad: false,
@@ -28214,18 +28264,28 @@ var Mermaid = ({ chart, theme }) => {
28214
28264
  suppressErrorRendering: true
28215
28265
  });
28216
28266
  const { svg: svg6 } = await mermaid.render(`mermaid-${id}`, chart);
28217
- setSvg(svg6);
28267
+ if (!cancelled) {
28268
+ setSvg(svg6);
28269
+ }
28218
28270
  } catch (error) {
28219
28271
  console.error("Failed to render mermaid chart:", error);
28220
- setSvg(
28221
- `<pre class="mermaid-error" style="color: var(--nmd-error, #ef4444); padding: 8px; background: var(--nmd-error-bg, #fee2e2); border-radius: 4px;">Mermaid syntax error</pre>`
28222
- );
28272
+ if (!cancelled) {
28273
+ setSvg(
28274
+ `<pre class="mermaid-error" style="color: var(--nmd-error, #ef4444); padding: 8px; background: var(--nmd-error-bg, #fee2e2); border-radius: 4px;">Mermaid syntax error</pre>`
28275
+ );
28276
+ }
28223
28277
  }
28224
28278
  };
28225
28279
  if (chart) {
28226
28280
  renderChart();
28227
28281
  }
28282
+ return () => {
28283
+ cancelled = true;
28284
+ };
28228
28285
  }, [chart, id, theme]);
28286
+ if (!hydrated) {
28287
+ return /* @__PURE__ */ jsx2("pre", { className: "mermaid", children: chart });
28288
+ }
28229
28289
  return /* @__PURE__ */ jsx2("div", { className: "mermaid", dangerouslySetInnerHTML: { __html: svg5 } });
28230
28290
  };
28231
28291
 
@@ -28334,12 +28394,19 @@ var NestedMarkdown = ({
28334
28394
  defaultShow,
28335
28395
  ...reactMarkdownProps
28336
28396
  }) => {
28337
- const [expandedMarkdown, setExpandedMarkdown] = useState3("");
28397
+ const markdownSource = content3 ?? children2 ?? "";
28398
+ const [expandedMarkdown, setExpandedMarkdown] = useState3(() => {
28399
+ try {
28400
+ return expandNestedMarkdownSync(markdownSource, { defaultShow });
28401
+ } catch (err) {
28402
+ console.error("Markdown expansion error:", err);
28403
+ return markdownSource;
28404
+ }
28405
+ });
28338
28406
  useEffect3(() => {
28339
28407
  let mounted = true;
28340
28408
  const process = async () => {
28341
28409
  try {
28342
- const markdownSource = content3 ?? children2 ?? "";
28343
28410
  const expanded = await expandNestedMarkdown(markdownSource, {
28344
28411
  defaultShow
28345
28412
  });