feat: add auto-RTL for mixed-script content in auto mode - #96
Conversation
When auto mode is active and the setting is enabled, lines containing RTL scripts (Arabic, Hebrew, Persian, etc.) are treated as RTL regardless of their first directional character. Works in both editor (CodeMirror line decorations) and reading view (MarkdownPostProcessor). Includes a toggle in settings.
|
Hi Ali, First, we cannot call it "auto", as "auto RTL" is the common and universal name for the "BIDI algorithm" which is exactly the default behavior: decide based on the first letter with strong directionality. What you want is a different logic that I don't know how to call, perhaps something like "RTL-preferred auto". As it gives precedence to switching to RTL regardless of where the relevant characters are in the sentence. However, I find this logic very uncommon and very counter-intuitive. Do you really want sentences like "I wrote a message to my friend Ali (علي), who said RTL needs to be improved" to switch to RTL? Can you explain the need, and whether you know any software that behaves like that? |
|
Hi esm7,
I’ve been using your “RTL Support” plugin in Obsidian and ran into an
issue with mixed Farsi–English content.
In Farsi text, we sometimes need to mix English and Farsi words within
the same sentence to explain some concept—particularly when the first
word can't be translated because it's something like a Git command,
and the rest of the sentence explains it in Farsi. For example:
"""
دستورات مهم گیت:
git status: وضعیت مخزن را نشان میدهد.
git commit -m "توضیح": تغییرات را به مخزن میفرستد و یک Commit ایجاد میکند.
git checkout -b <نام شاخه>: یک شاخه جدید ایجاد میکند و به آن میرود.
"""
When I use the "RTL Support" plugin in Obsidian with the "Default text
direction" set to "Auto", these kinds of sentences consistently break
and become unreadable. The issue is that although the sentence should
be read right-to-left, the editor renders it left-to-right because the
first word is in English.
To address this, I submitted a pull request on GitHub proposing that
my changes be merged into the original code. My solution introduces
new logic to handle this scenario, and users can deliberately enable
it through the plugin's settings.
|
|
Hi Ali, Since RTL and BIDI support is a well-established doctrine, I don't want us to make up behaviors that no app ever uses. For example, how about we add a syntax to a block to mark it as RTL, e.g. decide that if the block opening includes |
Summary
Adds a new setting Auto-RTL for mixed LTR/RTL content (disabled by default) that, when enabled in auto mode, overrides the browser's first-strong-character heuristics for lines containing RTL scripts.
Details
Problem
When a line starts with English text but contains Arabic, Hebrew, or Persian later (e.g.
Hello سلام), the browser'sdir="auto"sees the first strong character (Latin) and renders the line LTR — which is incorrect.Solution
buildDecorations()to check each line for RTL characters when auto mode + setting enabled, applyingdir="rtl"when found.applyMixedContentRTL()that appliesdir="rtl"to block elements (p, li, h1-h6, td, th, blockquote) containing RTL scripts in reading view.RTL_CHAR_REGEXandhasRtlChar()utility.autoRTLForMixedContentwith UI toggle.Testing
Set default direction to Auto, enable the new setting, and type mixed LTR/RTL content in a note.