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.cjs CHANGED
@@ -28185,6 +28185,47 @@ async function expandNestedMarkdownInternal(markdown, depth, recursionLimit, def
28185
28185
  }
28186
28186
  return result;
28187
28187
  }
28188
+ function expandNestedMarkdownInternalSync(markdown, depth, recursionLimit, defaultShow) {
28189
+ if (depth > recursionLimit) return markdown;
28190
+ let result = markdown;
28191
+ const blocks = [...findFencedBlocks(result), ...findCommentBlocks(result)];
28192
+ const reversedBlocks = blocks.sort((a, b) => b.startIndex - a.startIndex);
28193
+ for (const block2 of reversedBlocks) {
28194
+ const expandedBody = expandNestedMarkdownInternalSync(
28195
+ block2.nestedMarkdown,
28196
+ depth + 1,
28197
+ recursionLimit,
28198
+ defaultShow
28199
+ );
28200
+ const isInlineBlock = !isAtLineStart(result, block2.startIndex);
28201
+ const resolvedShow = defaultShow || block2.attributes.show || "preview";
28202
+ const canInlineRender = isInlineBlock && resolvedShow === "preview";
28203
+ const renderedHTML = canInlineRender ? marked.parseInline(expandedBody) : marked.parse(expandedBody);
28204
+ let wrapperHTML = generateWrapperHTML({
28205
+ attributes: block2.attributes,
28206
+ nestedMarkdown: block2.nestedMarkdown,
28207
+ renderedHTML,
28208
+ inline: canInlineRender,
28209
+ defaultShow
28210
+ });
28211
+ if (!isInlineBlock) {
28212
+ const currentBlock = result.slice(block2.startIndex, block2.endIndex);
28213
+ const indentation = currentBlock.match(/^[ \t]*/)?.[0] || "";
28214
+ if (indentation) {
28215
+ wrapperHTML = indentation + wrapperHTML;
28216
+ }
28217
+ }
28218
+ const after = result.slice(block2.endIndex);
28219
+ if (isAtLineStart(result, block2.startIndex)) {
28220
+ const trailing = countTrailingNewlines(wrapperHTML);
28221
+ const leading = countLeadingNewlines(after);
28222
+ const needed = Math.max(0, 2 - (trailing + leading));
28223
+ if (needed > 0) wrapperHTML += "\n".repeat(needed);
28224
+ }
28225
+ result = result.slice(0, block2.startIndex) + wrapperHTML + after;
28226
+ }
28227
+ return result;
28228
+ }
28188
28229
  async function expandNestedMarkdown(markdown, options2) {
28189
28230
  const recursionLimit = options2?.recursionLimit ?? DEFAULT_RECURSION_LIMIT;
28190
28231
  return expandNestedMarkdownInternal(
@@ -28194,49 +28235,68 @@ async function expandNestedMarkdown(markdown, options2) {
28194
28235
  options2?.defaultShow
28195
28236
  );
28196
28237
  }
28238
+ function expandNestedMarkdownSync(markdown, options2) {
28239
+ const recursionLimit = options2?.recursionLimit ?? DEFAULT_RECURSION_LIMIT;
28240
+ return expandNestedMarkdownInternalSync(
28241
+ markdown,
28242
+ 0,
28243
+ recursionLimit,
28244
+ options2?.defaultShow
28245
+ );
28246
+ }
28197
28247
 
28198
28248
  // src/Mermaid.tsx
28199
28249
  var import_react2 = require("react");
28200
- var import_mermaid = __toESM(require("mermaid"), 1);
28201
28250
  var import_jsx_runtime2 = require("react/jsx-runtime");
28202
- import_mermaid.default.initialize({
28203
- startOnLoad: false,
28204
- theme: "default",
28205
- securityLevel: "loose"
28206
- });
28207
28251
  var Mermaid = ({ chart, theme }) => {
28208
28252
  const id = (0, import_react2.useId)().replace(/:/g, "");
28209
28253
  const [svg5, setSvg] = (0, import_react2.useState)("");
28254
+ const [hydrated, setHydrated] = (0, import_react2.useState)(false);
28210
28255
  (0, import_react2.useEffect)(() => {
28256
+ let cancelled = false;
28257
+ setHydrated(true);
28211
28258
  const renderChart = async () => {
28212
28259
  try {
28260
+ const { default: mermaid } = await import("mermaid");
28213
28261
  let mermaidTheme = "default";
28214
28262
  if (theme === "dark") {
28215
28263
  mermaidTheme = "dark";
28216
28264
  } else if (theme === "light") {
28217
28265
  mermaidTheme = "default";
28218
28266
  } else {
28219
- mermaidTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "default";
28267
+ if (typeof window !== "undefined") {
28268
+ mermaidTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "default";
28269
+ }
28220
28270
  }
28221
- import_mermaid.default.initialize({
28271
+ mermaid.initialize({
28222
28272
  startOnLoad: false,
28223
28273
  theme: mermaidTheme,
28224
28274
  securityLevel: "loose",
28225
28275
  suppressErrorRendering: true
28226
28276
  });
28227
- const { svg: svg6 } = await import_mermaid.default.render(`mermaid-${id}`, chart);
28228
- setSvg(svg6);
28277
+ const { svg: svg6 } = await mermaid.render(`mermaid-${id}`, chart);
28278
+ if (!cancelled) {
28279
+ setSvg(svg6);
28280
+ }
28229
28281
  } catch (error) {
28230
28282
  console.error("Failed to render mermaid chart:", error);
28231
- setSvg(
28232
- `<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>`
28233
- );
28283
+ if (!cancelled) {
28284
+ setSvg(
28285
+ `<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>`
28286
+ );
28287
+ }
28234
28288
  }
28235
28289
  };
28236
28290
  if (chart) {
28237
28291
  renderChart();
28238
28292
  }
28293
+ return () => {
28294
+ cancelled = true;
28295
+ };
28239
28296
  }, [chart, id, theme]);
28297
+ if (!hydrated) {
28298
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("pre", { className: "mermaid", children: chart });
28299
+ }
28240
28300
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "mermaid", dangerouslySetInnerHTML: { __html: svg5 } });
28241
28301
  };
28242
28302
 
@@ -28345,12 +28405,19 @@ var NestedMarkdown = ({
28345
28405
  defaultShow,
28346
28406
  ...reactMarkdownProps
28347
28407
  }) => {
28348
- const [expandedMarkdown, setExpandedMarkdown] = (0, import_react3.useState)("");
28408
+ const markdownSource = content3 ?? children2 ?? "";
28409
+ const [expandedMarkdown, setExpandedMarkdown] = (0, import_react3.useState)(() => {
28410
+ try {
28411
+ return expandNestedMarkdownSync(markdownSource, { defaultShow });
28412
+ } catch (err) {
28413
+ console.error("Markdown expansion error:", err);
28414
+ return markdownSource;
28415
+ }
28416
+ });
28349
28417
  (0, import_react3.useEffect)(() => {
28350
28418
  let mounted = true;
28351
28419
  const process = async () => {
28352
28420
  try {
28353
- const markdownSource = content3 ?? children2 ?? "";
28354
28421
  const expanded = await expandNestedMarkdown(markdownSource, {
28355
28422
  defaultShow
28356
28423
  });