Skip to content

Add direction property to syntax tree and generated HTML? #694

Closed

Description

@forabi

Some languages like Arabic and Hebrew are written from right to left, and considering many apps and websites now depend on marked, I would like to add support for text direction at the syntax tree level so it benefits every project that depends on marked. Here's how I'd go about it:

  1. For elements like headings and paragraphs, perform a check to determine the direction of the text using the text property of the token. Here's how it is usually done:
var rtl = /[\u060C-\u06FE\uFB50-\uFEFC]/gi,
    threshold = 0.2; 

function isRTL(text) {
  var matches = text.match(rtl);
  var count = matches ? matches.length : 0;
  return count / text.length > threshold;
}

function dir(text) {
  return isRTL(text) ? 'rtl' : 'ltr';
}


console.log(dir('Potato')); // "ltr"
console.log(dir('بطاطا')); // "rtl"

To avoid performance regressions, we could make this optional.

  1. For tables and lists, check is performed for each cell and list item individually.
  2. For the generated HTML, simply add the attribute dir="rtl" for each element. Compliant browsers and apps should set the direction appropriately.

Are you interested in adding this feature? I could implement it.

Activity

  1. ali4ever4 commented on Jan 20, 2016

    @ali4ever4

    i was looking for such feature it really will be helpful

  2. KayvanMazaheri commented on Mar 28, 2016

    @KayvanMazaheri

    It would be very helpful to have RTL support in Marked. What do you think @chjj ?

  3. joshbruce commented on Apr 13, 2018

    @joshbruce
    Member

    Closing for now for future consideration.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions