Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(chat): an Arabic reply that opens on a Latin name reads right-to-left

Chidambaram3187f132d9ce
diff --git a/docs/verification/bidi.md b/docs/verification/bidi.md
index 9d0aca7cde..28aa9c5b30 100644
--- a/docs/verification/bidi.md
+++ b/docs/verification/bidi.md
@@ -23,7 +23,11 @@ these checks. What is being verified is content direction, per block.
for the rest. The inner element holding the text must have `chat-text`:
`unicode-bidi` does not inherit from the outer bubble.
2. In the Arabic reply, the heading, paragraphs, list and quote all read
- right-to-left — bullets on the right, the quote's rule on the right.
+ right-to-left — bullets on the right, the quote's rule on the right. That
+ includes the paragraph opening on "Chrome 158" and the list whose items
+ open on "Node 24" and "pnpm 10": the script with more letters decides a
+ block, so a Latin name at the start does not turn it left-to-right, and a
+ bare address is not counted.
3. Its table's columns run right-to-left, and every header sits over its own
data. A header drifting to the opposite edge from its column means cells
are resolving individually instead of inheriting the table.
diff --git a/scripts/verify-bidi.ts b/scripts/verify-bidi.ts
index c266c847b6..ff315b4c79 100644
--- a/scripts/verify-bidi.ts
+++ b/scripts/verify-bidi.ts
@@ -18,6 +18,11 @@ const ARABIC_REPLY = [
"- عدم وجود cache للنتائج بين الاستدعاءات المتتالية",
"- تمرير المصفوفة كاملة إلى `sort()` في كل مرة (المشكلة الأهم)",
"",
+ "Chrome 158 هو الإصدار الذي أُجريت عليه القياسات، على Windows 11 و macOS 15، بتاريخ 2026-11-18.",
+ "",
+ "- **Node 24**: بيئة التشغيل المستخدمة في كل القياسات",
+ "- **pnpm 10**: مدير الحزم، والتفاصيل في https://pnpm.io/installation",
+ "",
"| الدالة | الزمن | النسبة |",
"| --- | --- | --- |",
"| buildIndex | 340ms | 66% |",
diff --git a/src/components/ChatMarkdown.test.ts b/src/components/ChatMarkdown.test.ts
index 0ca0a8d7c4..71236c0cc9 100644
--- a/src/components/ChatMarkdown.test.ts
+++ b/src/components/ChatMarkdown.test.ts
@@ -1290,7 +1290,33 @@ describe("a fence's wrap directive", () => {
describe("bidi: message content carries its own direction", () => {
const ARABIC = "مرحبا بالعالم";
- it("reads direction from the first strong letter, ignoring weak characters", () => {
+ it("goes with the script that has more letters, so an Arabic answer opening on a Latin name stays right-to-left (#6541)", () => {
+ expect(textDirection("Chrome 158 هو الإصدار الأحدث من المتصفح")).toBe("rtl");
+ expect(textDirection("Node.js بيئة تشغيل لجافاسكريبت على الخادم")).toBe("rtl");
+ expect(textDirection("OpenMausBot v0.1.169 صدر في 2026-11-18 مع إصلاحات كثيرة")).toBe("rtl");
+ expect(textDirection("The Arabic word مرحبا means hello")).toBe("ltr");
+ expect(textDirection("שלום is how you say hello in Hebrew, among other things")).toBe("ltr");
+ });
+
+ it("does not count a bare address, which is Latin in any language", () => {
+ // Counted, the address's letters would outnumber the Arabic ones.
+ expect(textDirection("راجع https://github.com/milind-soni/OpenMausBot/issues/6541 للتفاصيل")).toBe("rtl");
+ expect(textDirection("https://openmausbot.com/download هو رابط التحميل الرسمي")).toBe("rtl");
+ expect(textDirection("انظر www.example.com/a/very/long/path/to/the/release/notes هنا")).toBe("rtl");
+ expect(textDirection("https://example.com/page")).toBe("ltr");
+ });
+
+ it("gives a list whose items open on Latin names the direction of its text", () => {
+ const html = renderToStaticMarkup(createElement(ChatMarkdown, {
+ text: [
+ "- **Chrome**: متصفح من جوجل يعمل على كل الأنظمة",
+ "- **Firefox**: متصفح مفتوح المصدر من موزيلا",
+ ].join("\n"),
+ }));
+ expect(html).toMatch(/<ul dir="rtl"/);
+ });
+
+ it("breaks a tie with the first strong letter, ignoring weak characters", () => {
expect(textDirection("مرحبا")).toBe("rtl");
expect(textDirection("hello")).toBe("ltr");
expect(textDirection("")).toBe("ltr");
diff --git a/src/components/ChatMarkdown.tsx b/src/components/ChatMarkdown.tsx
index 5717ad2ae2..fb42684b64 100644
--- a/src/components/ChatMarkdown.tsx
+++ b/src/components/ChatMarkdown.tsx
@@ -155,10 +155,22 @@ function unwrapLinkedImages() {
// directions falls back to LTR. JS regexes cannot match Bidi_Class, so these are the blocks Unicode reserves for
// right-to-left letters.
const RTL_LETTER = /[\u0590-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF\u{10800}-\u{10FFF}\u{1E800}-\u{1EFFF}]/u;
+/** A bare address is Latin whatever language the sentence around it is in, so its letters are not counted. */
+const ADDRESS = /(?:[a-z][a-z0-9+.-]*:\/\/|www\.)\S+/giu;
+/** The script with more letters decides; the first letter only breaks a tie. An Arabic answer often opens on a name
+ * ("Chrome 158 \u0647\u0648 \u0627\u0644\u0625\u0635\u062F\u0627\u0631 \u0627\u0644\u0623\u062D\u062F\u062B", "**Node.js** \u0628\u064A\u0626\u0629 \u062A\u0634\u063A\u064A\u0644"), and the first letter alone laid that whole paragraph or
+ * list out left-to-right: the Arabic words, numbers and Latin names in it reordered, and its bullets went left (#6541). */
export function textDirection(value: string): "rtl" | "ltr" {
- const strong = /\p{Letter}/u.exec(value);
- return strong && RTL_LETTER.test(strong[0]) ? "rtl" : "ltr";
+ let rtl = 0, ltr = 0, first: "rtl" | "ltr" | null = null;
+ for (const [letter] of value.replace(ADDRESS, " ").matchAll(/\p{Letter}/gu)) {
+ const direction = RTL_LETTER.test(letter) ? "rtl" : "ltr";
+ first ??= direction;
+ if (direction === "rtl") rtl += 1;
+ else ltr += 1;
+ }
+ if (rtl !== ltr) return rtl > ltr ? "rtl" : "ltr";
+ return first ?? "ltr";
}
interface HastNode {