Skip to content

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

fix(chat): count sentence-final words; digits are not RTL words

Chidambaram3471bd881919
diff --git a/src/components/ChatMarkdown.test.ts b/src/components/ChatMarkdown.test.ts
index d2fd01dfce..fd6f527611 100644
--- a/src/components/ChatMarkdown.test.ts
+++ b/src/components/ChatMarkdown.test.ts
@@ -1316,6 +1316,18 @@ describe("bidi: message content carries its own direction", () => {
expect(textDirection("Set process.env.NODE_ENV in config.ts for مرحبا")).toBe("ltr");
});
+ it("counts a word that ends a sentence, and still skips a dotted technical token", () => {
+ expect(textDirection("Hello. Welcome. مرحبا")).toBe("ltr");
+ expect(textDirection("Done! Thanks, really? مرحبا بك")).toBe("ltr");
+ expect(textDirection("افتح config.ts. ثم احفظ")).toBe("rtl");
+ });
+
+ it("does not count digits as words, Arabic-Indic ones included", () => {
+ expect(textDirection("١٢٣ hello")).toBe("ltr");
+ expect(textDirection("١٢٣ مرحبا")).toBe("rtl");
+ expect(textDirection("٤٥٦ ٧٨٩ hello world")).toBe("ltr");
+ });
+
it("does not let an address swallow the RTL text glued to it", () => {
expect(textDirection("API https://x.io/aوالتفاصيل هنا")).toBe("rtl");
expect(textDirection("التوثيق (https://pnpm.io/installation) فيه كل التفاصيل")).toBe("rtl");
diff --git a/src/components/ChatMarkdown.tsx b/src/components/ChatMarkdown.tsx
index af19c14ee0..6bd6e60505 100644
--- a/src/components/ChatMarkdown.tsx
+++ b/src/components/ChatMarkdown.tsx
@@ -161,8 +161,9 @@ const TECHNICAL = /[\d._/\\@=]|^-|\p{Ll}\p{Lu}/u;
export function textDirection(value: string): "rtl" | "ltr" {
const text = value.replace(ADDRESS, " ");
let rtl = 0, ltr = 0, first: "rtl" | "ltr" | null = null;
- for (const word of text.split(/\s+/)) {
- const direction = RTL_LETTER.test(word) ? "rtl"
+ for (const raw of text.split(/\s+/)) {
+ const word = raw.replace(/[.,;:!?]+$/u, "");
+ const direction = [...word].some((char) => RTL_LETTER.test(char) && /\p{Letter}/u.test(char)) ? "rtl"
: /\p{Letter}/u.test(word) && !TECHNICAL.test(word) ? "ltr"
: null;
if (!direction) continue;