99

Is there any existing addon spec for markdown that includes support for RTL languages?

What I'm hoping for is something like

This paragraph is left to right
<- This paragraph is right to left

Or something... I can tweak my parser to handle this but I want to make sure it doesn't exist already.

3
  • You can just write in Hebrew/Arabic and in the code, insert the markdown in a div and put style="direction: rtl" it'll make the text rtl, and the rest of the page ltr
    – Nirgn
    Jun 5, 2015 at 13:49
  • The syntax is: <div dir="rtl">
    – robi-y
    Oct 14, 2016 at 7:37
  • 3
    @Nir “insert the markdown in a div...” is not a solution within markdown. Feb 19, 2020 at 21:13

11 Answers 11

101

Actually as my friend Aevyz reminded me, Markdown parses HTML in it.

You won't need to change your parser. The quickest path to solve that I could think of is this:

<div dir="rtl">

سلام دنیا

مرحبا العالم

שלום בעולם

ہیلو دنیا
</div>

So you need to add literally two lines to turn a whole document or an arbitrary section of it into RTL. It will be more compatible than an own script.

3
  • Among online editors, this jbt.github.io/markdown-editor has adopted the HTML in...
    – Makan
    Aug 30, 2018 at 11:04
  • 6
    But unfortunately markdown attributes not working in HTML tags :(
    – ttrasn
    Jul 30, 2020 at 5:33
  • Can you elaborate? Which attributes? If you mean dir="rtl", this attribute is not a "markdown attribute". It belongs to HTML anyway.
    – Makan
    Jul 30, 2020 at 7:18
39

Not exactly markdown, but this is how you can override paragraph direction in StackExchange questions and answers (this method does not work for comments):

add &#x202b; (RIGHT-TO-LEFT EMBEDDING) in the beginning of a paragraph does control the direction of this paragraph (auto-reset on <br/> or empty line):

&#x202b;test מה זה? YES<br/>
test1 מה זה? NO
test2 מה זה? NO

&#x202b;test1 מה זה? YES
test2 מה זה? YES

‫test מה זה? YES
test1 מה זה? NO test2 מה זה? NO

‫test1 מה זה? YES test2 מה זה? YES

5
  • 2
    On Github issues using &#x202b; also provides correct results. A little trick is that I need to enclose the RTL paragraph in <p></p> or Github cannot render it. Nov 27, 2015 at 14:20
  • You forgot to close the RLE with PDF (&#x202c;). But these days, use neither. Use &#x2067; and close with &#x2069; the isolating versions of the former called RLI and PDI. Feb 20, 2020 at 13:48
  • @RobertSiemer I did not forget to close RLE; the StackExchange implementation resets on new line.
    – Alex Cohn
    Feb 22, 2020 at 8:57
  • @AlexCohn Would you recommend using it anyway? Feb 23, 2020 at 23:20
  • @RobertSiemer The question is what you want to achieve; luckily, it's easy to preview the result and change it if it's not to your liking. I believe that for some strings, the isolating BiDi control chars are better, but not averywhere.
    – Alex Cohn
    Feb 24, 2020 at 15:47
23

Here is a JavaScript implementation of Markdown, which (according to the commit comments) adds support for RTL languages, namely Arabic, Hebrew, Syriac, Thaana. And it seems trivially easy to add more languages.

https://github.com/hasenj/showdown/

It's based on Showdown, http://attacklab.net/showdown.

It seems to automatically understand whether or not to render the text from right to left.
Consider this code snippet: (from the very first commit at GitHub)

var p_tag = "<p>";
var rtl_p_tag = "<p style='direction:rtl; text-align: right'>";

// Check for RTL paragraphs: paragraphs that start with a character
// from an RTL script.
// RTL scripts are: Arabic, Hebrew, Syriac, Thaana
// Unicode ranges reference: http://www.ssec.wisc.edu/~tomw/java/unicode.html
var first_char = str.charCodeAt(str.search(/\S/)); //first non-white-space char
if(first_char >= 1424 && first_char <= 1983) 
{
    p_tag = rtl_p_tag;
}

    str = _RunSpanGamut(str);
    str = str.replace(/^([ \t]*)/g, p_tag);

Hope this helps,
Magnus

1
16

I don't find anything in markdown standard for bidi texts. I use my own editor : rtlmd

5
  • 4
    how should we use it with a github project? May 26, 2016 at 18:23
  • wow Proud! I have the same question as Ali. But i'm not optimistic that github's markdown reader will adopt this.
    – Makan
    Aug 30, 2018 at 8:46
  • 3
    In github, you just need add your content between: <div dir="rtl> </div> Jan 9, 2019 at 11:21
  • @DariushAbbasi Setting direction works for content but doesn't for titles.
    – Alireza
    Oct 6, 2019 at 9:48
  • @Alireza You should leave a blank line after opening div tag: <div dir="rtl">\n\n # RTL Title </div> Oct 6, 2021 at 12:57
14

Just use dir="auto" like the below example and it will work as you wanted:

 <div dir="auto">
                با استفاده از attribute dir با مقدار auto در HTML می تونید به طور خودکار RTL رو هم داشته باشید.
  </div>

12

You can use:

<div align="right">
   این متن test است
</div>

or:

<div dir="auto" align="right">
   این متن test است
</div>

or (recommended):

<div dir="auto">
   این متن test است
</div>
0
9

מעניין. עכשיו אני רואה שבעצם יש גם לאתר הזה פה תמיכה בעברית וכתיבה מימין לשמאל. הבעיה היא שזה כותב טוב, אבל בתרגום בתיבה למטה שמציגה כמו שזה אמור להראות זה לא עובד טוב.

The above paragraph was written in Hebrew RTL and was displayed correctly in the input box but not in the preview one. However, there was no support for mixing - having one paragraph RTL and another one LTR. Seems someone needs to port the above Hebrew support in Markdown also for MarkdownSharp, SO's version. Shouldn't be too hard.

1
  • 6
    Not markdown, but adding &#x202b; (RIGHT-TO-LEFT EMBEDDING) in the beginning of a paragraph does control the direction of this paragraph (auto-reset on <br/> or empty line).
    – Alex Cohn
    Jan 7, 2014 at 6:47
7

You can simply add everything between span tags like below:

<span dir="rtl" align="right">
      این یک test است
</span>

You may want to check this link to see an example in action.

2

Simple Solution

In Visual Studio Code, you can set the document direction by adding the following comment at the beginning of your Markdown file:

<!-- language: rtl -->

or

also in the beginning of your Markdown file:

if all the file is rtl

<style>
    * {
        direction: rtl;
    }
</style>

or any style you want

or

you can add separate custom css file like we are doing with Html file, like this in the beginning of your Markdown file :

<link rel="stylesheet" href="custom.css">
1
  • thanks, second option worked well for me.
    – andykandev
    Feb 15 at 11:29
0

Since the markdown accepts html, using this trick might be useful:

<style>*{direction: rtl}</style>

متن راست به چپ rtl text

## **rtl title عنوان راست به چپ**

I use this in VSCode Jupyter Notebooks, and the good thing is you can still use markdown tags (which is not possible when you wrap the content in <div dir="rtl"> or any other HTML tag).

-1

when I use align it looks better, but has no indents in bullets and numbering

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 7, 2023 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.