From 69aa8f18c2171dbfc2569847bb5e674daf62ded9 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Tue, 25 Dec 2018 17:02:28 +0200 Subject: [PATCH] FEATURE: allow for custom excerpt BBCODE This allows fidelity in controlling excerpt (text that shows up when you pin a topic or link to it externally): ``` I am some text [excerpt] This is some **custom** markdown that should be the excerpt [/excerpt] More text ``` Previous solution relied on DIVs, unfortunately DIVs do not play well, by design with mixing markdown unless you have a preceding newline eg: ```
this will be treated properly as markdown
``` This extra newline is not desirable. I am also considering adding ``` [div class=excerpt] [/div] ``` This would offer lots of flexibility to themes and plugins that do not want the extra annoying newline. --- .../engines/discourse-markdown/bbcode-block.js.es6 | 5 +++++ spec/components/pretty_text_spec.rb | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 index 4229ab34183..2270d8a612e 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 @@ -344,6 +344,11 @@ export function setup(helper) { helper.registerPlugin(md => { const ruler = md.block.bbcode.ruler; + ruler.push("excerpt", { + tag: "excerpt", + wrap: "div.excerpt" + }); + ruler.push("code", { tag: "code", replace: function(state, tagInfo, content) { diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 64ce1a3a402..1e0cfe9c73c 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -287,7 +287,7 @@ describe PrettyText do end it 'should not convert mentions to links' do - user = Fabricate(:user) + _user = Fabricate(:user) expect(PrettyText.cook('hi @user')).to eq('

hi @user

') end @@ -579,6 +579,17 @@ describe PrettyText do expect(PrettyText.excerpt(nil, 100)).to eq('') end + it "handles custom bbcode excerpt" do + raw = <<~RAW + [excerpt] + hello [site](https://site.com) + [/excerpt] + more stuff + RAW + post = Fabricate(:post, raw: raw) + expect(post.excerpt).to eq("hello site") + end + it "handles span excerpt at the beginning of a post" do expect(PrettyText.excerpt("hi test", 100)).to eq('hi') post = Fabricate(:post, raw: "hi test")