FIX: Replace spoilers with URLs to post. (#35)

FIX: Replace spoilers with URLs to post. (#35)

diff --git a/plugin.rb b/plugin.rb
index 0c8e8a0..db2f124 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -13,18 +13,15 @@ register_asset "stylesheets/discourse_spoiler_alert.css"
 
 after_initialize do
 
-  # black out spoilers in emails
-  Email::Styles.register_plugin_style do |fragment|
-    fragment.css(".spoiler").each do |spoiler|
-      spoiler["style"] = "color: #000; background-color: #000;"
+  on(:reduce_cooked) do |fragment, post|
+    fragment.css(".spoiler").each do |el|
+      link = fragment.document.create_element("a")
+      link["href"] = post.url
+      link.content = I18n.t("spoiler_alert.excerpt_spoiler")
+      el.inner_html = link.to_html
     end
   end
 
-  # remove spoilers in embedded comments
-  on(:reduce_cooked) do |fragment|
-    fragment.css(".spoiler").remove
-  end
-
   on(:pre_notification_alert) do |user, payload|
     payload[:excerpt] = payload[:excerpt].gsub(/\<span class=\"spoiler\"\>.*\<\/span\>/, "[#{I18n.t 'spoiler_alert.excerpt_spoiler'}]")
   end
diff --git a/spec/pretty_text_spec.rb b/spec/pretty_text_spec.rb
index 8b4c297..37babd4 100644
--- a/spec/pretty_text_spec.rb
+++ b/spec/pretty_text_spec.rb
@@ -4,6 +4,8 @@ require 'rails_helper'
 
 describe PrettyText do
 
+  let(:post) { Fabricate(:post) }
+
   def n(html)
     html.strip
   end
@@ -21,4 +23,12 @@ describe PrettyText do
 
     expect(md).to eq(html)
   end
+
+  it 'can replace spoilers in emails' do
+    md = PrettyText.cook('I like watching [spoiler]my tests fail[/spoiler]')
+    md = PrettyText.format_for_email(md, post)
+    html = "<p>I like watching <span class=\"spoiler\"><a href=\"#{post.full_url}\">spoiler</a></span></p>"
+
+    expect(md).to eq(html)
+  end
 end

GitHub sha: 0dad3634

2 Likes