Skip to content

fix(mermaid): escape the punctuation a sequence diagram reads as syntax - #169

Merged
nao1215 merged 1 commit into
mainfrom
fix/sequence-statement-punctuation
Aug 12, 2026
Merged

nao1215 merged 1 commit into
mainfrom
fix/sequence-statement-punctuation

Conversation

@nao1215

@nao1215 nao1215 commented Aug 12, 2026 •

Copy link
Copy Markdown
Owner

Refs #146 — one subpackage per pull request. This is sequence, and it has the widest spread of the set because a sequence diagram takes no quoted text anywhere.

Two escape sets, measured one character at a time

Message, note, block description — # ;

char what happens today
; ends the statement, whole diagram refused
# opens a comment — deploy #2 of 3 reaches the drawing as deploy, silently

Participant name — ; : , < > - %% ( )

char what happens today
; ends the statement
: separates the name from the message after it
, separates one name from the next
< > open the arrow syntax
- starts every arrow — web-server could be declared but never sent a message
%% opens a comment on the message line and drops the message
( ) safe alone, fatal as a pair

A name is escaped identically where it is declared and where a message refers to it, so the two still match. There is a test for exactly that, since getting it wrong would silently create a second participant.

Deliberately left alone

  • A # in a participant name — safe there, unlike in message text.
  • A lone % anywhere — only a run opens a comment, so 50% traffic is untouched.
  • The comma between two participants a note spans. NoteOver("Alice,Bob", …) is documented mermaid syntax for a note across both, and this package cannot tell it apart from one name holding a comma. Tested, and called out in the code.

On the parentheses: a single unmatched ( renders today, so escaping it does change working output. An unmatched parenthesis is not a name anybody writes, and the matched pair — Deploy (prod) — is broken today. The drawn text is identical either way.

Verification

  • go test ./... passes with no golden file changed; golangci-lint run ./... reports 0 issues.
  • mermaid/sequence coverage 98.7%.
  • The sequence entry in doc/edgecase/main.go goes from "'#[]+emoji+Japanese+*-| to the full probe set, and the document is regenerated. Every committed diagram renders: 64 blocks, 0 failed.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Mermaid sequence diagrams by safely escaping special characters in participant names, messages, notes, and control-block descriptions.
    • Added support for punctuation, brackets, braces, symbols, emoji, Japanese text, and encoded content without breaking diagram syntax.
    • Preserved valid formatting for colons, percent pairs, hashes, and participant separators.
    • Maintained support for inline <br/> breaks while excluding unsupported line breaks.
  • Tests
    • Expanded coverage for escaping across messages, notes, blocks, participants, and actors.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 33e63cb1-e6fa-4860-8b23-1b940a358f7b

📥 Commits

Reviewing files that changed from the base of the PR and between 11e990d and f3d2194.

📒 Files selected for processing (1)
  • doc/edgecase/main.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • doc/edgecase/main.go

📝 Walkthrough

Walkthrough

Sequence diagram generation now escapes syntax-sensitive participant names, messages, notes, and control-block descriptions. Tests and edge-case fixtures cover punctuation, entity-like sequences, preserved percent and hash forms, emoji, and Japanese text.

Changes

Sequence diagram escaping

Layer / File(s) Summary
Escaping utilities
mermaid/sequence/escape.go
Added escaping rules for text, participant names, note targets, and participant lists. Entity-like hashes and percent pairs receive special handling.
Sequence output integration
mermaid/sequence/*.go
Participant directives, activation methods, messages, notes, and control-block descriptions now use escaped values.
Escaping validation and edge cases
mermaid/sequence/sequence_test.go, doc/edgecase/*
Tests and fixtures cover syntax-sensitive punctuation, preserved valid forms, Unicode content, emoji, and Japanese text.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

  • nao1215/markdown#147 — Both PRs update sequence edge-case fixtures, while this PR also adds sequence-content escaping.
  • nao1215/markdown#156 — Both PRs modify Mermaid escaping behavior and edge-case coverage.
  • nao1215/markdown#159 — Both PRs add Mermaid escaping utilities and apply entity handling to diagram text.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: escaping punctuation that Mermaid interprets as sequence-diagram syntax.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/sequence-statement-punctuation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

This comment has been minimized.

A sequence diagram takes no quoted text anywhere, so a message, a note, a
block description and a participant name all go out bare, and each loses
a different set of characters.

In the text of a diagram a semicolon ends the statement and loses the
whole picture, and a "#" opens a comment: "deploy #2 of 3" reached the
drawing as "deploy" and nothing said so.

A participant name loses more. A semicolon ends the statement there too;
a colon separates a name from the message after it and a comma separates
one name from the next; an angle bracket opens the arrow syntax. A hyphen
is the start of every arrow, so "web-server" could be declared and then
never sent a message. A "%%" opens a comment on the message line and
drops the message without a word. A parenthesis is safe on its own and a
pair is not, so both are written out: an unmatched one is not a name
anybody writes, and the pair is what "Deploy (prod)" has.

Each is written as the entity form mermaid decodes, and a name is escaped
identically where it is declared and where a message refers to it, so the
two still match. A test pins that.

Three things are deliberately left alone: a "#" in a participant name, a
lone "%" anywhere, and the comma between two participants a note is
placed over, which is the syntax that says the note spans both and cannot
be told apart from one name holding a comma.

The sequence entry in doc/edgecase goes from six punctuation marks to the
full set. No golden file moves.

Refs #146
@nao1215
nao1215 force-pushed the fix/sequence-statement-punctuation branch from 11e990d to f3d2194 Compare August 12, 2026 06:29
@github-actions

Copy link
Copy Markdown

Code Metrics Report

main (d06f2bb) #169 (528a9e4) +/-
Coverage 96.4% 96.4% -0.1%
Test Execution Time 10s 9s -1s
Details
  |                     | main (d06f2bb) | #169 (528a9e4) |  +/-  |
  |---------------------|----------------|----------------|-------|
- | Coverage            |          96.4% |          96.4% | -0.1% |
  |   Files             |             77 |             78 |    +1 |
  |   Lines             |           3263 |           3285 |   +22 |
+ |   Covered           |           3148 |           3169 |   +21 |
+ | Test Execution Time |            10s |             9s |   -1s |

Code coverage of files in pull request scope (99.0% → 98.4%)

Files Coverage +/- Status
mermaid/sequence/activation.go 100.0% 0.0% modified
mermaid/sequence/directive.go 100.0% 0.0% modified
mermaid/sequence/escape.go 95.4% +95.4% added
mermaid/sequence/note.go 100.0% 0.0% modified
mermaid/sequence/sequence.go 97.1% 0.0% modified
mermaid/sequence/statement.go 100.0% 0.0% modified

Reported by octocov

@nao1215
nao1215 merged commit 4b6a98d into main Aug 12, 2026
22 checks passed
@nao1215
nao1215 deleted the fix/sequence-statement-punctuation branch August 12, 2026 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant