Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions app/helpers/messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ def render_email_content(message)
# Parse the HTML
doc = Nokogiri::HTML.fragment(html_content)

# Remove the trix-content wrapper div if present (do this first)
trix_div = doc.at_css('div.trix-content')
if trix_div
# Replace the trix-content div with its children
trix_div.replace(trix_div.children.to_html)
# Re-parse after removing trix wrapper
doc = Nokogiri::HTML.fragment(doc.to_html)
# Detect if this is a Revolvapp email template by checking for characteristic tags
# Revolvapp emails have meta, style, title, or link tags
is_revolvapp_email = doc.css('meta, style, title, link').any?

# Only remove the trix-content wrapper for Revolvapp emails
# Regular Trix editor content needs the wrapper for proper CSS styling
if is_revolvapp_email
trix_div = doc.at_css('div.trix-content')
if trix_div
# Replace the trix-content div with its children
trix_div.replace(trix_div.children.to_html)
# Re-parse after removing trix wrapper
doc = Nokogiri::HTML.fragment(doc.to_html)
end
end

# Remove meta tags, style tags, title, and link tags
# Remove meta tags, style tags, title, and link tags (only present in Revolvapp emails)
doc.css('meta').remove
doc.css('style').remove
doc.css('title').remove
Expand All @@ -29,9 +36,11 @@ def render_email_content(message)
result = doc.to_html

# Remove leading text nodes that appear before the first HTML tag
# This handles orphaned text like "My Email" from the title tag
# Match any text at the beginning that comes before the first < character
result = result.sub(/\A([^<]*?)(<)/, '\2')
# This handles orphaned text like "My Email" from the title tag in Revolvapp emails
# Only do this for Revolvapp emails to avoid affecting regular content
if is_revolvapp_email
result = result.sub(/\A([^<]*?)(<)/, '\2')
end

result.html_safe
end
Expand Down