mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
Allow images in the daily digest for top scoring posts
This commit is contained in:
parent
0dc678c1e7
commit
6384518599
|
@ -12,7 +12,8 @@
|
||||||
<%- if t.best_post.present? %>
|
<%- if t.best_post.present? %>
|
||||||
<%= raw(t.best_post.excerpt(1000,
|
<%= raw(t.best_post.excerpt(1000,
|
||||||
strip_links: true,
|
strip_links: true,
|
||||||
text_entities: true)) %>
|
text_entities: true,
|
||||||
|
markdown_images: true)) %>
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
options || {}
|
options || {}
|
||||||
@strip_links = options[:strip_links] == true
|
@strip_links = options[:strip_links] == true
|
||||||
@text_entities = options[:text_entities] == true
|
@text_entities = options[:text_entities] == true
|
||||||
|
@markdown_images = options[:markdown_images] == true
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_excerpt(html, length, options)
|
def self.get_excerpt(html, length, options)
|
||||||
|
@ -21,9 +22,17 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
me.excerpt
|
me.excerpt
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def include_tag(name, attributes)
|
||||||
|
characters("<#{name} #{attributes.map{|k,v| "#{k}='#{v}'"}.join(' ')}>", false, false, false)
|
||||||
|
end
|
||||||
|
|
||||||
def start_element(name, attributes=[])
|
def start_element(name, attributes=[])
|
||||||
case name
|
case name
|
||||||
when "img"
|
when "img"
|
||||||
|
|
||||||
|
# If include_images is set, include the image in markdown
|
||||||
|
characters("!") if @markdown_images
|
||||||
|
|
||||||
attributes = Hash[*attributes.flatten]
|
attributes = Hash[*attributes.flatten]
|
||||||
if attributes["alt"]
|
if attributes["alt"]
|
||||||
characters("[#{attributes["alt"]}]")
|
characters("[#{attributes["alt"]}]")
|
||||||
|
@ -32,12 +41,12 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
else
|
else
|
||||||
characters("[image]")
|
characters("[image]")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
characters("(#{attributes['src']})") if @markdown_images
|
||||||
|
|
||||||
when "a"
|
when "a"
|
||||||
unless @strip_links
|
unless @strip_links
|
||||||
c = "<a "
|
include_tag(name, attributes)
|
||||||
c << attributes.map{|k,v| "#{k}='#{v}'"}.join(' ')
|
|
||||||
c << ">"
|
|
||||||
characters(c, false, false, false)
|
|
||||||
@in_a = true
|
@in_a = true
|
||||||
end
|
end
|
||||||
when "aside"
|
when "aside"
|
||||||
|
|
|
@ -105,14 +105,7 @@ test
|
||||||
|
|
||||||
describe "Excerpt" do
|
describe "Excerpt" do
|
||||||
|
|
||||||
it "should have an option to strip links" do
|
context "images" do
|
||||||
PrettyText.excerpt("<a href='http://cnn.com'>cnn</a>",100, strip_links: true).should == "cnn"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should preserve links" do
|
|
||||||
PrettyText.excerpt("<a href='http://cnn.com'>cnn</a>",100).should == "<a href='http://cnn.com'>cnn</a>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should dump images" do
|
it "should dump images" do
|
||||||
PrettyText.excerpt("<img src='http://cnn.com/a.gif'>",100).should == "[image]"
|
PrettyText.excerpt("<img src='http://cnn.com/a.gif'>",100).should == "[image]"
|
||||||
end
|
end
|
||||||
|
@ -125,6 +118,20 @@ test
|
||||||
PrettyText.excerpt("<img src='http://cnn.com/a.gif' title='car'>",100).should == "[car]"
|
PrettyText.excerpt("<img src='http://cnn.com/a.gif' title='car'>",100).should == "[car]"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should convert images to markdown if the option is set" do
|
||||||
|
PrettyText.excerpt("<img src='http://cnn.com/a.gif' title='car'>", 100, markdown_images: true).should == "![car](http://cnn.com/a.gif)"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have an option to strip links" do
|
||||||
|
PrettyText.excerpt("<a href='http://cnn.com'>cnn</a>",100, strip_links: true).should == "cnn"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should preserve links" do
|
||||||
|
PrettyText.excerpt("<a href='http://cnn.com'>cnn</a>",100).should == "<a href='http://cnn.com'>cnn</a>"
|
||||||
|
end
|
||||||
|
|
||||||
it "should deal with special keys properly" do
|
it "should deal with special keys properly" do
|
||||||
PrettyText.excerpt("<pre><b></pre>",100).should == ""
|
PrettyText.excerpt("<pre><b></pre>",100).should == ""
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user