Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ gem "net-http-persistent"
gem "kredis"
gem "platform_agent"
gem "thruster"
gem "redcarpet"

group :development, :test do
gem "debug"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ GEM
rdoc (6.14.2)
erb
psych (>= 4.0.0)
redcarpet (3.6.1)
redis (5.4.1)
redis-client (>= 0.22.0)
redis-client (0.25.2)
Expand Down Expand Up @@ -423,6 +424,7 @@ DEPENDENCIES
puma (~> 6.6)
rails!
rails_autolink
redcarpet
redis (~> 5.4)
resque (~> 2.7.0)
resque-pool (~> 0.7.1)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/content_filters.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ContentFilters
TextMessagePresentationFilters = ActionText::Content::Filters.new(RemoveSoloUnfurledLinkText, StyleUnfurledTwitterAvatars, SanitizeTags)
TextMessagePresentationFilters = ActionText::Content::Filters.new(MarkdownFilter, RemoveSoloUnfurledLinkText, StyleUnfurledTwitterAvatars, SanitizeTags)
end
52 changes: 52 additions & 0 deletions app/helpers/content_filters/markdown_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class ContentFilters::MarkdownFilter < ActionText::Content::Filter
def applicable?
contains_markdown?(content.to_plain_text)
end

def apply
# Process the entire content as markdown
plain_text = content.to_plain_text
html = markdown_to_html(plain_text)

# Replace the entire fragment content with the HTML
fragment.update do |source|
source.inner_html = html
end
end

private

def contains_markdown?(text)
# Check for common markdown patterns
text.match?(/\*\*.*\*\*|__.*__|\*.*\*|_.*_|`.*`|#{Regexp.escape("```")}.*#{Regexp.escape("```")}|^#{Regexp.escape("#")}|^#{Regexp.escape("-")}|^#{Regexp.escape("*")}|^#{Regexp.escape("1.")}|\[.*\]\(.*\)/)
end

def markdown_to_html(text)
renderer = Redcarpet::Render::HTML.new(
filter_html: false,
no_images: false,
no_links: false,
no_styles: false,
safe_links_only: false,
with_toc_data: false,
hard_wrap: true,
link_attributes: { target: "_blank" }
)

markdown = Redcarpet::Markdown.new(renderer, {
autolink: true,
disable_indented_code_blocks: false,
fenced_code_blocks: true,
footnotes: false,
highlight: false,
no_intra_emphasis: false,
space_after_headers: false,
strikethrough: true,
superscript: false,
tables: true,
underline: true
})

markdown.render(text)
end
end
4 changes: 3 additions & 1 deletion app/helpers/messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def message_presentation(message)
when "sound"
message_sound_presentation(message)
else
auto_link h(ContentFilters::TextMessagePresentationFilters.apply(message.body.body)), html: { target: "_blank" }
# Apply markdown rendering first, then other content filters
processed_content = ContentFilters::TextMessagePresentationFilters.apply(message.body.body)
auto_link processed_content, html: { target: "_blank" }
end
rescue Exception => e
Sentry.capture_exception(e, extra: { message: message })
Expand Down