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
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)
end
end

View File

@ -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 %>

View File

@ -343,29 +343,36 @@ module PrettyText
fragment.to_html
end
# 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
end
def self.strip_image_wrapping(doc)
doc.css(".lightbox-wrapper .meta").remove
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)
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_html
end