A platform for community discussion. Free, open, simple.
FIX: format posts for embedded comments as we do for emails
Régis Hanol6a006b3646df
diff --git a/app/helpers/embed_helper.rb b/app/helpers/embed_helper.rbindex 53eedd62592..3115f59e1f4 100644--- a/app/helpers/embed_helper.rb+++ b/app/helpers/embed_helper.rb@@ -14,27 +14,7 @@ module EmbedHelperendend- def get_html(cooked)- fragment = Nokogiri::HTML.fragment(cooked)-- # convert lazyYT div to link- fragment.css('div.lazyYT').each do |yt_div|- youtube_id = yt_div["data-youtube-id"]- youtube_link = "https://www.youtube.com/watch?v=#{youtube_id}"- yt_div.replace "<p><a href='#{youtube_link}'>#{youtube_link}</a></p>"- end-- # convert Vimeo iframe to link- fragment.css('iframe').each do |iframe|- if iframe['src'] =~ /player.vimeo.com/- vimeo_id = iframe['src'].split('/').last- iframe.replace "<p><a href='https://vimeo.com/#{vimeo_id}'>https://vimeo.com/#{vimeo_id}</a></p>"- end- end-- # Strip lightbox metadata- fragment.css('.lightbox-wrapper .meta').remove-- raw fragment+ def get_html(post)+ raw PrettyText.format_for_email(post.cooked, post)endenddiff --git a/app/views/embed/comments.html.erb b/app/views/embed/comments.html.erbindex b6863c21aab..3c26b1b32d1 100644--- a/app/views/embed/comments.html.erb+++ b/app/views/embed/comments.html.erb@@ -25,7 +25,7 @@<span class='title'><%= post.user.title %></span><%- end %></h3>- <%= get_html(post.cooked) %>+ <%= get_html(post) %><%- if post.reply_count > 0 && post.replies.exists? %><%- if post.reply_count == 1 %>diff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex ebfe8a23e1b..f157800e386 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -343,29 +343,36 @@ module PrettyTextfragment.to_htmlend- # Given a Nokogiri doc, convert all links to absolute- def self.make_all_links_absolute(doc)- site_uri = nil- doc.css("a").each do |link|- href = link["href"].to_s- begin- uri = URI(href)- site_uri ||= URI(Discourse.base_url)- link["href"] = "#{site_uri}#{link['href']}" unless uri.host.present?- rescue URI::InvalidURIError, URI::InvalidComponentError- # leave it- end- end- end+ def self.make_all_links_absolute(doc)+ site_uri = nil+ doc.css("a").each do |link|+ href = link["href"].to_s+ begin+ uri = URI(href)+ site_uri ||= URI(Discourse.base_url)+ link["href"] = "#{site_uri}#{link['href']}" unless uri.host.present?+ rescue URI::InvalidURIError, URI::InvalidComponentError+ # leave it+ end+ end+ enddef self.strip_image_wrapping(doc)doc.css(".lightbox-wrapper .meta").removeend+ def self.convert_vimeo_iframes(doc)+ doc.css("iframe[src*='player.vimeo.com']").each do |iframe|+ vimeo_id = iframe['src'].split('/').last+ iframe.replace "<p><a href='https://vimeo.com/#{vimeo_id}'>https://vimeo.com/#{vimeo_id}</a></p>"+ end+ end+def self.format_for_email(html, post = nil)doc = Nokogiri::HTML.fragment(html)DiscourseEvent.trigger(:reduce_cooked, doc, post)strip_image_wrapping(doc)+ convert_vimeo_iframes(doc)make_all_links_absolute(doc)doc.to_htmlend