mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 04:11:12 +08:00
DEV: Apply syntax_tree formatting to plugins/*
This commit is contained in:
@ -12,32 +12,34 @@ hide_plugin if self.respond_to?(:hide_plugin)
|
||||
register_asset "stylesheets/details.scss"
|
||||
|
||||
after_initialize do
|
||||
|
||||
Email::Styles.register_plugin_style do |fragment|
|
||||
# remove all elided content
|
||||
fragment.css("details.elided").each(&:remove)
|
||||
|
||||
# replace all details with their summary in emails
|
||||
fragment.css("details").each do |details|
|
||||
summary = details.css("summary")
|
||||
if summary && summary[0]
|
||||
summary = summary[0]
|
||||
if summary && summary.respond_to?(:name)
|
||||
summary.name = "p"
|
||||
details.replace(summary)
|
||||
fragment
|
||||
.css("details")
|
||||
.each do |details|
|
||||
summary = details.css("summary")
|
||||
if summary && summary[0]
|
||||
summary = summary[0]
|
||||
if summary && summary.respond_to?(:name)
|
||||
summary.name = "p"
|
||||
details.replace(summary)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
on(:reduce_cooked) do |fragment, post|
|
||||
fragment.css("details").each do |el|
|
||||
text = el.css("summary").text
|
||||
link = fragment.document.create_element("a")
|
||||
link["href"] = post.url if post
|
||||
link.content = I18n.t("details.excerpt_details")
|
||||
el.replace CGI.escapeHTML(text) + " " + link.to_html
|
||||
end
|
||||
fragment
|
||||
.css("details")
|
||||
.each do |el|
|
||||
text = el.css("summary").text
|
||||
link = fragment.document.create_element("a")
|
||||
link["href"] = post.url if post
|
||||
link.content = I18n.t("details.excerpt_details")
|
||||
el.replace CGI.escapeHTML(text) + " " + link.to_html
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,10 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'pretty_text'
|
||||
require "rails_helper"
|
||||
require "pretty_text"
|
||||
|
||||
RSpec.describe PrettyText do
|
||||
|
||||
let(:post) { Fabricate(:post) }
|
||||
|
||||
it "supports details tag" do
|
||||
@ -17,17 +16,19 @@ RSpec.describe PrettyText do
|
||||
HTML
|
||||
|
||||
expect(cooked_html).to match_html(cooked_html)
|
||||
expect(PrettyText.cook("[details=foo]\nbar\n[/details]").gsub("\n", "")).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<details class='elided'>42</details>")
|
||||
mail_html = "<p>Hello World</p>\n<a href=\"http://test.localhost\">(click for more details)</a>"
|
||||
mail_html = "<p>Hello World</p>\n<a href=\"http://test.localhost\">(click for more details)</a>"
|
||||
|
||||
expect(PrettyText.format_for_email(cooked_html)).to match_html(mail_html)
|
||||
end
|
||||
|
||||
it 'can replace spoilers in emails' do
|
||||
it "can replace spoilers in emails" do
|
||||
md = PrettyText.cook(<<~MD)
|
||||
hello
|
||||
|
||||
@ -41,7 +42,7 @@ RSpec.describe PrettyText do
|
||||
expect(md).to eq(html)
|
||||
end
|
||||
|
||||
it 'properly handles multiple spoiler blocks in a post' do
|
||||
it "properly handles multiple spoiler blocks in a post" do
|
||||
md = PrettyText.cook(<<~MD)
|
||||
[details="First"]
|
||||
body secret stuff very long
|
||||
@ -58,13 +59,13 @@ RSpec.describe PrettyText do
|
||||
MD
|
||||
|
||||
md = PrettyText.format_for_email(md, post)
|
||||
expect(md).not_to include('secret stuff')
|
||||
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)
|
||||
expect(md.scan(I18n.t("details.excerpt_details")).size).to eq(3)
|
||||
end
|
||||
|
||||
it 'escapes summary text' do
|
||||
it "escapes summary text" do
|
||||
md = PrettyText.cook(<<~MD)
|
||||
<script>alert('hello')</script>
|
||||
[details="<script>alert('hello')</script>"]
|
||||
@ -73,7 +74,6 @@ RSpec.describe PrettyText do
|
||||
MD
|
||||
md = PrettyText.format_for_email(md, post)
|
||||
|
||||
expect(md).not_to include('<script>')
|
||||
expect(md).not_to include("<script>")
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user