From 25aea657e280204f1083a72279a4fb5bd5b521e6 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Fri, 2 Aug 2024 20:01:52 +0300 Subject: [PATCH] Detect images in markdown rich text --- lib/rich_text.rb | 3 ++- test/lib/rich_text_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index f19d3d3a9..de35aae9c 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -104,7 +104,8 @@ module RichText end def first_image_element(element) - return element if element.type == :img + return element if element.type == :img || + (element.type == :html_element && element.value == "img") element.children.find do |child| nested_image = first_image_element(child) diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index 8dc9e49b1..8c1205317 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -275,6 +275,11 @@ class RichTextTest < ActiveSupport::TestCase assert_equal "https://example.com/image1.jpg", r.image end + def test_markdown_html_image + r = RichText.new("markdown", "") + assert_equal "https://example.com/img_element.png", r.image + end + private def assert_html(richtext, &block) -- 2.39.5