discourse/app/serializers/post_item_excerpt.rb
David Battersby 1150994e42
FIX: draft excerpts with deeply nested html (#35678)
Posts with deeply nested html structures (ie. greater than Nokogiri's
max tree depth) is causing a 500 error when viewing the drafts list
route.

When we can't process the excerpt for overly complex posts, we can just
return a blank value which will still allows us to view and edit the
draft as usual.

Internal ref: /t/-/166042
2025-11-03 13:37:15 +04:00

30 lines
566 B
Ruby
Vendored

# frozen_string_literal: true
module PostItemExcerpt
def self.included(base)
base.attributes(:excerpt, :truncated)
end
def cooked
@cooked ||= object.cooked || PrettyText.cook(object.raw)
end
def excerpt
return nil unless cooked
@excerpt ||=
begin
PrettyText.excerpt(cooked, 300, keep_emoji_images: true)
rescue ArgumentError => e
e.message.include?("Document tree depth limit exceeded") ? "" : raise
end
end
def truncated
true
end
def include_truncated?
cooked.length > 300
end
end