diff --git a/app/views/user_notifications/digest.text.erb b/app/views/user_notifications/digest.text.erb index d9d640453fa..caf580b02d9 100644 --- a/app/views/user_notifications/digest.text.erb +++ b/app/views/user_notifications/digest.text.erb @@ -12,7 +12,8 @@ <%- if t.best_post.present? %> <%= raw(t.best_post.excerpt(1000, strip_links: true, - text_entities: true)) %> + text_entities: true, + markdown_images: true)) %> -------------------------------------------------------------------------------- diff --git a/lib/excerpt_parser.rb b/lib/excerpt_parser.rb index 167a54025a3..112b3ec20c1 100644 --- a/lib/excerpt_parser.rb +++ b/lib/excerpt_parser.rb @@ -9,6 +9,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document options || {} @strip_links = options[:strip_links] == true @text_entities = options[:text_entities] == true + @markdown_images = options[:markdown_images] == true end def self.get_excerpt(html, length, options) @@ -21,9 +22,17 @@ class ExcerptParser < Nokogiri::XML::SAX::Document me.excerpt end + def include_tag(name, attributes) + characters("<#{name} #{attributes.map{|k,v| "#{k}='#{v}'"}.join(' ')}>", false, false, false) + end + def start_element(name, attributes=[]) case name when "img" + + # If include_images is set, include the image in markdown + characters("!") if @markdown_images + attributes = Hash[*attributes.flatten] if attributes["alt"] characters("[#{attributes["alt"]}]") @@ -32,12 +41,12 @@ class ExcerptParser < Nokogiri::XML::SAX::Document else characters("[image]") end + + characters("(#{attributes['src']})") if @markdown_images + when "a" unless @strip_links - c = "" - characters(c, false, false, false) + include_tag(name, attributes) @in_a = true end when "aside" diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 0d4447e0ec5..a9686ad9e55 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -105,6 +105,25 @@ test describe "Excerpt" do + context "images" do + it "should dump images" do + PrettyText.excerpt("",100).should == "[image]" + end + + it "should keep alt tags" do + PrettyText.excerpt("car",100).should == "[car]" + end + + it "should keep title tags" do + PrettyText.excerpt("",100).should == "[car]" + end + + it "should convert images to markdown if the option is set" do + PrettyText.excerpt("", 100, markdown_images: true).should == "![car](http://cnn.com/a.gif)" + end + + end + it "should have an option to strip links" do PrettyText.excerpt("cnn",100, strip_links: true).should == "cnn" end @@ -113,18 +132,6 @@ test PrettyText.excerpt("cnn",100).should == "cnn" end - it "should dump images" do - PrettyText.excerpt("",100).should == "[image]" - end - - it "should keep alt tags" do - PrettyText.excerpt("car",100).should == "[car]" - end - - it "should keep title tags" do - PrettyText.excerpt("",100).should == "[car]" - end - it "should deal with special keys properly" do PrettyText.excerpt("
",100).should == "" end