FIX: format posts for embedded comments as we do for emails

This commit is contained in:
Régis Hanol
2018-05-09 19:24:44 +02:00
parent 86eb3528ec
commit 6a006b3646
3 changed files with 24 additions and 37 deletions

View File

@@ -14,27 +14,7 @@ module EmbedHelper
end end
end end
def get_html(cooked) def get_html(post)
fragment = Nokogiri::HTML.fragment(cooked) raw PrettyText.format_for_email(post.cooked, post)
# 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
end end
end end

View File

@@ -25,7 +25,7 @@
<span class='title'><%= post.user.title %></span> <span class='title'><%= post.user.title %></span>
<%- end %> <%- end %>
</h3> </h3>
<%= get_html(post.cooked) %> <%= get_html(post) %>
<%- if post.reply_count > 0 && post.replies.exists? %> <%- if post.reply_count > 0 && post.replies.exists? %>
<%- if post.reply_count == 1 %> <%- if post.reply_count == 1 %>

View File

@@ -343,29 +343,36 @@ module PrettyText
fragment.to_html fragment.to_html
end end
# Given a Nokogiri doc, convert all links to absolute def self.make_all_links_absolute(doc)
def self.make_all_links_absolute(doc) site_uri = nil
site_uri = nil doc.css("a").each do |link|
doc.css("a").each do |link| href = link["href"].to_s
href = link["href"].to_s begin
begin uri = URI(href)
uri = URI(href) site_uri ||= URI(Discourse.base_url)
site_uri ||= URI(Discourse.base_url) link["href"] = "#{site_uri}#{link['href']}" unless uri.host.present?
link["href"] = "#{site_uri}#{link['href']}" unless uri.host.present? rescue URI::InvalidURIError, URI::InvalidComponentError
rescue URI::InvalidURIError, URI::InvalidComponentError # leave it
# leave it end
end end
end end
end
def self.strip_image_wrapping(doc) def self.strip_image_wrapping(doc)
doc.css(".lightbox-wrapper .meta").remove doc.css(".lightbox-wrapper .meta").remove
end end
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) def self.format_for_email(html, post = nil)
doc = Nokogiri::HTML.fragment(html) doc = Nokogiri::HTML.fragment(html)
DiscourseEvent.trigger(:reduce_cooked, doc, post) DiscourseEvent.trigger(:reduce_cooked, doc, post)
strip_image_wrapping(doc) strip_image_wrapping(doc)
convert_vimeo_iframes(doc)
make_all_links_absolute(doc) make_all_links_absolute(doc)
doc.to_html doc.to_html
end end