mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 21:52:43 +08:00
option to strip links from excerpts
This commit is contained in:
@ -221,14 +221,14 @@ class Post < ActiveRecord::Base
|
|||||||
user_id: user_id).first.try(:user)
|
user_id: user_id).first.try(:user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.excerpt(cooked, maxlength = nil)
|
def self.excerpt(cooked, maxlength = nil, options = {})
|
||||||
maxlength ||= SiteSetting.post_excerpt_maxlength
|
maxlength ||= SiteSetting.post_excerpt_maxlength
|
||||||
PrettyText.excerpt(cooked, maxlength)
|
PrettyText.excerpt(cooked, maxlength, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Strip out most of the markup
|
# Strip out most of the markup
|
||||||
def excerpt(maxlength = nil)
|
def excerpt(maxlength = nil, options = {})
|
||||||
Post.excerpt(cooked, maxlength)
|
Post.excerpt(cooked, maxlength, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# What we use to cook posts
|
# What we use to cook posts
|
||||||
|
@ -60,7 +60,7 @@ class TopicListItemSerializer < ListableTopicSerializer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def excerpt
|
def excerpt
|
||||||
object.posts.first.try(:excerpt,200) || nil
|
object.posts.first.try(:excerpt,220, strip_links: true) || nil
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -56,7 +56,7 @@ class Autospec::Runner
|
|||||||
Signal.trap("SIGINT") {stop_spork; exit }
|
Signal.trap("SIGINT") {stop_spork; exit }
|
||||||
|
|
||||||
puts "Forced polling (slower) - inotify does not work on network filesystems, use local filesystem to avoid" if force_polling
|
puts "Forced polling (slower) - inotify does not work on network filesystems, use local filesystem to avoid" if force_polling
|
||||||
|
|
||||||
Thread.start do
|
Thread.start do
|
||||||
Listen.to('.', force_polling: force_polling, filter: /^app|^spec|^lib/, relative_paths: true) do |modified, added, removed|
|
Listen.to('.', force_polling: force_polling, filter: /^app|^spec|^lib/, relative_paths: true) do |modified, added, removed|
|
||||||
process_change([modified, added].flatten.compact)
|
process_change([modified, added].flatten.compact)
|
||||||
|
@ -239,14 +239,16 @@ module PrettyText
|
|||||||
|
|
||||||
attr_reader :excerpt
|
attr_reader :excerpt
|
||||||
|
|
||||||
def initialize(length)
|
def initialize(length,options)
|
||||||
@length = length
|
@length = length
|
||||||
@excerpt = ""
|
@excerpt = ""
|
||||||
@current_length = 0
|
@current_length = 0
|
||||||
|
@strip_links = options[:strip_links] == true
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_excerpt(html, length)
|
def self.get_excerpt(html, length, options)
|
||||||
me = self.new(length)
|
|
||||||
|
me = self.new(length,options)
|
||||||
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
||||||
begin
|
begin
|
||||||
copy = "<div>"
|
copy = "<div>"
|
||||||
@ -271,11 +273,13 @@ module PrettyText
|
|||||||
characters("[image]")
|
characters("[image]")
|
||||||
end
|
end
|
||||||
when "a"
|
when "a"
|
||||||
c = "<a "
|
unless @strip_links
|
||||||
c << attributes.map{|k,v| "#{k}='#{v}'"}.join(' ')
|
c = "<a "
|
||||||
c << ">"
|
c << attributes.map{|k,v| "#{k}='#{v}'"}.join(' ')
|
||||||
characters(c, false, false, false)
|
c << ">"
|
||||||
@in_a = true
|
characters(c, false, false, false)
|
||||||
|
@in_a = true
|
||||||
|
end
|
||||||
when "aside"
|
when "aside"
|
||||||
@in_quote = true
|
@in_quote = true
|
||||||
end
|
end
|
||||||
@ -284,8 +288,10 @@ module PrettyText
|
|||||||
def end_element(name)
|
def end_element(name)
|
||||||
case name
|
case name
|
||||||
when "a"
|
when "a"
|
||||||
characters("</a>",false, false, false)
|
unless @strip_links
|
||||||
@in_a = false
|
characters("</a>",false, false, false)
|
||||||
|
@in_a = false
|
||||||
|
end
|
||||||
when "p", "br"
|
when "p", "br"
|
||||||
characters(" ")
|
characters(" ")
|
||||||
when "aside"
|
when "aside"
|
||||||
@ -307,8 +313,8 @@ module PrettyText
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.excerpt(html, length)
|
def self.excerpt(html, max_length, options={})
|
||||||
ExcerptParser.get_excerpt(html, length)
|
ExcerptParser.get_excerpt(html, max_length, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -104,6 +104,11 @@ test
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "Excerpt" do
|
describe "Excerpt" do
|
||||||
|
|
||||||
|
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
|
it "should preserve links" do
|
||||||
PrettyText.excerpt("<a href='http://cnn.com'>cnn</a>",100).should == "<a href='http://cnn.com'>cnn</a>"
|
PrettyText.excerpt("<a href='http://cnn.com'>cnn</a>",100).should == "<a href='http://cnn.com'>cnn</a>"
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user