A platform for community discussion. Free, open, simple.
DEV: Add keep_images option to ExcerptParser (#36077)
Martin Brennan21f682435a61
diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rbindex 9316d529b40..347a0b15d0f 100644--- a/lib/excerpt_parser.rb+++ b/lib/excerpt_parser.rb@@ -4,7 +4,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Documentattr_reader :excerptCUSTOM_EXCERPT_REGEX = /<\s*(span|div)[^>]*class\s*=\s*['"]excerpt['"][^>]*>/- IMAGE_MODES = [%i[strip_images strip], %i[markdown_images markdown]].freeze+ IMAGE_MODES = [%i[strip_images strip], %i[markdown_images markdown], %i[keep_images keep]].freezedef initialize(length, options = nil)@length = length@@ -30,7 +30,7 @@ class ExcerptParser < Nokogiri::XML::SAX::DocumentIMAGE_MODES.each { |key, mode| return mode if options[key] }end- def self.get_excerpt(html, length, options)+ def self.get_excerpt(html, length, options = {})return "" if html.blank?length = html.length if html.include?("excerpt") && CUSTOM_EXCERPT_REGEX === html@@ -81,8 +81,9 @@ class ExcerptParser < Nokogiri::XML::SAX::Documentendend- unless @image_mode == :strip- # If include_images is set, include the image in markdown+ return include_tag(name, attributes) if @image_mode == :keep++ if @image_mode != :stripcharacters("!") if @image_mode == :markdownif attributes["alt"].present?@@ -90,7 +91,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Documentelsif attributes["title"].present?characters("[#{attributes["title"]}]")else- characters("[#{I18n.t "excerpt_image"}]")+ characters("[#{I18n.t("excerpt_image")}]")endcharacters("(#{attributes["src"]})") if @image_mode == :markdowndiff --git a/spec/lib/excerpt_parser_spec.rb b/spec/lib/excerpt_parser_spec.rbindex 3649833ce31..8ed511cba26 100644--- a/spec/lib/excerpt_parser_spec.rb+++ b/spec/lib/excerpt_parser_spec.rb@@ -152,4 +152,131 @@ RSpec.describe ExcerptParser do)endend++ describe "image handling options" do+ describe "default behavior (no image option specified)" do+ it "replaces images with alt text in brackets" do+ html = '<p>Check out <img src="/uploads/image.jpg" alt="sunset"></p>'+ expect(ExcerptParser.get_excerpt(html, 100)).to eq("Check out [sunset]")+ end++ it "uses title text when alt is not present" do+ html = '<p><img src="/uploads/image.jpg" title="My Image"></p>'+ expect(ExcerptParser.get_excerpt(html, 100)).to eq("[My Image]")+ end++ it "uses default image text when neither alt nor title is present" do+ html = '<p><img src="/uploads/image.jpg"></p>'+ expect(ExcerptParser.get_excerpt(html, 100)).to eq("[image]")+ end++ it "handles multiple images" do+ html =+ '<p><img src="/uploads/1.jpg" alt="first"> and <img src="/uploads/2.jpg" alt="second"></p>'+ expect(ExcerptParser.get_excerpt(html, 100)).to eq("[first] and [second]")+ end++ it "does not include the URL" do+ html = '<p><img src="/uploads/image.jpg" alt="photo"></p>'+ result = ExcerptParser.get_excerpt(html, 100)+ expect(result).to eq("[photo]")+ expect(result).not_to include("/uploads/image.jpg")+ end+ end++ describe "strip_images option" do+ it "completely removes images with no replacement text" do+ html = '<p>Check out this photo: <img src="/uploads/image.jpg" alt="sunset"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, strip_images: true)).to eq(+ "Check out this photo:",+ )+ end++ it "removes images regardless of alt or title attributes" do+ html = '<p><img src="/uploads/image.jpg" title="My Image"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, strip_images: true)).to eq("")+ end++ it "removes images with no attributes" do+ html = '<p><img src="/uploads/image.jpg"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, strip_images: true)).to eq("")+ end++ it "removes multiple images leaving only text" do+ html =+ '<p><img src="/uploads/1.jpg" alt="first"> and <img src="/uploads/2.jpg" alt="second"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, strip_images: true)).to eq("and")+ end++ it "still handles emoji images with keep_emoji_images" do+ html =+ '<p>Hello <img src="/images/emoji/emoji_one/smile.png" class="emoji" alt=":smile:"></p>'+ expect(+ ExcerptParser.get_excerpt(html, 100, strip_images: true, keep_emoji_images: true),+ ).to match(/<img.*class="emoji"/)+ end+ end++ describe "markdown_images option" do+ it "converts images to markdown format with alt text" do+ html = '<p>Check out <img src="/uploads/image.jpg" alt="sunset"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, markdown_images: true)).to eq(+ "Check out ",+ )+ end++ it "uses title text when alt is not present" do+ html = '<p><img src="/uploads/image.jpg" title="My Image"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, markdown_images: true)).to eq(+ "",+ )+ end++ it "uses default image text when neither alt nor title is present" do+ html = '<p><img src="/uploads/image.jpg"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, markdown_images: true)).to eq(+ "",+ )+ end++ it "handles multiple images" do+ html =+ '<p><img src="/uploads/1.jpg" alt="first"> and <img src="/uploads/2.jpg" alt="second"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, markdown_images: true)).to eq(+ " and ",+ )+ end++ it "handles images with complex URLs" do+ html = '<p><img src="https://example.com/path/to/image.jpg?size=large" alt="external"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, markdown_images: true)).to eq(+ "",+ )+ end+ end++ describe "keep_images option" do+ it "preserves the full img tag" do+ html = '<p>Check out <img src="/uploads/image.jpg" alt="sunset" class="photo"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, keep_images: true)).to eq(+ 'Check out <img src="/uploads/image.jpg" alt="sunset" class="photo">',+ )+ end++ it "preserves multiple attributes" do+ html =+ '<p><img src="/uploads/image.jpg" alt="sunset" title="Beautiful" width="100" height="100"></p>'+ expect(ExcerptParser.get_excerpt(html, 100, keep_images: true)).to eq(+ '<img src="/uploads/image.jpg" alt="sunset" title="Beautiful" width="100" height="100">',+ )+ end++ it "preserves multiple images" do+ html = '<p><img src="/1.jpg" alt="a"> <img src="/2.jpg" alt="b"></p>'+ result = ExcerptParser.get_excerpt(html, 100, keep_images: true)+ expect(result).to include('<img src="/1.jpg" alt="a">')+ expect(result).to include('<img src="/2.jpg" alt="b">')+ end+ end+ endend