Skip to content

Commit 647387f

Browse files
committed
fix(wiki): unwrap fenced diagram placeholders
1 parent 11a91f7 commit 647387f

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

backend/app/services/wiki/sources/rendering.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
from backend.app.services.wiki.sources.urls import _source_file_href, _source_ref_href
77

88
DIAGRAM_SLOT_RE = re.compile(r"^\s*\[\[DIAGRAM:(?P<slot>[a-zA-Z0-9_-]+)\]\]\s*$", re.MULTILINE)
9+
FENCED_DIAGRAM_SLOT_RE = re.compile(
10+
r"(?m)^[ \t]*`{3,}[ \t]*\n"
11+
r"[ \t]*\[\[DIAGRAM:(?P<slot>[a-zA-Z0-9_-]+)\]\][ \t]*\n"
12+
r"[ \t]*`{3,}[ \t]*$"
13+
)
914
DIAGRAM_HEADING_CANDIDATES = {
1015
"component": ("## System Context", "## Architecture", "## Core Components", "## Overview"),
1116
"data_flow": ("## Control Flow", "## Core Workflows", "## System Context"),
@@ -143,7 +148,8 @@ def replace_slot(match: re.Match[str]) -> str:
143148
used_slots.add(slot)
144149
return _diagram_markdown(diagram, source_refs)
145150

146-
placed = DIAGRAM_SLOT_RE.sub(replace_slot, markdown)
151+
placed = FENCED_DIAGRAM_SLOT_RE.sub(replace_slot, markdown)
152+
placed = DIAGRAM_SLOT_RE.sub(replace_slot, placed)
147153
unused_diagrams = [diagram for diagram in diagrams if diagram.slot not in used_slots]
148154
placed = _insert_unused_diagrams(placed, unused_diagrams, source_refs)
149155
return _strip_unknown_diagram_slots(placed)

tests/backend/test_wiki_generator.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,33 @@ def test_page_markdown_repairs_conjoined_mermaid_fence_headings() -> None:
10991099
assert "```\n### Training a Skill interaction sequence" in rendered
11001100

11011101

1102+
def test_diagram_placeholders_inside_plain_fences_do_not_wrap_mermaid() -> None:
1103+
diagram = MermaidDiagram(
1104+
slot="component-relationships",
1105+
kind="component",
1106+
title="User Guide component relationships",
1107+
heading_hint="System Context",
1108+
reason="test diagram",
1109+
lines=["graph TD", " A --> B"],
1110+
)
1111+
markdown = (
1112+
"# User Guide\n\n"
1113+
"## Purpose and Scope\n\n"
1114+
"The page explains user workflows.\n\n"
1115+
"## System Context\n\n"
1116+
"```\n"
1117+
"[[DIAGRAM:component-relationships]]\n"
1118+
"```\n\n"
1119+
"## Configuration\n\n"
1120+
"Configuration follows."
1121+
)
1122+
1123+
rendered = _compose_page_markdown(markdown, [diagram], [])
1124+
1125+
assert "```\n### User Guide component relationships" not in rendered
1126+
assert "### User Guide component relationships\n\n```mermaid\ngraph TD\n A --> B\n```" in rendered
1127+
1128+
11021129
def test_unknown_diagram_placeholders_are_validation_errors() -> None:
11031130
errors = _validate_diagram_placeholders(
11041131
"# Page\n\n## Purpose and Scope\n\n[[DIAGRAM:invented]]",

0 commit comments

Comments
 (0)