본문으로 건너뛰기
A

dsh-fa-bidi

alifirozjaei/dsh-fa-bidi

Resolves right-to-left direction per block for Persian chat text and the Markdown preview, including lists, quotes, tables and code comments.

설치

dsh plugin --profile web add github:alifirozjaei/dsh-fa-bidi

README

dsh-fa-bidi

License: MIT dsh-plugin

Right-to-left layout for Persian chat text, decided per block and kept correct while an answer streams in.

CSS has exactly one automatic direction mechanism — unicode-bidi: plaintext — and it resolves each paragraph from its first strong character. A Persian paragraph that opens with a Latin product name therefore lays out left-to-right, lists keep their indent on the wrong side, and quote borders stay on the left. This plugin computes the decision from the block's own letters, writes it as a real dir attribute, and fixes the geometry that the app writes in physical CSS.

The decision

For every prose block (paragraph, list item, heading, quote, definition, table cell, and plain-text message container):

  1. A first strong character that is Persian/Arabic means RTL — the Unicode answer, so short blocks stay predictable.

  2. No Persian letters at all means LTR.

  3. Otherwise the block is RTL when Persian letters are 30% or more of its letters. That keeps

    DeepSeek Harness یک ابزار کدنویسی است that helps you build.

    right-to-left (35% Persian) while leaving an English sentence that quotes one Persian word left-to-right (17%).

Code, formulas and hidden accessibility text are excluded from the letter count, so a Persian sentence with an equation inside is still judged by its words. Blocks that already carry a dir attribute from the app are left alone.

The 30% share is RTL_LETTER_SHARE at the top of lib/client.js: lower it toward 0.1 if any Persian word should flip a block, raise it toward 0.5 to require a Persian majority.

Where it applies

Two surfaces, both addressed by app-owned attributes rather than hashed class names:

SurfaceAttributeWhat is resolved
Chat transcript[data-chat-flow-kind], [data-chat-group-part], [data-turn-process-answer]messages, reasoning, tool rows
Sidebar Markdown preview[data-document-markdown]headings, paragraphs, lists, quotes, tables, frontmatter values, code-block comments

The document preview keeps its own geometry, so the same rules apply there: Persian paragraphs and headings go right-to-left, lists take the indent of their items, quotes move their border to the right edge, frontmatter values follow their own text while field names stay left, and a Persian comment inside a code fence is isolated exactly as it is in chat.

Not scoped yet: the plan panel, agent-preset pages, and other Markdown surfaces that MarkdownText renders outside a chat row. They keep the app's default direction.

What follows the direction

  • Paragraph, heading, list-item, quote, definition and table-cell alignment (dir + text-align: start).
  • List containers take the majority direction of their items; on a tie they take the direction of their own text. Indent is applied to both banks, so a Latin item inside a Persian list keeps its marker inside the list.
  • Quote borders and padding mirror to the right edge.
  • Task-list checkboxes move to the right of their text.

What stays left-to-right

  • Code blocks and inline code — a Persian comment inside code does not flip the line, and a - b never turns into b - a.
  • Formulas (.katex, .katex-display, mjx-container) — isolated LTR, so operators and fractions keep their order inside a Persian paragraph.
  • Table column order — cells follow their own text, the grid does not mirror, so numeric columns stay where the author put them.

Composer

The message box auto-detects while you type through unicode-bidi: plaintext on [data-composer-input], without touching the rich-text editor's DOM, so the caret and input method behave normally.

Code blocks

A code block is one left-to-right surface: commands, indentation and operators keep their order, and Persian inside code is treated as an embedded right-to-left run.

Two defects showed up with Persian code blocks, and both are fixed here:

The comment tail is isolated. A line that has Persian before the comment marker and a Persian comment after it used to come out scrambled: the marker and the spaces around it are neutral characters, so the bidi algorithm merged the placeholder and the comment into one right-to-left run and laid the run out backwards — the two pieces swapped places and < > mirrored. The plugin wraps just that comment tail in an isolated RTL segment, so the code keeps the order it was typed in and the comment reads right-to-left. Only that exact shape is touched; a comment line that is Persian from the start already renders correctly and is left alone, and an all-English block is never modified.

Persian words stop breaking mid-word. The app sets word-break: break-all on code, which splits words wherever the line ends. Persian text now wraps at its spaces first (word-break: normal + overflow-wrap: anywhere), while long code tokens still break instead of overflowing.

Code block before and after

No flash when switching conversations

Direction is a DOM fact — dir on each block — because CSS cannot decide "right-to-left although this paragraph starts in English". That means the plugin has to write it after the app renders, and the obvious implementation (wait for a timer, then sweep) paints the new conversation once in the default left-to-right layout and flips it a moment later.

The sweep therefore runs where it can still affect the first frame: a MutationObserver callback is a microtask, so it fires after React commits the new transcript and before the browser paints it. Inserted nodes get their direction there and then; only the batched work that can afford to lag (code comment isolation, list re-checks during a long stream) stays on the timer.

Relation to dsh-fa-rtl

dsh-fa-rtl can stay installed. It applies unicode-bidi: plaintext to the same elements; because plaintext ignores a dir attribute, this plugin turns it back to normal on those elements (a single !important declaration) and takes over the decision. Nothing about that plugin's CSS is edited or removed.

Verification

Before and after: the same chat blocks, first with plaintext auto-direction, then with per-block decisions

Checked in Chromium against the app's own markdown stylesheet (extracted from the shipped build) with dsh-fa-rtl's rule present, on 18 blocks:

CaseDirection
Persian onlyrtl
DeepSeek Harness یک ابزار کدنویسی است that helps you build.rtl
English onlyltr
English quoting «سلام»ltr
API یک رابط برنامه‌نویسی ساده است.rtl
Persian list, Latin item, Persian-first itemrtl / ltr / rtl
Persian quotertl, border on the right
Persian paragraph with inline formulartl, formula ltr + isolated
Code block with Persian commentltr, a - b preserved
Persian and Latin table cellsrtl / ltr, columns unchanged
Persian and English plain-text user messagesrtl / ltr

Lists: padding-left and padding-right both 18px on RTL containers, quote border 2px on the right, 0 on the left.

Code blocks were checked against a Shiki-shaped DOM (highlighted <span class="line"> lines injected through innerHTML, syntax colors spread over several token spans): the comment tail is wrapped exactly once, the line's text content is unchanged so copy still copies the original code, the wrapper survives three full re-injections and grows correctly while a comment streams in, an all-English block is never touched, and the direction assertions above still hold.

From GitHub

dsh plugin add github:alifirozjaei/dsh-fa-bidi

Then reload the app. The bundle patch inserts the plugin row; nothing else in the profile changes.

Install

Add this directory to a DSH profile with Plugins → Add plugin. The patch inserts the fa-bidi row; the Host half is empty because every decision needs the live DOM.

Known limits

  • Direction is inferred per block; a deliberately mixed paragraph still picks one base direction, and the browser's bidi algorithm orders the runs inside it.
  • Interface chrome outside the chat transcript (menus, settings, session titles) is not touched.
  • Text inside images, canvases and the terminal is out of reach.

관련 플러그인