FEATURE: Small improvements to the topic list embed (#12881)

FEATURE: Small improvements to the topic list embed (#12881)

  • FEATURE: Small improvements to the topic list embed
  • Ability to wrap the list in a custom class so you can styles different lists using specific CSS

  • Adds a topic link to the thumbnail when using the complete template

  • FIX: Be more strict about allowed chars in class name
diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rb
index db0fc72..2a063ad 100644
--- a/app/controllers/embed_controller.rb
+++ b/app/controllers/embed_controller.rb
@@ -34,6 +34,10 @@ class EmbedController < ApplicationController
       raise Discourse::InvalidParameters.new(:embed_id) unless @embed_id =~ /^de\-[a-zA-Z0-9]+$/
     end
 
+    if @embed_class = params[:embed_class]
+      raise Discourse::InvalidParameters.new(:embed_class) unless @embed_class =~ /^[a-zA-Z0-9\-_]+$/
+    end
+
     if params.has_key?(:template) && params[:template] == "complete"
       @template = "complete"
     else
diff --git a/app/views/embed/topics.html.erb b/app/views/embed/topics.html.erb
index 10b825f..f7444fa 100644
--- a/app/views/embed/topics.html.erb
+++ b/app/views/embed/topics.html.erb
@@ -7,7 +7,7 @@
       </button>
     <%- end %>
   <%- end %>
-  <div class='topics-list' data-embed-state='loaded' <%- if @embed_id %>data-embed-id="<%= @embed_id %>"<%- end %>>
+  <div class='topics-list <%= @embed_class %>' data-embed-state='loaded' <%- if @embed_id %>data-embed-id="<%= @embed_id %>"<%- end %>>
     <%- @list.topics.each do |t| %>
       <div class='topic-list-item'>
         <%- if @template == "complete" %>
@@ -49,7 +49,9 @@
               <div class='topic-column featured-image-column'>
                 <%- if t.image_url.present? %>
                   <div class='topic-featured-image'>
-                    <img src="<%= t.image_url %>">
+                    <a target="_parent" href="<%= t.url %>" class="title raw-link raw-topic-link" data-topic-id="<%= t.id %>">
+                      <img src="<%= t.image_url %>">
+                    </a>
                   </div>
                 <%- end %>
               </div>
diff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rb
index 965a78f..ff89614 100644
--- a/spec/requests/embed_controller_spec.rb
+++ b/spec/requests/embed_controller_spec.rb
@@ -100,7 +100,7 @@ describe EmbedController do
         expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")
       end
 
-      it "returns a list of topics" do
+      it "returns a list of top topics" do
         bad_topic = Fabricate(:topic)
         good_topic = Fabricate(:topic, like_count: 1000, posts_count: 100)
         TopTopic.refresh!
@@ -116,6 +116,16 @@ describe EmbedController do
         expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")
       end
 
+      it "wraps the list in a custom class" do
+        topic = Fabricate(:topic)
+        get '/embed/topics?discourse_embed_id=de-1234&embed_class=my-special-class', headers: {
+          'REFERER' => 'https://example.com/evil-trout'
+        }
+        expect(response.status).to eq(200)
+        expect(response.headers['X-Frame-Options']).to be_nil
+        expect(response.body).to match("class='topics-list my-special-class'")
+      end
+
       it "returns no referer if not supplied" do
         get '/embed/topics?discourse_embed_id=de-1234'
         expect(response.status).to eq(200)

GitHub sha: e2154b3d

This commit appears in #12881 which was approved by eviltrout. It was merged by Falco.