Skip to content

A platform for community discussion. Free, open, simple.

UX: Link to diagrams instead of showing their source in emails

Régis Hanol649f6820834e
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index d8484004c2c..12164336fcf 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -485,6 +485,7 @@ en:
unrecognized_error: "Unrecognized Error"
secure_uploads_placeholder: "Redacted: This site has secure uploads enabled, visit the topic or click View Media to see the attached uploads."
view_redacted_media: "View Media"
+ view_diagram: "View diagram"
errors: &errors
format: ! "%{attribute} %{message}"
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 9a1043e7f3e..6aea62899de 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -530,6 +530,14 @@ module PrettyText
doc.css(".video-onebox,video").remove
end
+ def self.replace_diagrams(doc, post, selector = "pre[data-code-wrap='mermaid']")
+ doc
+ .css(selector)
+ .each do |node|
+ node.replace(link_paragraph(doc, post.full_url, I18n.t("emails.view_diagram")))
+ end
+ end
+
def self.convert_vimeo_iframes(doc)
doc
.css("iframe[src*='player.vimeo.com']")
@@ -655,6 +663,7 @@ module PrettyText
DiscourseEvent.trigger(:reduce_cooked, doc, post)
strip_secure_uploads(doc) if post&.should_secure_uploads?
strip_image_wrapping(doc)
+ replace_diagrams(doc, post) if post
convert_vimeo_iframes(doc)
make_all_links_absolute(doc)
doc.to_html
diff --git a/plugins/discourse-graphviz/plugin.rb b/plugins/discourse-graphviz/plugin.rb
index 783d24e6a3a..fd5544e469b 100644
--- a/plugins/discourse-graphviz/plugin.rb
+++ b/plugins/discourse-graphviz/plugin.rb
@@ -11,3 +11,9 @@ enabled_site_setting :discourse_graphviz_enabled
register_svg_icon "diagram-project"
register_asset "stylesheets/common/graphviz.scss"
+
+after_initialize do
+ on(:reduce_cooked) do |fragment, post|
+ PrettyText.replace_diagrams(fragment, post, "div.graphviz") if post
+ end
+end
diff --git a/plugins/discourse-graphviz/spec/integration/graphviz_email_spec.rb b/plugins/discourse-graphviz/spec/integration/graphviz_email_spec.rb
new file mode 100644
index 00000000000..f2ddd6bcc74
--- /dev/null
+++ b/plugins/discourse-graphviz/spec/integration/graphviz_email_spec.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+RSpec.describe "Graphviz in emails" do
+ before { SiteSetting.discourse_graphviz_enabled = true }
+
+ let(:post) { Fabricate(:post, raw: "[graphviz]\ndigraph G { a -> b }\n[/graphviz]") }
+
+ it "replaces the graph with a link to the post" do
+ expect(PrettyText.format_for_email(post.cooked, post)).to match_html(
+ %(<p><a href="#{post.full_url}">#{I18n.t("emails.view_diagram")}</a></p>),
+ )
+ end
+
+ it "keeps the graph when there is no post to link to" do
+ expect(PrettyText.format_for_email(post.cooked)).to include("digraph G")
+ end
+end
diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb
index 111661a9cb3..4255cffdea6 100644
--- a/spec/lib/pretty_text_spec.rb
+++ b/spec/lib/pretty_text_spec.rb
@@ -1407,6 +1407,26 @@ RSpec.describe PrettyText do
end
end
+ describe ".replace_diagrams" do
+ let(:html) { <<~HTML }
+ <pre data-code-wrap="mermaid"><code class="lang-mermaid">flowchart LR
+ A --&gt; B
+ </code></pre>
+ <pre data-code-wrap="ruby"><code class="lang-ruby">puts 1</code></pre>
+ HTML
+
+ it "replaces diagram code blocks with a link to the post" do
+ expect(described_class.format_for_email(html, post)).to match_html(<<~HTML)
+ <p><a href="#{post.full_url}">#{I18n.t("emails.view_diagram")}</a></p>
+ <pre data-code-wrap="ruby"><code class="lang-ruby">puts 1</code></pre>
+ HTML
+ end
+
+ it "keeps diagram code blocks when there is no post to link to" do
+ expect(described_class.format_for_email(html)).to include("lang-mermaid")
+ end
+ end
+
describe ".convert_vimeo_iframes" do
it "converts a Vimeo iframe to a link, ignoring an invalid unlisted hash" do
html = %(<p>Video:</p><iframe src="https://player.vimeo.com/video/1?h=not-valid!"></iframe>)