Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(chat): right-align Arabic assistant replies with logical text-align

koeriacd93b21a133a
diff --git a/apps/desktop/src/components/Markdown.tsx b/apps/desktop/src/components/Markdown.tsx
index 9d8a9eaab..c4e1a997d 100644
--- a/apps/desktop/src/components/Markdown.tsx
+++ b/apps/desktop/src/components/Markdown.tsx
@@ -1,6 +1,7 @@
import { useChatSessionCapability } from "../hooks/use-chat-session-capability";
import {
createContext,
+ createElement,
Fragment,
isValidElement,
memo,
@@ -832,18 +833,6 @@ function VideoBlock({
);
}
-const markdownComponents: Components = {
- pre: PreBlock,
- code: InlineCode,
- a: Anchor,
- img: MarkdownImage,
- audio: AudioBlock,
- video: VideoBlock,
- table: Table,
- p: BidiParagraph,
- li: BidiListItem,
-};
-
// #2388: mixed RTL/LTR paragraphs must not take their base direction from the
// first strong character alone. Each block paragraph picks its direction from
// the whole text (majority strong script), so a Persian paragraph that opens
@@ -866,14 +855,66 @@ function textOfChildren(node: unknown): string {
return "";
}
-function BidiParagraph({ children, ...rest }: ComponentProps<"p">) {
- return <p {...rest} dir={directionAttr(textOfChildren(children))}>{children}</p>;
+// #2635: `text-align: start` follows the element's dir. A physical
+// `text-align: left` inherited from an ancestor (or a platform default)
+// would keep an Arabic-only reply left-aligned even when dir=rtl is set.
+// User message paragraphs already use this logical alignment.
+function bidiDirAndStyle(children: ReactNode, style?: CSSProperties) {
+ return {
+ dir: directionAttr(textOfChildren(children)),
+ style: { ...style, textAlign: "start" as const },
+ };
}
-function BidiListItem({ children, ...rest }: ComponentProps<"li">) {
- return <li {...rest} dir={directionAttr(textOfChildren(children))}>{children}</li>;
+type BidiTag = "p" | "li" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote";
+
+function makeBidi<T extends BidiTag>(tag: T) {
+ function BidiElement({
+ children,
+ style,
+ node: _node,
+ ...rest
+ }: ComponentProps<T> & { node?: unknown }) {
+ return createElement(
+ tag,
+ { ...rest, ...bidiDirAndStyle(children, style) },
+ children,
+ );
+ }
+ BidiElement.displayName = `Bidi(${tag})`;
+ return BidiElement;
}
+const BidiParagraph = makeBidi("p");
+const BidiListItem = makeBidi("li");
+const BidiHeading1 = makeBidi("h1");
+const BidiHeading2 = makeBidi("h2");
+const BidiHeading3 = makeBidi("h3");
+const BidiHeading4 = makeBidi("h4");
+const BidiHeading5 = makeBidi("h5");
+const BidiHeading6 = makeBidi("h6");
+const BidiBlockquote = makeBidi("blockquote");
+
+const markdownComponents: Components = {
+ pre: PreBlock,
+ code: InlineCode,
+ a: Anchor,
+ img: MarkdownImage,
+ audio: AudioBlock,
+ video: VideoBlock,
+ table: Table,
+ p: BidiParagraph,
+ li: BidiListItem,
+ h1: BidiHeading1,
+ h2: BidiHeading2,
+ h3: BidiHeading3,
+ h4: BidiHeading4,
+ h5: BidiHeading5,
+ h6: BidiHeading6,
+ blockquote: BidiBlockquote,
+};
+
+
// The grammar the block splitter parses with, plus the renderer-only rewrite
// of local image paths. `remarkLocalImagePaths` transforms URLs and moves no
// block boundary, so the splitter has no reason to run it.
diff --git a/apps/desktop/src/styles/prose.css b/apps/desktop/src/styles/prose.css
index 4b312eeb9..7d56599b7 100644
--- a/apps/desktop/src/styles/prose.css
+++ b/apps/desktop/src/styles/prose.css
@@ -55,6 +55,20 @@
margin: 0.65em 0;
}
+/* #2635: logical alignment follows each block's dir so Arabic replies
+ align to the right even if an ancestor used a physical `left`. */
+.prose-chat p,
+.prose-chat li,
+.prose-chat h1,
+.prose-chat h2,
+.prose-chat h3,
+.prose-chat h4,
+.prose-chat h5,
+.prose-chat h6,
+.prose-chat blockquote {
+ text-align: start;
+}
+
.prose-chat p + p {
margin-top: 0.8em;
}
@@ -93,7 +107,7 @@
.prose-chat .footnotes ol {
margin: 0.3em 0;
- padding-left: 1.3em;
+ padding-inline-start: 1.3em;
}
.prose-chat .footnotes li {
@@ -170,7 +184,7 @@
.prose-chat ul,
.prose-chat ol {
margin: 0.65em 0;
- padding-left: 1.4em;
+ padding-inline-start: 1.4em;
}
.prose-chat ul {
@@ -184,7 +198,7 @@
.prose-chat li {
margin: 0.28em 0;
- padding-left: 0.15em;
+ padding-inline-start: 0.15em;
}
.prose-chat li::marker {
@@ -206,7 +220,7 @@
}
.prose-chat .contains-task-list {
- padding-left: 0.15em;
+ padding-inline-start: 0.15em;
list-style: none;
}
@@ -216,7 +230,7 @@
align-items: flex-start;
gap: 0.45em;
margin: 0.28em 0;
- padding-left: 0;
+ padding-inline-start: 0;
}
.prose-chat .task-list-item > input[type="checkbox"] {
diff --git a/apps/desktop/test/markdown-bidi-alignment.test.mjs b/apps/desktop/test/markdown-bidi-alignment.test.mjs
new file mode 100644
index 000000000..7152d95cf
--- /dev/null
+++ b/apps/desktop/test/markdown-bidi-alignment.test.mjs
@@ -0,0 +1,92 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { createInstance } from "i18next";
+import { I18nextProvider } from "react-i18next";
+import { catalogs } from "@pi-desktop/i18n";
+import { createServer } from "vite";
+import { fileURLToPath } from "node:url";
+import { readFile } from "node:fs/promises";
+
+/*
+ * Issue #2635: Arabic assistant replies stayed left-aligned on Windows even
+ * when the paragraph was Arabic-only. #2389 already set dir from majority
+ * script and used text-align:start on user bubbles, but Markdown blocks only
+ * set dir. This renders the production Markdown pipeline and checks that
+ * Arabic blocks also get logical alignment, including headings.
+ */
+test("Arabic Markdown blocks get dir=rtl and text-align:start", async () => {
+ const styles = await readFile(
+ new URL("../src/styles/prose.css", import.meta.url),
+ "utf8",
+ );
+ assert.match(styles, /\.prose-chat p,\s*\.prose-chat li,[\s\S]*?text-align:\s*start/);
+ assert.match(styles, /\.prose-chat ul,\s*\.prose-chat ol\s*\{[^}]*padding-inline-start/);
+
+ const server = await createServer({
+ root: fileURLToPath(new URL("..", import.meta.url)),
+ configFile: false,
+ server: { middlewareMode: true, hmr: false, ws: false },
+ esbuild: { jsx: "automatic" },
+ appType: "custom",
+ optimizeDeps: { noDiscovery: true, include: [] },
+ });
+ const originalDocument = globalThis.document;
+ try {
+ globalThis.document = { documentElement: { dataset: {} } };
+ const { Markdown } = await server.ssrLoadModule(
+ "/src/components/Markdown.tsx",
+ );
+ const i18n = createInstance();
+ await i18n.init({
+ lng: "en",
+ resources: { en: { translation: catalogs.en } },
+ });
+ const render = (source) =>
+ renderToStaticMarkup(
+ createElement(
+ I18nextProvider,
+ { i18n },
+ createElement(Markdown, { source }),
+ ),
+ );
+
+ const start = (html) =>
+ /style="[^"]*text-align:start/.test(html) ||
+ /style="[^"]*text-align: start/.test(html);
+ const hasDir = (html, tag, dir) =>
+ new RegExp(`<${tag}[^>]*dir="${dir}"`).test(html);
+ const noNodeAttr = (html) => !/\snode="/.test(html);
+
+ const arabic = render("مرحبا، هذه فقرة عربية بالكامل.");
+ assert.ok(hasDir(arabic, "p", "rtl"), `Arabic paragraph dir missing: ${arabic}`);
+ assert.ok(start(arabic), `Arabic paragraph text-align missing: ${arabic}`);
+ assert.ok(noNodeAttr(arabic), `hast node leaked to the DOM: ${arabic}`);
+
+ const mixed = render("hello English paragraph 123!\n\nمرحبا هذه فقرة عربية");
+ assert.ok(hasDir(mixed, "p", "ltr") && hasDir(mixed, "p", "rtl"), mixed);
+ assert.equal([...mixed.matchAll(/<p\b/g)].length, 2);
+ assert.ok(start(mixed), mixed);
+
+ const emphasis = render("**مرحبا** هذه فقرة");
+ assert.ok(hasDir(emphasis, "p", "rtl") && start(emphasis), emphasis);
+
+ const heading = render("# عنوان عربي\n\nفقرة عربية");
+ assert.ok(hasDir(heading, "h1", "rtl") && hasDir(heading, "p", "rtl"), heading);
+ assert.ok(start(heading), heading);
+ assert.ok(noNodeAttr(heading), heading);
+
+ const quote = render("> اقتباس عربي");
+ assert.ok(hasDir(quote, "blockquote", "rtl") && start(quote), quote);
+
+ const list = render("- عنصر قائمة عربي");
+ assert.ok(hasDir(list, "li", "rtl") && start(list), list);
+
+ const english = render("Just an English paragraph.");
+ assert.ok(hasDir(english, "p", "ltr") && start(english), english);
+ } finally {
+ globalThis.document = originalDocument;
+ await server.close();
+ }
+});
diff --git a/scripts/e2e/bidi-direction.tsx b/scripts/e2e/bidi-direction.tsx
index 9652c48be..abd234a4e 100644
--- a/scripts/e2e/bidi-direction.tsx
+++ b/scripts/e2e/bidi-direction.tsx
@@ -26,7 +26,7 @@ export async function bidiDirectionProbe(i18n: ReturnType<typeof createInstance>
const sessionId = "bidi-direction-probe";
const messages = [
message("bidi-user", "user", "hello English paragraph 123!\n\nپژگچ این پاراگراف فارسی است ؟"),
- message("bidi-assistant", "assistant", "English **paragraph** 123!\n\nپژگچ **پاسخ** فارسی ؟ ؛\n\n- English item\n- پژگچ فارسی"),
+ message("bidi-assistant", "assistant", "English **paragraph** 123!\n\nپژگچ **پاسخ** فارسی ؟ ؛\n\n- English item\n- پژگچ فارسی\n\n# عنوان عربي"),
];
try {
useAppStore.setState({ activeSessionId: sessionId, page: "chat" });
@@ -114,6 +114,25 @@ export async function bidiDirectionProbe(i18n: ReturnType<typeof createInstance>
assert(bubble.textContent === messages[0].content, "paragraph splitting changed user text");
const rtlMarkdown = markdownParagraphs[1];
assert(rtlMarkdown && glyph(rtlMarkdown, "؟") < glyph(rtlMarkdown, "پ"), "Markdown punctuation order is wrong");
+ const rtlAlign = getComputedStyle(rtlMarkdown).textAlign;
+ assert(
+ rtlAlign === "start" || rtlAlign === "right",
+ `RTL Markdown paragraph text-align was ${rtlAlign}`,
+ );
+ const rtlItem = [...host.querySelectorAll<HTMLElement>(".prose-chat li")]
+ .find((item) => item.dir === "rtl");
+ const itemAlign = rtlItem && getComputedStyle(rtlItem).textAlign;
+ assert(
+ itemAlign === "start" || itemAlign === "right",
+ `RTL Markdown list item text-align was ${itemAlign}`,
+ );
+ const heading = host.querySelector<HTMLElement>(".prose-chat h1");
+ assert(heading?.dir === "rtl", `Markdown heading dir was ${heading?.dir}`);
+ const headingAlign = heading && getComputedStyle(heading).textAlign;
+ assert(
+ headingAlign === "start" || headingAlign === "right",
+ `RTL Markdown heading text-align was ${headingAlign}`,
+ );
assert(
getComputedStyle(editor).unicodeBidi === "plaintext",
`composer unicode-bidi was ${getComputedStyle(editor).unicodeBidi}`,