Skip to content

[Bug] mermaid-text: classDiagram panics on non-ASCII lines (byte index is not a char boundary) #50

Closed

Description

@worldnine

Description

mermaid_text::render panics on a classDiagram whose lines contain multi-byte characters (CJK, Cyrillic, …), e.g. a plain relation 動物 <|-- 犬.

The class parser still has its own copy of the keyword-strip helper, strip_keyword_prefix_ci (crates/mermaid-text/src/parser/class.rs:619), which slices line[..kw.len()] at a byte index. It looks like the one copy the #29 audit (0.56.1) didn't catch: gantt/timeline/journey were routed to parser::common::strip_keyword_prefix then, but this _ci-suffixed one in class.rs was left behind.

Steps to Reproduce

On master (186698c) and on the published 0.57.0:

printf 'classDiagram\n  動物 <|-- 犬\n' | mermaid-text

These lines panic the same way:

  • Животное <|-- Собака
  • 犬 : +吠える()
  • 動物 : 名前

class 動物 and 犬 --|> 動物 happen not to panic, because none of the probed keyword lengths (class, direction, note, link, click, namespace) lands inside a character there.

Expected Behavior

The diagram renders like its ASCII spelling. For the unsupported colon shorthand, render returns the usual Err.

Actual Behavior

thread 'main' panicked at crates/mermaid-text/src/parser/class.rs:622:16:
byte index 4 is not a char boundary; it is inside '物' (bytes 3..6) of `動物 <|-- 犬`

Environment

Field Value
OS macOS 27.2
Terminal Ghostty (not relevant; reproduced with the CLI)
Image protocol n/a
Rust version rustc 1.92.0
markdown-reader version n/a (mermaid-text used as a library)
mermaid-text version 0.57.0 from crates.io; also master 186698c
Installation method library dependency; CLI built from source

Additional Context

I ran into this in cosentty, a terminal viewer for Cosense (formerly Scrapbox, a wiki service popular in Japan). It renders Mermaid blocks with mermaid-text and wraps the call in catch_unwind for now. I also fed CJK input to every diagram type the crate parses (30 sources); this was the only panic.

A small PR with regression tests follows. I used an AI coding assistant for the investigation and drafting; the repro and the diff were checked locally.

Activity

  1. leboiko commented on Oct 13, 2026

    @leboiko
    Owner

    Fixed on master by #51. Non-ASCII class names now use the shared UTF-8-safe keyword helper. The accompanying review also fixed Unicode whitespace in class methods/ER relationships and malformed class-method parentheses, with exact parser regressions.

    Thank you @worldnine for the original fix and reproductions. Your authored commit is preserved, and both changelogs credit your contribution. The fixes passed local stable/Rust 1.92 workspace tests and validation gates. This closes the source issue; no release has been published yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions