Skip to content

fix(react): apply CommonMark flanking rules to composer emphasis - #1376

Open
ishan-one8 wants to merge 2 commits into
RocketChat:developfrom
ishan-one8:fix/composer-intraword-emphasis
Open

ishan-one8 wants to merge 2 commits into
RocketChat:developfrom
ishan-one8:fix/composer-intraword-emphasis

Conversation

@ishan-one8

Copy link
Copy Markdown

Apply CommonMark flanking rules to composer emphasis

Root cause. appendMarkdown in packages/react/src/lib/contentEditableComposer.js treated any _, *, __ or ** as an emphasis delimiter and paired it with the next identical marker, without ever checking whether that delimiter run was allowed to open or close emphasis. CommonMark decides that with the left/right-flanking rules and additionally forbids intra-word _, so any underscore or asterisk with another one later in the line opened emphasis.

Fixes #1372

Acceptance Criteria fulfillment

  • my_file_name.js, call foo_bar_baz() now and 2 * 3 * 4 = 24 render literally again
  • **bold**, *italic*, __init__, a _b_ c and intra-word foo*bar*baz still render as emphasis
  • Ships with a test that fails before the fix and passes after
  • Inline code, strikethrough, links and <br> paths untouched
  • yarn build still produces dist/cjs and dist/esm (exit 0)

Video/Screenshots

Not applicable — no visible UI change beyond the corrected text rendering, which the tests cover.

PR Test Details

Two commits:

  1. test(react): emit CommonJS under the test env so jest can run — babel.config.js had modules: false unconditionally, so babel-jest handed ESM to jest and every test file died with "Cannot use import statement outside a module". It now emits CommonJS only when api.env('test') is true; the rollup build still gets native ES modules, which is why the bundle output is unchanged. Also adds the missing test script. This is the same area as fix(react): src/index.test.js fails on develop due to missing swiper/element/bundle module #1191.
  2. fix(react): apply CommonMark flanking rules to composer emphasis — adds canOpenEmphasis / canCloseEmphasis / findClosingMarker and uses them when selecting a delimiter. When the nearest matching marker cannot close, the scan continues to the next one instead of giving up.

Before the fix, the new test fails:

✕ does not treat underscores inside a word as emphasis
✕ does not open emphasis on a marker followed by whitespace
    Expected: "2 * 3 * 4 = 24"
    Received: "2 <em> 3 </em> 4 = 24"

After it, yarn test in packages/react passes 3/3.

I also diffed the fixed renderer against the reference commonmark package across 13 emphasis cases — including foo*bar*baz, snake_case and _real_ emphasis, _leading, trailing_ and 5 * 6 — and all 13 now match.

This branch has not been deployed

No deployments
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.

bug: AI composer renders intra-word _ and * as italic/bold (e.g. my_file_name.js -> my<em>file</em>name.js)

1 participant