fix(ink): render Mermaid fences inside callouts/quotes #37

Merged
daniel merged 2 commits from agent/mermaid-in-quotes into main 2026-10-21 00:35:44 +02:00
Collaborator

Closes #36

What to change and where

One function: mermaidBlocks() in src/ink/mermaidtheme.ts:96. It needs to find Mermaid fences inside > quotes and callouts, at any depth. It must return each diagram's source exactly as parseBlocks(..., { nestedQuotes: true }) puts it in the nested code block's lines, so that diagramKey(source) matches.

Suggested shape:

  • Strip one quote level the way the parser does: the regex ^\s*>\s?, applied repeatedly (see quoteInner in src/convert/mdblocks.ts:166, which parseBlocks calls recursively once per level). Use it to get the opening line's quote depth d and its inner text, then test the existing /^\s*(```|~~~)\s*mermaid\b/ against that inner text.
  • For each following line, strip at most d levels:
    • Stop at a closing fence (/^\s*(```|~~~)/ on the inner text). The outer i++ skips that line, as it does today.
    • Also stop when the line has fewer than d quote markers, because the quote ended (e.g. a blank or unquoted line). That is where the parser's quote run ends, so an unclosed quoted fence ends there too. Do not consume that line: back i up one so the outer loop still sees it, since it could open another diagram.
    • Otherwise push the line with exactly d levels stripped. Do not strip more: a diagram line that itself starts with > keeps it, just as in the parser.
  • Top-level fences (d = 0) must behave exactly as today.
  • Update the doc comment ("Mermaid fenced blocks in markdown, as parseBlocks will see their source") to mention quotes and callouts.

Why

Reproduced on main @ 67092a6. The input is the preprocessed callout "> **Note**\n> ```mermaid\n> flowchart LR\n> a --> b\n> ```\n" (flattenCallouts has already turned > [!note] into > **Note**):

  • parseBlocks(md, { html: true, quotePictures: true, nestedQuotes: true }) → quote → blocks contains { type: "code", lang: "mermaid", lines: ["flowchart LR", " a --> b"] }, key mermaid:76715218. The same happens at depth 2 (> outer\n> > ```mermaid …).
  • mermaidBlocks(md) → [].

So main.ts noteDrawings() (src/main.ts:1703) never renders the diagram and drawings has no entry for it. The renderer is already wired up for nested blocks: withImageBlocks() in src/ink/inkrender.ts:136-148 recurses into quote.blocks (added in !23 for pictures) and swaps any code block whose diagramKey is in drawings for an inkimage. Collecting the source is the only missing piece.

Constraints and decisions

  • Decision (the issue's open question): fix it in mermaidBlocks(), not in the nested-quote rendering. Rendering a Mermaid diagram needs the DOM and an async mermaid.render in main.ts. The pure parseBlocks → renderInk pipeline can't do that, and it already handles nested code → drawing swaps. Keep parseBlocks, withImageBlocks and diagramKey unchanged.
  • Replay or import of older sends is unaffected. Those sends parse flat quotes (nestedQuotes is off), so they never produce a nested code block, and an extra recorded drawing entry is harmless.
  • When inkDiagrams !== "handwritten", nothing changes: noteDrawings returns early.

Scope boundary

Not part of this MR: fences inside list items, and mermaidBlocks mis-detecting a ```mermaid line that sits inside another code fence. That second limitation already exists at top level and is unchanged here. No changes to mdblocks.ts.

Tests (add to src/__tests__/ink-drawing.test.ts, describe("Mermaid sources"))

  1. Callout at depth 1, "> **Note**\n> ```mermaid\n" + SOURCES.flowchart (each line prefixed "> ") + "\n> ```": mermaidBlocks returns [SOURCES.flowchart], and its diagramKey equals the key of the nested code block found by walking parseBlocks(md, { nestedQuotes: true }) quote blocks.
  2. Same at depth 2 (> > ), plus a line whose content keeps indentation (> a --> b → " a --> b").
  3. An unclosed quoted fence followed by an unquoted paragraph and a top-level mermaid fence: the quoted source stops at the end of the quote, and the top-level diagram is still found (2 results).
  4. Renderer swap: withImageBlocks(parseBlocks(md, { nestedQuotes: true }), undefined, { [diagramKey(src)]: FLOWCHART }) gives a quote whose blocks contains inkimage and no code.

Validation contract

The repo has no flake.nix, so run Node directly: npm ci first.

  • Red gate (fails today): test 1 above → mermaidBlocks returns [] instead of [SOURCES.flowchart].
  • npx vitest run src/__tests__/ink-drawing.test.ts → all pass, including the existing "finds fenced diagrams exactly as the block parser sees them".
  • npx vitest run → whole suite passes.
  • npx tsc -noEmit -skipLibCheck → exit 0. npx eslint src/ink/mermaidtheme.ts → clean.

🤖 Generated with Claude Code

Closes #36 ## What to change and where **One function: `mermaidBlocks()` in `src/ink/mermaidtheme.ts:96`.** It needs to find Mermaid fences inside `>` quotes and callouts, at any depth. It must return each diagram's source **exactly as `parseBlocks(..., { nestedQuotes: true })` puts it in the nested `code` block's `lines`**, so that `diagramKey(source)` matches. Suggested shape: - Strip one quote level the way the parser does: the regex `^\s*>\s?`, applied repeatedly (see `quoteInner` in `src/convert/mdblocks.ts:166`, which `parseBlocks` calls recursively once per level). Use it to get the opening line's quote **depth `d`** and its inner text, then test the existing `/^\s*(```|~~~)\s*mermaid\b/` against that inner text. - For each following line, strip **at most `d` levels**: - **Stop** at a closing fence (`/^\s*(```|~~~)/` on the inner text). The outer `i++` skips that line, as it does today. - **Also stop** when the line has fewer than `d` quote markers, because the quote ended (e.g. a blank or unquoted line). That is where the parser's quote run ends, so an unclosed quoted fence ends there too. Do **not** consume that line: back `i` up one so the outer loop still sees it, since it could open another diagram. - Otherwise push the line with exactly `d` levels stripped. Do not strip more: a diagram line that itself starts with `>` keeps it, just as in the parser. - Top-level fences (`d = 0`) must behave exactly as today. - Update the doc comment ("Mermaid fenced blocks in markdown, as parseBlocks will see their source") to mention quotes and callouts. ## Why Reproduced on `main` @ 67092a6. The input is the preprocessed callout `"> **Note**\n> ```mermaid\n> flowchart LR\n> a --> b\n> ```\n"` (`flattenCallouts` has already turned `> [!note]` into `> **Note**`): - `parseBlocks(md, { html: true, quotePictures: true, nestedQuotes: true })` → quote → `blocks` contains `{ type: "code", lang: "mermaid", lines: ["flowchart LR", " a --> b"] }`, key `mermaid:76715218`. The same happens at depth 2 (`> outer\n> > ```mermaid …`). - `mermaidBlocks(md)` → `[]`. So `main.ts` `noteDrawings()` (`src/main.ts:1703`) never renders the diagram and `drawings` has no entry for it. The renderer is already wired up for nested blocks: `withImageBlocks()` in `src/ink/inkrender.ts:136-148` recurses into `quote.blocks` (added in !23 for pictures) and swaps any `code` block whose `diagramKey` is in `drawings` for an `inkimage`. Collecting the source is the only missing piece. ## Constraints and decisions - **Decision (the issue's open question):** fix it in `mermaidBlocks()`, not in the nested-quote rendering. Rendering a Mermaid diagram needs the DOM and an async `mermaid.render` in `main.ts`. The pure `parseBlocks` → `renderInk` pipeline can't do that, and it already handles nested code → drawing swaps. Keep `parseBlocks`, `withImageBlocks` and `diagramKey` unchanged. - Replay or import of older sends is unaffected. Those sends parse flat quotes (`nestedQuotes` is off), so they never produce a nested code block, and an extra recorded drawing entry is harmless. - When `inkDiagrams !== "handwritten"`, nothing changes: `noteDrawings` returns early. ## Scope boundary Not part of this MR: fences inside list items, and `mermaidBlocks` mis-detecting a ```` ```mermaid ```` line that sits inside another code fence. That second limitation already exists at top level and is unchanged here. No changes to `mdblocks.ts`. ## Tests (add to `src/__tests__/ink-drawing.test.ts`, `describe("Mermaid sources")`) 1. Callout at depth 1, `"> **Note**\n> ```mermaid\n" + SOURCES.flowchart (each line prefixed "> ") + "\n> ```"`: `mermaidBlocks` returns `[SOURCES.flowchart]`, and its `diagramKey` equals the key of the nested `code` block found by walking `parseBlocks(md, { nestedQuotes: true })` quote blocks. 2. Same at depth 2 (`> > `), plus a line whose content keeps indentation (`> a --> b` → `" a --> b"`). 3. An unclosed quoted fence followed by an unquoted paragraph and a top-level mermaid fence: the quoted source stops at the end of the quote, and the top-level diagram is still found (2 results). 4. Renderer swap: `withImageBlocks(parseBlocks(md, { nestedQuotes: true }), undefined, { [diagramKey(src)]: FLOWCHART })` gives a quote whose `blocks` contains `inkimage` and no `code`. ## Validation contract The repo has no flake.nix, so run Node directly: `npm ci` first. - **Red gate (fails today):** test 1 above → `mermaidBlocks` returns `[]` instead of `[SOURCES.flowchart]`. - `npx vitest run src/__tests__/ink-drawing.test.ts` → all pass, including the existing "finds fenced diagrams exactly as the block parser sees them". - `npx vitest run` → whole suite passes. - `npx tsc -noEmit -skipLibCheck` → exit 0. `npx eslint src/ink/mermaidtheme.ts` → clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
claude self-assigned this 2026-10-21 00:28:26 +02:00
Author
Collaborator

✅ Agent run completed

2m17s · 19 turns · $0.46 · pushed agent/mermaid-in-quotes · session saved — new comments resume this run

✅ **Agent run completed** _2m17s · 19 turns · $0.46 · pushed `agent/mermaid-in-quotes` · session saved — new comments resume this run_
mermaidBlocks now strips quote markers the way the nested-quote parse
does, so a diagram in a callout gets drawn instead of sent as code.

Closes #36

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Collaborator

What changed

  • mermaidBlocks() (src/ink/mermaidtheme.ts) now finds Mermaid fences inside > quotes and callouts at any depth. It reads the opening line's quote depth d with the parser's ^\s*>\s? rule, then strips exactly d levels from each later line. The source ends at a closing fence, or where the quote ends (fewer than d markers). In the second case that line is handed back to the outer loop, so it can still open the next diagram. Top-level fences behave as before. The doc comment is updated.
  • Added 4 tests to describe("Mermaid sources") in src/__tests__/ink-drawing.test.ts: callout at depth 1; depth 2, keeping a line's indentation; an unclosed quoted fence followed by a top-level fence; and the withImageBlocks swap to inkimage. No changes to mdblocks.ts, withImageBlocks or diagramKey.

Verify

Red gate: the 4 new tests fail on the old code. Now: ink-drawing.test.ts 14/14 pass; full vitest run 782 passed, 0 failed, 1 skipped; tsc -noEmit -skipLibCheck exit 0; eslint clean. No CI pipeline has run for head e188a35 yet; the only recent run is an older one on main.

Follow-ups

None. Out of scope, as the hand-off said: fences inside list items, and the existing mis-detection of a ```mermaid line inside another code fence.

## What changed - `mermaidBlocks()` (`src/ink/mermaidtheme.ts`) now finds Mermaid fences inside `>` quotes and callouts at any depth. It reads the opening line's quote depth `d` with the parser's `^\s*>\s?` rule, then strips exactly `d` levels from each later line. The source ends at a closing fence, or where the quote ends (fewer than `d` markers). In the second case that line is handed back to the outer loop, so it can still open the next diagram. Top-level fences behave as before. The doc comment is updated. - Added 4 tests to `describe("Mermaid sources")` in `src/__tests__/ink-drawing.test.ts`: callout at depth 1; depth 2, keeping a line's indentation; an unclosed quoted fence followed by a top-level fence; and the `withImageBlocks` swap to `inkimage`. No changes to `mdblocks.ts`, `withImageBlocks` or `diagramKey`. ## Verify Red gate: the 4 new tests fail on the old code. Now: `ink-drawing.test.ts` 14/14 pass; full `vitest run` 782 passed, 0 failed, 1 skipped; `tsc -noEmit -skipLibCheck` exit 0; eslint clean. No CI pipeline has run for head `e188a35` yet; the only recent run is an older one on `main`. ## Follow-ups None. Out of scope, as the hand-off said: fences inside list items, and the existing mis-detection of a ```` ```mermaid ```` line inside another code fence.
daniel merged commit 735c4b4731 into main 2026-10-21 00:35:44 +02:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
daniel/obsidian-remarkable-round-trip!37
No description provided.