Skip to content

fix(render): render CJK emphasis and wrap CJK text per character - #135

Merged
backnotprop merged 3 commits into
plannotator:mainfrom
Manho:fix/cjk-markdown-rendering
Nov 15, 2026
Merged

backnotprop merged 3 commits into
plannotator:mainfrom
Manho:fix/cjk-markdown-rendering

Conversation

@Manho

@Manho Manho commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Chinese Markdown shows raw ** on screen because of one CommonMark rule, and long CJK runs wrap badly. This PR fixes both.

What was wrong

  • Bold stays literal. 改造成**“最强妖怪”**。 shows the **. CommonMark refuses a delimiter with punctuation on its inner side and a letter on its outer side. Chinese has no spaces to satisfy that, and the leftover ** can then pair with the wrong partner further along the line.
  • CJK wraps badly. A whole CJK run counted as one unbreakable token, so a long list item wrapped straight after its -, leaving the marker alone on a row.

What changed

  • cjk_emphasis.rs: puts a U+200B on the inner side of a refused delimiter before parsing, then strips it from the rendered text. tui-markdown runs its own parser and has no option for this, so the repair happens in its input. Alignment still runs against the original source, so offsets don't move. Delimiters in code, math and raw HTML are left alone. A block with no * returns at once, and the extra parse for literal ranges runs only when a delimiter needs repair.
  • wrap/breaks.rs: breaks CJK per character with basic kinsoku (opening punctuation stays with the text after it, closing punctuation with the text before it), and keeps Latin words whole. wrap.rs only calls it.

Results

  • At 60 columns, a CJK sample's repaired bold renders without **, and list items no longer leave the - alone on a row.
  • --annotate on quotes inside repaired bold and across a wrapped CJK list item stores exact originalText, with no U+200B in it.
  • --bench samples/big.md, 5 interleaved runs against main: render+align+wrap 339–407 ms (main 349–413 ms), and reflow overlaps. No measurable cost.

Testing

cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace (545 passed, 10 new), and python3 herdr/test-manifest.py, on macOS.

Known limits

  • protect doesn't exclude * inside link destinations or titles.
  • strip_marks also removes a U+200B that was already in the source, but only in a block that needed repair.

Not in this PR

The decoration part of the original PR (bullets, task boxes, quote bars, rules, hanging indents) moved to #152. That PR builds it from the parsed document instead of the rendered text.

Manho and others added 3 commits November 15, 2026 10:48
CommonMark's flanking rule leaves `**` literal when punctuation sits
inside and a CJK letter outside, which is common in Chinese text. A
zero-width mark inserted before parsing makes the delimiter valid; it is
removed from the output and alignment still uses the original source.

Break CJK runs per character with basic kinsoku and keep Latin words
whole.
…eds repair

Blocks without `*` return at once, and the literal-range parse runs only after a repairable delimiter is found. Text that was not protected skips the mark stripping.
@backnotprop
backnotprop force-pushed the fix/cjk-markdown-rendering branch from 353b25e to 62b8f91 Compare November 15, 2026 15:51
@backnotprop backnotprop changed the title fix(render): render CJK emphasis and leftover Markdown syntax fix(render): render CJK emphasis and wrap CJK text per character Nov 15, 2026
@backnotprop
backnotprop added this pull request to the merge queue Nov 15, 2026
Merged via the queue into plannotator:main with commit ccad87f Nov 15, 2026
3 checks passed
@backnotprop

Copy link
Copy Markdown
Contributor

Thanks @Manho, this is great work! I split it so the CJK part could land now: I rebased your commit, added an early return in protect for blocks without *, and moved the break rules into wrap/breaks.rs. The decoration is rebuilt from the parser's offsets in #152, with you as co-author. Your review there would be very welcome.

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.

2 participants