make markdown bidirectional with dir="auto"

This commit is contained in:
NeatNit
2025-03-23 21:38:47 +02:00
parent 59e6081aed
commit 14a0df4bb5

View File

@ -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)