From 14a0df4bb55b80e9c8142f2feda7f463bea50bf3 Mon Sep 17 00:00:00 2001 From: NeatNit Date: Sun, 23 Mar 2025 21:38:47 +0200 Subject: [PATCH] make markdown bidirectional with dir="auto" --- lib/rich_text.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index 792497307..1d5ebab76 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -138,7 +138,25 @@ module RichText private def document - @document ||= Kramdown::Document.new(self) + return @document if @document + + @document = Kramdown::Document.new(self) + + should_get_dir_auto = lambda do |el| + dir_auto_types = [:p, :header, :codespan, :codeblock, :pre, :ul, :ol, :table, :dl, :math] + return true if dir_auto_types.include?(el.type) + return true if el.type == :a && el.children.length == 1 && el.children[0].type == :text && el.children[0].value == el.attr["href"] + + false + end + + add_dir = lambda do |element| + element.attr["dir"] ||= "auto" if should_get_dir_auto.call(element) + element.children.each(&add_dir) + end + add_dir.call(@document.root) + + @document end def first_image_element(element) -- 2.39.5