Skip to content

Fix reading view direction for right-to-left articles - #909

Open
a-y-ibrahim wants to merge 1 commit into
obsidianmd:mainfrom
a-y-ibrahim:fix/reader-rtl-direction
Open

Fix reading view direction for right-to-left articles#909
a-y-ibrahim wants to merge 1 commit into
obsidianmd:mainfrom
a-y-ibrahim:fix/reader-rtl-direction

Conversation

@a-y-ibrahim

Copy link
Copy Markdown

Summary

The reading view stayed left-aligned for right-to-left articles (Arabic, Persian, Hebrew, and others), which makes them hard to read. This derives the reader's text direction from the article so RTL pages mirror correctly. Closes #864.

Root cause

Reader.apply() only re-applied a dir that the source <html> already carried. Many RTL pages declare lang but omit an explicit dir, so the reader kept the default LTR direction. The standalone reader.html and in-reader navigation paths never carried the source direction at all, since there <html> belongs to reader.html (lang="en"), not to the article.

Changes

Defuddle already reports the article language (result.language). This threads it through ReaderContent and centralizes direction in one helper, applyReaderDirection, shared by all three reader paths (live page, standalone reader page, in-reader navigation). Resolution order: honor an explicit source dir if present, otherwise derive rtl from the language via the existing isRTLLanguage() helper, otherwise clear any stale direction.

Design notes

  • Direction is derived from the article language, not dir="auto". auto infers base direction from the first strong character, so an Arabic paragraph starting with a Latin word (for example "Google هي شركة ...") would flip to LTR. Deriving from the language avoids that. Inline mixed runs are still handled by the browser bidi algorithm.
  • No CSS changes: the reader styles already use logical properties (padding-inline-*, text-align: start), so setting dir mirrors alignment.
  • An author-declared dir is always respected and never overridden.

Tests

Added i18n.test.ts (isRTLLanguage) and reader.test.ts (applyReaderDirection: RTL languages and region subtags, LTR untouched, explicit dir precedence, stale reset on navigation).

Known limitation

A page declaring neither a language nor a dir, and whose language cannot be detected, still renders LTR (no signal to infer direction). Unchanged from current behavior and out of scope here.

The reader kept the default LTR direction for RTL articles (Arabic,
Persian, Hebrew, and others) that declare a language but no explicit
dir, and the standalone reader page and in-reader navigation never
carried the source direction at all.

Thread the article language (from Defuddle) through ReaderContent and
resolve direction in one shared helper, applyReaderDirection, used by
the live page, the standalone reader page, and in-reader navigation:
honor an explicit source dir, else derive rtl from the language via the
existing isRTLLanguage helper, else clear it. No CSS changes.

Add tests for isRTLLanguage and applyReaderDirection.

Closes obsidianmd#864
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Reading view doesn't align RTL pages properly

1 participant