Skip to content

fix(chat): render mixed RTL/LTR paragraphs with content-aware direction - #2389

Merged
vastsa merged 4 commits into
vastsa:mainfrom
koeria:fix/issue-2388-bidi
Nov 20, 2026
Merged

vastsa merged 4 commits into
vastsa:mainfrom
koeria:fix/issue-2388-bidi

Conversation

@koeria

@koeria koeria commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

根因 (Root cause)

聊天段落依赖第一个强方向字符时,英文开头、波斯文占多数的段落会被判成 LTR。原分类还漏掉 پچژگ,并把 ؟、؛ 等标点当作 RTL 强字符。整条消息共用一个方向也无法正确显示英文和波斯文分段。

修复 (Fix)

  • 按 Unicode 字母统计 LTR/RTL 多数方向,覆盖波斯字母、RTL 历史文字、表现形式及补充平面字母;数字、标点、组合符和方向控制符不投票。无字母时不指定方向,平票时采用 LTR。
  • 用户消息按换行分段,使用独立的段落方向和起始对齐;段落内部保留真实 LinkifiedText、附件位置及源码偏移。
  • Markdown 的生产段落与列表项分别计算方向;行内链接、代码及强调内容保持 bidi 隔离。
  • composer 与消息编辑框使用 unicode-bidi: plaintext,按每段 first-strong 规则编辑。
  • 移除原来的三项源码正则“接线验证”,改用真实 ChatTranscript → MessageRow / Markdown、ComposerInput 与生产编辑器绘制函数的 Chromium DOM 验证。

验证 (Verification)

  • desktop tsc -p apps/desktop/tsconfig.json --noEmit 通过;另用继承 desktop 配置的临时项目包含新 scripts/e2e/bidi-direction.tsx,生产组件和新探针一起通过 strict typecheck。
  • 下载官方 Unicode 17.0 UnicodeData.txt,对生产 countStrongCharacters 检查全部 40,575 条显式记录,含 2,611 个 RTL 字母,零分类差异;26 个 Bidi_Class=ON 的 modifier letters 也不投票(本地 Node Unicode 版本 17.0)。
  • text-direction、session-message-presentation、session-reference-ui、markdown-* 聚焦回归:56/56 通过。
  • 使用仓库 electron-vite 4.0.1 完成 desktop main/preload/renderer 构建。
  • 已安装的 Chromium headless-shell 执行 CHROMIUM_BINARY=<installed-browser> node scripts/e2e-bidi.mjs 通过:同一气泡与 Markdown 分别得到 LTR/RTL 段落,列表项方向独立,行内强调隔离;生产 composer 绘制混合草稿后内容往返不变,Range 实际字形位置验证英文与波斯文标点顺序,气泡和 Markdown 的波斯问号位置也经过字形几何断言。
  • Biome 2.5.13 按仓库 biome.json 范围 lint、style tokens、git diff --check 通过;仓库 formatter 禁用,未进行全文件格式化。
  • Electron 完整 transcript E2E 因环境未安装 Electron 二进制未执行;上述独立 Chromium 路径执行真实生产组件与构建样式,不依赖 Electron 或 native host。

Fixes #2388

Paragraph base direction followed the first strong character, so a
Persian/Arabic paragraph opening with an English word rendered LTR and
its RTL runs, punctuation, and parentheses scrambled (vastsa#2388).

- New lib/text-direction counts strong LTR/RTL characters per paragraph;
  the majority script picks the base direction, neutral text stays auto.
- Markdown paragraph and list-item renderers set the computed dir on
  every rendered block.
- User message bubbles get the same computed dir on the text container.
- Inline chips, links, and prose emphasis isolate (unicode-bidi) so
  surrounding punctuation cannot migrate across them; the composer and
  the message-edit textarea use per-paragraph plaintext auto direction.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现有 8 个 text-direction 测试通过,但直接执行生产函数发现 RTL_STRONG 漏掉波斯字母 پ/چ/ژ/گ,并把阿拉伯问号/分号等中性标点计作 strong RTL。实测 countStrongCharacters("پژگچ") 返回 {ltr:0,rtl:0},directionAttr("... ؟ ؛") 返回 rtl;"hello چپ چپ چپ چپ چپ" 返回 {ltr:5,rtl:0},强制 ltr,复现本 PR 要修的英文开头、波斯语占多数错误方向。

请可靠区分 Unicode 强方向字母、数字、标点、组合符,覆盖完整波斯字母并增加以上反例。另外 MessageRow 对整条多段消息只算一个 dir,无法满足每段独立方向;请覆盖同一消息包含英文段落与波斯文段落的真实渲染路径。现有后 3 项是源码正则断言,不能证明实际 DOM 和混排顺序;请增加组件/浏览器用户路径验证,覆盖气泡、Markdown 和 composer 的方向与标点表现后再合入。

@vastsa
vastsa merged commit d10bcb7 into vastsa:main Nov 20, 2026
5 checks passed
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] Mixed Persian/English (RTL/LTR) text is not rendered correctly in chat

2 participants