feat(render): support right-to-left (RTL) languages in the preview - #797
mostafasarhan wants to merge 1 commit into
Conversation
comrak (the Rust renderer) emits plain block elements with no dir attribute, so Arabic and Hebrew content renders left-to-right. Add dir="auto" to text-bearing block elements and switch .markdown-body to text-align:start so each block resolves its own direction. Code blocks (pre/code) deliberately stay LTR. Covers preview, HTML export and print/PDF through processMarkdownHtml. Relaxes one export test regex that pinned attribute order rather than the behaviour it asserts.
|
Thanks for this. Markpad has no RTL handling anywhere today, so this change is the base for RTL support as a whole. It is also what #192 asks for, so I'd like this PR to be the answer to that request. A few things before merge:
Two questions:
Once it's updated I'll test it on macOS as well. |
|
Following up with the full picture, since I'd like this PR to cover RTL in the preview and export completely. My earlier point 2 was only the tip of it. 1. Put 2. Left-anchored rules in
These should stay physical: the checkmark drawn inside the task checkbox (1795, 1800, flipping it mirrors the tick), the code block Two more places show document text: the footnote tooltip ( 3. Mixed-language cases to check.
For the first case, do you think a manual override is worth it, e.g. 4. Editor. Agreed that it stays as it is. Monaco has no RTL layout mode (microsoft/vscode#11770 has been open since 2016), so there's nothing reasonable to do there in this PR. |
What this is
Adds right-to-left (RTL) language support to rendered markdown, so content in RTL scripts displays correctly in the preview, the HTML export and print/PDF. No issue tracks this — it's a gap I hit opening an Arabic document.
Mechanism
comrak emits plain
<p>/<li>/<h1>blocks with nodirattribute, and.markdown-bodypinstext-align: left(insrc/styles.cssandMarkdownViewer.svelte). RTL content therefore renders left-to-right: paragraphs line up on the left, and mixed text (digits, URLs, a Latin word inside an RTL sentence) scrambles in the visual order.The fix is a single post-processing pass in
processMarkdownHtml— the one choke point the preview, export and print all go through — that setsdir="auto"on text-bearing block elements (p, h1–h6, li, blockquote, td, th, figcaption, dt, dd), plustext-align: startinstead oflefton.markdown-body.dir="auto"lets the browser resolve each block's direction from its own first strong directional character — the same rule GitHub renders markdown with.preand inlinecodeare deliberately excluded so source stays LTR.Scope
dirthe author already set is never overridden.text-align: leftare left as-is (UI chrome, not document content).Tests
No new behaviour test — the change is observable in the rendered output, and the existing suite already runs the real
processMarkdownHtmlend-to-end (foldKeys.test.ts,foldStatePerDocument.spec.ts,mathDelimiterContract.test.ts, …).One regex in
exportRichContent.test.tswas relaxed. It asserted<p data-math="display" data-math-source="E = mc\^2"><span class="katex">with the attributes in one exact order; the assertion's intent — the element contains the typeset formula, not its source — is unchanged, but the newdirattribute lands betweendata-math-sourceand the content. It now reads<p data-math="display" data-math-source="E = mc\^2"[^>]*><span class="katex">, matching the order-tolerant[^>]*>pattern already used on the line below it.Verification
Manual: rendered an Arabic sample (heading, mixed Arabic/English/digit paragraph, list, blockquote, table, code block) before and after — after the change paragraphs, lists and table cells align right and mixed text reads in the correct visual order, while code stays LTR.
Not verified locally:
cargo test(frontend-only change, Rust untouched — left to CI) and macOS/Linux rendering.