Skip to content

Commit 4adb4fe

Browse files
committed
fix(chat): ignore markdown code during direction detection
1 parent 39da1ee commit 4adb4fe

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

‎apps/mobile/src/lib/textDirection.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ describe("resolveTextDirection", () => {
2424

2525
it("ignores inline and fenced code when resolving markdown prose", () => {
2626
expect(resolveMarkdownTextDirection("`npm` שלום")).toBe("rtl");
27+
expect(resolveMarkdownTextDirection("``English `code` inside`` שלום")).toBe("rtl");
2728
expect(resolveMarkdownTextDirection("```sh\nnpm test\n```\n\nשלום")).toBe("rtl");
2829
expect(resolveMarkdownTextDirection("`שלום` English prose")).toBe("ltr");
2930
});
3031

32+
it("ignores indented code blocks when resolving markdown prose", () => {
33+
expect(resolveMarkdownTextDirection(" English command\n\nשלום")).toBe("rtl");
34+
expect(resolveMarkdownTextDirection("> English command\n\nשלום")).toBe("rtl");
35+
});
36+
3137
it("does not close a long fence at a shorter nested marker", () => {
3238
expect(
3339
resolveMarkdownTextDirection(

‎apps/mobile/src/lib/textDirection.ts‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ export type TextDirection = "ltr" | "rtl";
33
const LETTER_CHARACTER = /^\p{Letter}$/u;
44
const RTL_SCRIPT_CHARACTER =
55
/^[\u0590-\u08ff\ufb1d-\ufdff\ufe70-\ufeff\u{10800}-\u{10fff}\u{1e800}-\u{1eeff}]$/u;
6-
const INLINE_MARKDOWN_CODE = /`[^`\n]*`/g;
76
const GITHUB_ALERT_MARKER =
87
/^ {0,3}(?:> {0,3})+\[!(?:NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][\t ]*$/gimu;
8+
const INDENTED_CODE_LINE = /^(?: {4}|\t| {0,3}(?:> ?)+(?: {4}|\t))/;
99

1010
function stripFencedMarkdownCode(markdown: string): string {
1111
let fence: { character: "`" | "~"; length: number } | undefined;
@@ -22,7 +22,7 @@ function stripFencedMarkdownCode(markdown: string): string {
2222
continue;
2323
}
2424

25-
proseLines.push(line);
25+
if (!INDENTED_CODE_LINE.test(line)) proseLines.push(line);
2626
continue;
2727
}
2828

@@ -40,6 +40,47 @@ function stripFencedMarkdownCode(markdown: string): string {
4040
return proseLines.join("\n");
4141
}
4242

43+
function stripInlineMarkdownCode(markdown: string): string {
44+
let prose = "";
45+
let index = 0;
46+
47+
while (index < markdown.length) {
48+
if (markdown[index] !== "`") {
49+
prose += markdown[index];
50+
index += 1;
51+
continue;
52+
}
53+
54+
const openingStart = index;
55+
while (markdown[index] === "`") index += 1;
56+
const delimiterLength = index - openingStart;
57+
let searchIndex = index;
58+
let closingEnd: number | undefined;
59+
60+
while (searchIndex < markdown.length) {
61+
const closingStart = markdown.indexOf("`", searchIndex);
62+
if (closingStart === -1) break;
63+
64+
let runEnd = closingStart;
65+
while (markdown[runEnd] === "`") runEnd += 1;
66+
if (runEnd - closingStart === delimiterLength) {
67+
closingEnd = runEnd;
68+
break;
69+
}
70+
searchIndex = runEnd;
71+
}
72+
73+
if (closingEnd !== undefined) {
74+
index = closingEnd;
75+
continue;
76+
}
77+
78+
prose += markdown.slice(openingStart, index);
79+
}
80+
81+
return prose;
82+
}
83+
4384
export function resolveTextDirection(text: string): TextDirection {
4485
for (const character of text) {
4586
if (!LETTER_CHARACTER.test(character)) continue;
@@ -50,8 +91,6 @@ export function resolveTextDirection(text: string): TextDirection {
5091

5192
export function resolveMarkdownTextDirection(markdown: string): TextDirection {
5293
return resolveTextDirection(
53-
stripFencedMarkdownCode(markdown)
54-
.replace(GITHUB_ALERT_MARKER, "")
55-
.replace(INLINE_MARKDOWN_CODE, ""),
94+
stripInlineMarkdownCode(stripFencedMarkdownCode(markdown).replace(GITHUB_ALERT_MARKER, "")),
5695
);
5796
}

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,12 +2148,9 @@ function ChatMarkdown({
21482148
// text under a colored title — which is how the host renders it.
21492149
return (
21502150
<div role="note" className={cn("my-1 border-s-2 ps-3", alert.borderClassName)}>
2151-
<p
2152-
className={cn("flex items-center gap-1.5 font-medium", alert.titleClassName)}
2153-
dir="ltr"
2154-
>
2151+
<p className={cn("flex items-center gap-1.5 font-medium", alert.titleClassName)}>
21552152
<alert.Icon aria-hidden className="size-3.5 shrink-0" />
2156-
{alert.label}
2153+
<bdi>{alert.label}</bdi>
21572154
</p>
21582155
{children}
21592156
</div>

‎apps/web/src/components/chat/MessagesTimeline.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ describe("MessagesTimeline", () => {
892892
);
893893

894894
expect(markup).toMatch(/<div role="note" class="[^"]*border-s-2[^"]*ps-3/);
895-
expect(markup).toMatch(/<p class="[^"]*font-medium[^"]*" dir="ltr"/);
895+
expect(markup).toContain("<bdi>Note</bdi>");
896896
expect(markup).not.toContain("border-l-2");
897897
});
898898

0 commit comments

Comments
 (0)