diff --git a/plugins/discourse-details/plugin.rb b/plugins/discourse-details/plugin.rb index 2668042db2a..eba95c33a30 100644 --- a/plugins/discourse-details/plugin.rb +++ b/plugins/discourse-details/plugin.rb @@ -37,7 +37,7 @@ after_initialize do .each do |el| text = el.css("summary").text link = fragment.document.create_element("a") - link["href"] = post.url if post + link["href"] = post&.url.presence || Discourse.base_url link.content = I18n.t("details.excerpt_details") el.replace CGI.escapeHTML(text) + " " + link.to_html end diff --git a/plugins/discourse-details/spec/components/pretty_text_spec.rb b/plugins/discourse-details/spec/components/pretty_text_spec.rb index 3356f3a192a..cf1e87e6011 100644 --- a/plugins/discourse-details/spec/components/pretty_text_spec.rb +++ b/plugins/discourse-details/spec/components/pretty_text_spec.rb @@ -6,43 +6,54 @@ RSpec.describe PrettyText do let(:post) { Fabricate(:post) } it "supports details tag" do - cooked_html = <<~HTML.gsub("\n", "") + cooked_html = PrettyText.cook <<~MARKDOWN + [details="foo"] + bar + [/details] + MARKDOWN + + expect(cooked_html).to match_html <<~HTML
- - foo -

bar

+ foo +

bar

HTML - - expect(cooked_html).to match_html(cooked_html) - expect(PrettyText.cook("[details=foo]\nbar\n[/details]").gsub("\n", "")).to match_html( - cooked_html, - ) end it "deletes elided content" do - cooked_html = PrettyText.cook("Hello World\n\n
42
") - mail_html = "

Hello World

\n(click for more details)" + cooked_html = PrettyText.cook <<~MARKDOWN + Hello World + +
42
+ MARKDOWN - expect(PrettyText.format_for_email(cooked_html)).to match_html(mail_html) + email_html = PrettyText.format_for_email(cooked_html) + + expect(email_html).to match_html <<~HTML +

Hello World

+ #{I18n.t("details.excerpt_details")} + HTML end it "can replace spoilers in emails" do - md = PrettyText.cook(<<~MD) + cooked_html = PrettyText.cook <<~MARKDOWN hello [details="Summary"] world [/details] - MD - md = PrettyText.format_for_email(md, post) - html = "

hello

\n\nSummary (click for more details)" + MARKDOWN - expect(md).to eq(html) + email_html = PrettyText.format_for_email(cooked_html, post) + + expect(email_html).to match_html <<~HTML +

hello

+ Summary #{I18n.t("details.excerpt_details")} + HTML end it "properly handles multiple spoiler blocks in a post" do - md = PrettyText.cook(<<~MD) + cooked_html = PrettyText.cook <<~MARKDOWN [details="First"] body secret stuff very long [/details] @@ -55,24 +66,31 @@ RSpec.describe PrettyText do [details="Third"] body secret stuff very long [/details] - MD + MARKDOWN - md = PrettyText.format_for_email(md, post) - expect(md).not_to include("secret stuff") - expect(md.scan(/First/).size).to eq(1) - expect(md.scan(/Third/).size).to eq(1) - expect(md.scan(I18n.t("details.excerpt_details")).size).to eq(3) + email_html = PrettyText.format_for_email(cooked_html, post) + + expect(email_html).to match_html <<~HTML + First #{I18n.t("details.excerpt_details")} + Second #{I18n.t("details.excerpt_details")} +

Hey there.

+ Third #{I18n.t("details.excerpt_details")} + HTML end it "escapes summary text" do - md = PrettyText.cook(<<~MD) + cooked_html = PrettyText.cook <<~MARKDOWN + [details=""] [/details] - MD - md = PrettyText.format_for_email(md, post) + MARKDOWN - expect(md).not_to include("