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): cap Mermaid statement length and end spaced labels at their own operator (#258)

Cathryn Laveryc50910b768c8
diff --git a/scripts/verify-mermaid-import.py b/scripts/verify-mermaid-import.py
index f2a9009..5fbe420 100644
--- a/scripts/verify-mermaid-import.py
+++ b/scripts/verify-mermaid-import.py
@@ -319,6 +319,24 @@ H -- " padded " --> I
]:
fail("quoted labeled multidirectional links lost their arrowheads")
+ chain_file = tmp / "quoted-label-chain.mmd"
+ chain_file.write_text(
+ 'flowchart LR\nA -- "yes" --> B --> C\nD -- (maybe) --> E --> F\n',
+ encoding="utf-8",
+ )
+ chain = json.loads(run_extract([str(chain_file), "--json"]))["diagrams"][0]
+ chain_edges = [(edge["source"], edge["target"], edge["label"]) for edge in chain["edges"]]
+ if chain_edges != [
+ ("A", "B", "yes"),
+ ("B", "C", ""),
+ ("D", "E", "(maybe)"),
+ ("E", "F", ""),
+ ]:
+ fail(
+ "a quoted or bracketed spaced label followed by another link must end at "
+ f"its own closing operator: {chain_edges}"
+ )
+
compact_file = tmp / "compact-labeled-links.mmd"
compact_file.write_text(
"""flowchart LR
@@ -811,6 +829,63 @@ def check_errors_and_limits(tmp: Path) -> None:
oversized = tmp / "oversized.mmd"
oversized.write_bytes(b"flowchart TD\n" + b" " * extractor.MAX_SOURCE_BYTES)
expect_error([str(oversized)], "source exceeds")
+
+ statement_cap = getattr(extractor, "MAX_STATEMENT_CHARS", 4096)
+ at_cap = tmp / "statement-at-cap.mmd"
+ stem = 'A[""] --> B'
+ at_cap.write_text(
+ 'flowchart TD\nA["' + "x" * (statement_cap - len(stem)) + '"] --> B\n',
+ encoding="utf-8",
+ )
+ run_extract([str(at_cap)])
+ over_cap = tmp / "statement-over-cap.mmd"
+ over_cap.write_text(
+ 'flowchart TD\nA["' + "x" * (statement_cap - len(stem) + 1) + '"] --> B\n',
+ encoding="utf-8",
+ )
+ expect_error(
+ [str(over_cap)],
+ f"statement at line 2 exceeds the {statement_cap}-character limit",
+ )
+ minified = tmp / "statement-minified.mmd"
+ minified.write_text(
+ "flowchart TD\n" + ";".join(f"N{index}-->N{index + 1}" for index in range(600)) + "\n",
+ encoding="utf-8",
+ )
+ if len(minified.read_text(encoding="utf-8")) <= statement_cap:
+ fail("the minified fixture must be longer than one statement's cap")
+ run_extract([str(minified)])
+ unterminated_long = tmp / "statement-unterminated-long.mmd"
+ unterminated_long.write_text(
+ 'flowchart TD\nA["' + "\n".join("x" * 60 for _ in range(200)) + "\n",
+ encoding="utf-8",
+ )
+ expect_error(
+ [str(unterminated_long)],
+ f"statement at line 2 exceeds the {statement_cap}-character limit",
+ )
+ minified_then_multiline = tmp / "statement-minified-then-multiline.mmd"
+ minified_then_multiline.write_text(
+ "flowchart TD\n"
+ + ";".join(f"N{index}-->N{index + 1}" for index in range(600))
+ + ';M["label that\ncontinues"] --> N0\n',
+ encoding="utf-8",
+ )
+ minified_multiline = json.loads(run_extract([str(minified_then_multiline), "--json"]))["diagrams"][0]
+ if len(minified_multiline["edges"]) != 601:
+ fail(
+ "a minified line followed by a multiline label must parse every statement: "
+ f"{len(minified_multiline['edges'])} edges"
+ )
+ unterminated_one_line = tmp / "statement-unterminated-one-line.mmd"
+ unterminated_one_line.write_text(
+ 'flowchart TD\nA["' + "x" * (statement_cap + 10) + "\n",
+ encoding="utf-8",
+ )
+ expect_error(
+ [str(unterminated_one_line)],
+ f"statement at line 2 exceeds the {statement_cap}-character limit",
+ )
ok("all documented exit-2 paths and resource caps fire specifically")
diff --git a/skills/diagram-design/references/import-mermaid.md b/skills/diagram-design/references/import-mermaid.md
index 56ae64b..184a758 100644
--- a/skills/diagram-design/references/import-mermaid.md
+++ b/skills/diagram-design/references/import-mermaid.md
@@ -106,7 +106,7 @@ Markdown is the Mermaid analogue of multi-page draw.io. The header lists every f
| `no fenced mermaid block found` | Report it verbatim; ask for a `.mmd`/`.mermaid` file or a fenced block. |
| Unsupported kind such as `pie`, `mindmap`, `gitGraph`, `quadrantChart`, `timeline`, `C4Context`, or `sankey` | Report the supported-kinds message verbatim. Do not approximate it with a different type. |
| `malformed edge at line N` | Report the line number and stop. Do not guess endpoints. |
-| Node/edge/source limit exceeded | Ask for a smaller source or split by subgraph. Never bypass the cap. |
+| Node/edge/source/statement limit exceeded | Ask for a smaller source or split by subgraph; a single statement over the character limit usually means a pasted blob, not a diagram. Never bypass the cap. |
| Unconnected nodes listed | Usually legends or abandoned notes. Drop only with a fidelity-ledger entry. |
| Click handlers present | They were discarded. Never open or reproduce their targets. |
| Markdown labels or HTML entities | Use the normalized plain-text label from the digest. |
diff --git a/skills/diagram-design/scripts/mermaid_extract.py b/skills/diagram-design/scripts/mermaid_extract.py
index 643ab2f..4599f34 100644
--- a/skills/diagram-design/scripts/mermaid_extract.py
+++ b/skills/diagram-design/scripts/mermaid_extract.py
@@ -32,6 +32,10 @@ from typing import Any, NoReturn
MAX_SOURCE_BYTES = 4 * 1024 * 1024
MAX_NODES = 2000
MAX_EDGES = 5000
+# One logical flowchart statement, whether it spans lines or shares a line
+# with others. Real statements are a few hundred characters at most; the cap
+# keeps per-statement parsing work bounded.
+MAX_STATEMENT_CHARS = 4096
SUPPORTED_KINDS = "flowchart, sequenceDiagram, stateDiagram-v2, erDiagram"
UNSUPPORTED_KINDS = {
"pie",
@@ -405,6 +409,13 @@ def _statement_complete(text: str) -> bool:
return quote is None and not stack
+def _statement_too_long(line_number: int) -> NoReturn:
+ _fail(
+ f"statement at line {line_number} exceeds the "
+ f"{MAX_STATEMENT_CHARS}-character limit"
+ )
+
+
def _logical_statements(
lines: list[tuple[int, str]],
) -> list[tuple[int, str]]:
@@ -419,13 +430,22 @@ def _logical_statements(
start_line = line_number
pending.append(raw)
combined = "\n".join(pending)
- if not _statement_complete(combined):
+ statements = _split_top_level(combined, ";")
+ complete = _statement_complete(combined)
+ # Everything before the last top-level semicolon is finished even while
+ # a quote or bracket after it stays open, so only that open statement is
+ # carried to the next line. Each statement is bounded on its own, and
+ # the open one is bounded before the next line joins and rescans it.
+ for statement in statements if complete else statements[:-1]:
+ if len(statement) > MAX_STATEMENT_CHARS:
+ _statement_too_long(start_line)
+ logical.append((start_line, statement))
+ if complete:
+ pending = []
continue
- logical.extend(
- (start_line, statement)
- for statement in _split_top_level(combined, ";")
- )
- pending = []
+ if len(statements[-1]) > MAX_STATEMENT_CHARS:
+ _statement_too_long(start_line)
+ pending = [statements[-1]]
if pending:
_fail(f"unterminated statement at line {start_line}")
return logical
@@ -606,7 +626,10 @@ def _edge_operators(text: str) -> list[_Operator]:
# `A-- text -->B`, `A-. retry .-> B`, `A== critical ==> B`, and the
# undirected forms of each. The compact form drops the spaces —
# `B--yes-->C` — and may retain a left arrow/circle/cross marker, as in
- # `A<--yes-->B` or `A o--yes--o B`. Its label may not contain whitespace,
+ # `A<--yes-->B` or `A o--yes--o B`. The spaced form consumes exactly one
+ # whitespace character next to each operator; any further padding falls
+ # inside the label span, which `clean_label` strips, so the operator
+ # boundaries are never ambiguous. The compact label may not contain whitespace,
# and the operator characters themselves may not open one (keeping
# `A----->B` unlabeled and `A --o B --> C` two separate links).
text_edge = re.compile(
@@ -615,7 +638,7 @@ def _edge_operators(text: str) -> list[_Operator]:
r"|(?<![\w.:-])[xo](?:--|-\.|==)"
r"|(?:--|-\.|==)"
r")"
- r"(?:\s+(?P<spaced>.+?)\s+|(?![-=.\s])(?P<compact>[^\s|<>]+?))"
+ r"(?:\s(?P<spaced>.+?)\s|(?![-=.\s])(?P<compact>[^\s|<>]+?))"
r"(?P<closing>\.-+[>xo]|\.-+|-{2,}>|--[xo]|=+>|={2,}|-{3,})"
)
trailing_operator = re.compile(