FEATURE: Add topic excerpt max length site setting (#9847)

Adds a new topic_excerpt_maxlength site setting.

* When topic excerpt is requested for a post, use the new topic_excerpt_maxlength site setting to limit the size of the excerpt
* Remove code for getting/setting Post.excerpt_size as it is not used anywhere
This commit is contained in:
Martin Brennan 2020-05-21 13:19:48 +10:00 committed by GitHub
parent 68db5deaec
commit df68d11c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 11 deletions

View File

@ -159,14 +159,6 @@ class Post < ActiveRecord::Base
includes(:post_details).find_by(post_details: { key: key, value: value }) includes(:post_details).find_by(post_details: { key: key, value: value })
end end
def self.excerpt_size=(sz)
@excerpt_size = sz
end
def self.excerpt_size
@excerpt_size || 220
end
def whisper? def whisper?
post_type == Post.types[:whisper] post_type == Post.types[:whisper]
end end
@ -482,7 +474,7 @@ class Post < ActiveRecord::Base
end end
def excerpt_for_topic def excerpt_for_topic
Post.excerpt(cooked, Post.excerpt_size, strip_links: true, strip_images: true, post: self) Post.excerpt(cooked, SiteSetting.topic_excerpt_maxlength, strip_links: true, strip_images: true, post: self)
end end
def is_first_post? def is_first_post?

View File

@ -1467,6 +1467,7 @@ en:
exclude_rel_nofollow_domains: "A list of domains where nofollow should not be added to links. example.com will automatically allow sub.example.com as well. As a minimum, you should add the domain of this site to help web crawlers find all content. If other parts of your website are at other domains, add those too." exclude_rel_nofollow_domains: "A list of domains where nofollow should not be added to links. example.com will automatically allow sub.example.com as well. As a minimum, you should add the domain of this site to help web crawlers find all content. If other parts of your website are at other domains, add those too."
post_excerpt_maxlength: "Maximum length of a post excerpt / summary." post_excerpt_maxlength: "Maximum length of a post excerpt / summary."
topic_excerpt_maxlength: "Maximum length of a topic excerpt / summary, generated from the first post in a topic."
show_pinned_excerpt_mobile: "Show excerpt on pinned topics in mobile view." show_pinned_excerpt_mobile: "Show excerpt on pinned topics in mobile view."
show_pinned_excerpt_desktop: "Show excerpt on pinned topics in desktop view." show_pinned_excerpt_desktop: "Show excerpt on pinned topics in desktop view."
post_onebox_maxlength: "Maximum length of a oneboxed Discourse post in characters." post_onebox_maxlength: "Maximum length of a oneboxed Discourse post in characters."

View File

@ -819,6 +819,12 @@ posting:
ja: 120 ja: 120
zh_CN: 120 zh_CN: 120
zh_TW: 120 zh_TW: 120
topic_excerpt_maxlength:
default: 220
locale_default:
ja: 120
zh_CN: 120
zh_TW: 120
show_pinned_excerpt_mobile: show_pinned_excerpt_mobile:
client: true client: true
default: true default: true

View File

@ -444,7 +444,7 @@ describe Search do
end end
let(:expected_blurb) do let(:expected_blurb) do
"...to satisfy any test conditions that require content longer than the typical test post raw content. elephant" "...quire content longer than the typical test post raw content. It really is some long content, folks. elephant"
end end
it 'returns the post' do it 'returns the post' do

View File

@ -10,7 +10,7 @@ end
Fabricator(:post_with_long_raw_content, from: :post) do Fabricator(:post_with_long_raw_content, from: :post) do
raw 'This is a sample post with semi-long raw content. The raw content is also more than raw 'This is a sample post with semi-long raw content. The raw content is also more than
two hundred characters to satisfy any test conditions that require content longer two hundred characters to satisfy any test conditions that require content longer
than the typical test post raw content.' than the typical test post raw content. It really is some long content, folks.'
end end
Fabricator(:post_with_youtube, from: :post) do Fabricator(:post_with_youtube, from: :post) do

View File

@ -1158,6 +1158,25 @@ describe Post do
expect(post.custom_fields).to eq("Tommy" => "Hanks", "Vincent" => "Vega") expect(post.custom_fields).to eq("Tommy" => "Hanks", "Vincent" => "Vega")
end end
describe "#excerpt_for_topic" do
it "returns a topic excerpt, defaulting to 220 chars" do
expected_excerpt = "This is a sample post with semi-long raw content. The raw content is also more than \ntwo hundred characters to satisfy any test conditions that require content longer \nthan the typical test post raw content. It really is&hellip;"
post = Fabricate(:post_with_long_raw_content)
post.rebake!
excerpt = post.excerpt_for_topic
expect(excerpt).to eq(expected_excerpt)
end
it "respects the site setting for topic excerpt" do
SiteSetting.topic_excerpt_maxlength = 10
expected_excerpt = "This is a &hellip;"
post = Fabricate(:post_with_long_raw_content)
post.rebake!
excerpt = post.excerpt_for_topic
expect(excerpt).to eq(expected_excerpt)
end
end
describe "#rebake!" do describe "#rebake!" do
it "will rebake a post correctly" do it "will rebake a post correctly" do
post = create_post post = create_post