FEATURE: allow for custom excerpt BBCODE

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:

<div class='hello'>

this will be treated properly as markdown

</div>

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.

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 4229ab3..2270d8a 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 64ce1a3..1e0cfe9 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('<p>hi @user</p>')
       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 <a href=\"https://site.com\" rel=\"nofollow noopener\">site</a>")
+    end
+
     it "handles span excerpt at the beginning of a post" do
       expect(PrettyText.excerpt("<span class='excerpt'>hi</span> test", 100)).to eq('hi')
       post = Fabricate(:post, raw: "<span class='excerpt'>hi</span> test")

GitHub
sha: 69aa8f18

1 Like

FIX: correct excerpts not parsing markdown correctly