A platform for community discussion. Free, open, simple.
FEATURE: Add topic excerpt max length site setting (#9847)
Martin Brennandf68d11c388e
diff --git a/app/models/post.rb b/app/models/post.rbindex 89dbbcfebba..30ebaf01630 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -159,14 +159,6 @@ class Post < ActiveRecord::Baseincludes(:post_details).find_by(post_details: { key: key, value: value })end- def self.excerpt_size=(sz)- @excerpt_size = sz- end-- def self.excerpt_size- @excerpt_size || 220- end-def whisper?post_type == Post.types[:whisper]end@@ -482,7 +474,7 @@ class Post < ActiveRecord::Baseenddef 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)enddef is_first_post?diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 996fd4040b4..d44d4678ed4 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -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."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_desktop: "Show excerpt on pinned topics in desktop view."post_onebox_maxlength: "Maximum length of a oneboxed Discourse post in characters."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex ea4bbd4b665..88387952d73 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -819,6 +819,12 @@ posting:ja: 120zh_CN: 120zh_TW: 120+ topic_excerpt_maxlength:+ default: 220+ locale_default:+ ja: 120+ zh_CN: 120+ zh_TW: 120show_pinned_excerpt_mobile:client: truedefault: truediff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rbindex a455d81ec13..599ea3c5aa5 100644--- a/spec/components/search_spec.rb+++ b/spec/components/search_spec.rb@@ -444,7 +444,7 @@ describe Search doendlet(: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"endit 'returns the post' dodiff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rbindex 42235cb94fc..77a234d9814 100644--- a/spec/fabricators/post_fabricator.rb+++ b/spec/fabricators/post_fabricator.rb@@ -10,7 +10,7 @@ endFabricator(:post_with_long_raw_content, from: :post) doraw 'This is a sample post with semi-long raw content. The raw content is also more thantwo 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.'endFabricator(:post_with_youtube, from: :post) dodiff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rbindex 4cc716d6916..d0e2aba6319 100644--- a/spec/models/post_spec.rb+++ b/spec/models/post_spec.rb@@ -1158,6 +1158,25 @@ describe Post doexpect(post.custom_fields).to eq("Tommy" => "Hanks", "Vincent" => "Vega")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…"+ 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 …"+ post = Fabricate(:post_with_long_raw_content)+ post.rebake!+ excerpt = post.excerpt_for_topic+ expect(excerpt).to eq(expected_excerpt)+ end+ end+describe "#rebake!" doit "will rebake a post correctly" dopost = create_post