Skip to content

Editorial diagram design for Claude Code, Codex, GitHub Copilot, Cursor, Factory Droid, Qoder, and Pi. 45 diagram types. Self-contained HTML + SVG. No shadows. No Mermaid slop.

fix(import): ignore Mermaid samples inside outer Markdown fences (#421)

Rudy Mizrahi Celeklie86841c1cb10
diff --git a/scripts/verify-mermaid-import.py b/scripts/verify-mermaid-import.py
index 174b0f6..5232cb2 100644
--- a/scripts/verify-mermaid-import.py
+++ b/scripts/verify-mermaid-import.py
@@ -579,6 +579,56 @@ A --> B
ok("frontmatter is skipped, and an unterminated block still fails specifically")
+def check_outer_markdown_fences(tmp: Path) -> None:
+ for name, opening, closing in [
+ ("backtick", "````markdown", "````"),
+ ("tilde", "~~~markdown", "~~~"),
+ ("unlabelled", "````", "````"),
+ ("longer-close", "````markdown", "`````"),
+ ("three-space-indent", " ````markdown", " ````"),
+ ]:
+ path = tmp / f"outer-fence-{name}.md"
+ path.write_text(opening + "\n```mermaid\npie title Literal example\n```\n" + closing
+ + "\n\n```mermaid\nflowchart TD\nA --> B\n```\n", encoding="utf-8")
+ payload = json.loads(run_extract([str(path), "--diagram", "all", "--json"]))
+ if payload["diagrams_total"] != 1 or len(payload["diagrams"]) != 1:
+ fail(f"{name}: literal Mermaid sample was counted as a live block")
+ diagram = payload["diagrams"][0]
+ if diagram["kind"] != "flowchart" or {n["id"] for n in diagram["nodes"]} != {"A", "B"}:
+ fail(f"{name}: live flowchart was not preserved")
+ mixed = tmp / "outer-fence-live-controls.md"
+ mixed.write_text("~~~text\n```mermaid\npie title Literal\n```\n~~~\n"
+ "``` MeRmAiD \nflowchart LR\nC --> D\n````\n"
+ "~~~mermaid\nflowchart TD\nE --> F\n~~~~\n", encoding="utf-8")
+ payload = json.loads(run_extract([str(mixed), "--diagram", "all", "--json"]))
+ if payload["diagrams_total"] != 2 or [d["kind"] for d in payload["diagrams"]] != ["flowchart", "flowchart"]:
+ fail("live mixed-case/whitespace and tilde fences did not retain their order")
+ if [{n["id"] for n in d["nodes"]} for d in payload["diagrams"]] != [{"C", "D"}, {"E", "F"}]:
+ fail("ordinary fence contents leaked into live diagram nodes")
+ selected = json.loads(run_extract([str(mixed), "--diagram", "1", "--json"]))
+ if {n["id"] for n in selected["diagrams"][0]["nodes"]} != {"E", "F"}:
+ fail("selection counted literal samples instead of live Mermaid blocks")
+ expect_error([str(mixed), "--diagram", "2"], "no diagram with index 2 (have 0..1)")
+ indented = tmp / "outer-fence-indented-example.md"
+ indented.write_text(" ```text\n\n```mermaid\nflowchart TD\nA --> B\n```\n", encoding="utf-8")
+ payload = json.loads(run_extract([str(indented), "--json"]))
+ if {node["id"] for node in payload["diagrams"][0]["nodes"]} != {"A", "B"}:
+ fail("indented code example hid the following live Mermaid block")
+ indented.write_text("```text\n ```\nstill inside\n```\n\n```mermaid\nflowchart TD\nA --> B\n```\n", encoding="utf-8")
+ payload = json.loads(run_extract([str(indented), "--json"]))
+ if {node["id"] for node in payload["diagrams"][0]["nodes"]} != {"A", "B"}:
+ fail("indented closing fence ended an ordinary block early")
+ # Existing Mermaid indentation remains supported, including four spaces.
+ indented.write_text(" ```mermaid\nflowchart TD\nA --> B\n ```\n", encoding="utf-8")
+ payload = json.loads(run_extract([str(indented), "--json"]))
+ if {node["id"] for node in payload["diagrams"][0]["nodes"]} != {"A", "B"}:
+ fail("existing indented Mermaid support changed")
+ unclosed = tmp / "outer-fence-unclosed.md"
+ unclosed.write_text("````markdown\n```mermaid\nflowchart TD\nA --> B\n```\n", encoding="utf-8")
+ expect_error([str(unclosed)], "no fenced mermaid block found")
+ ok("outer code fences keep Mermaid samples literal and preserve live block order")
+
+
def check_markdown_and_grammars(tmp: Path) -> None:
header = run_extract([str(README_FIXTURE)])
if "2 diagram(s)" not in header or "[1] sequenceDiagram" not in header:
@@ -1353,6 +1403,7 @@ def main() -> int:
check_entity_label_codes(tmp)
check_shape_and_edge_vocabulary(tmp)
check_frontmatter(tmp)
+ check_outer_markdown_fences(tmp)
check_markdown_and_grammars(tmp)
check_state_descriptions(tmp)
check_named_composite_states(tmp)
diff --git a/skills/diagram-design/references/import-mermaid.md b/skills/diagram-design/references/import-mermaid.md
index 40cb1cc..554808f 100644
--- a/skills/diagram-design/references/import-mermaid.md
+++ b/skills/diagram-design/references/import-mermaid.md
@@ -25,6 +25,7 @@ Supported grammars are `flowchart` / `graph`, `sequenceDiagram`, `stateDiagram-v
Labels decode Mermaid base-10 entity codes (such as `#35;` and `#9829;`) and HTML character names (`#copy;`) once, after flattening source markup so escaped characters stay literal. Existing HTML references keep the same one-pass normalization; unknown names and invalid numeric code points stay literal.
+- Mermaid examples inside another Markdown code fence stay literal and are not counted as diagrams. Backtick and tilde outer fences close with the same character and at least the opening length.
- `--diagram N` selects one fenced block before parsing, so a malformed or unsupported block elsewhere in the file does not stop it. `--diagram all` selects every fenced block and fails if any of them fails. Default is diagram 0.
- `--json` emits the full IR, including ER fields and sequence fragments.
- `--max-rows N` controls digest table length; default 40.
diff --git a/skills/diagram-design/scripts/mermaid_extract.py b/skills/diagram-design/scripts/mermaid_extract.py
index 4d20a68..5987ed3 100644
--- a/skills/diagram-design/scripts/mermaid_extract.py
+++ b/skills/diagram-design/scripts/mermaid_extract.py
@@ -271,22 +271,28 @@ def load_blocks(path: Path) -> list[SourceBlock]:
start: int | None = None
fence = ""
content: list[str] = []
+ capture = False
for line_number, line in enumerate(lines, 1):
if start is None:
- match = re.match(r"^\s*(`{3,}|~{3,})\s*mermaid\s*$", line, re.I)
- if match:
+ match = re.match(r"^\s*(`{3,}|~{3,})(.*)$", line)
+ if (match and not (match.group(1)[0] == "`" and "`" in match.group(2))
+ and (match.group(2).strip().casefold() == "mermaid"
+ or re.match(r"^ {0,3}[`~]", line))):
start = line_number + 1
fence = match.group(1)
+ capture = match.group(2).strip().casefold() == "mermaid"
content = []
continue
- if re.match(rf"^\s*{re.escape(fence[0])}{{{len(fence)},}}\s*$", line):
- blocks.append(SourceBlock(len(blocks), "\n".join(content), start))
+ if (re.match(rf"^\s*{re.escape(fence[0])}{{{len(fence)},}}\s*$", line)
+ and (capture or re.match(r"^ {0,3}[`~]", line))):
+ if capture:
+ blocks.append(SourceBlock(len(blocks), "\n".join(content), start))
start = None
fence = ""
content = []
- else:
+ elif capture:
content.append(line)
- if start is not None:
+ if start is not None and capture:
_fail(f"{path.name}: unterminated mermaid fence starting at line {start - 1}")
if not blocks:
_fail(f"{path.name}: no fenced mermaid block found")