FEATURE: Show more context in Discourse topic oneboxes

Currently when generating a onebox for Discourse topics, some important
context is missing such as categories and tags.

This patch addresses this issue by introducing a new onebox engine
dedicated to display this information when available. Indeed to get this
new information, categories and tags are exposed in the topic metadata
as opengraph tags.
This commit is contained in:
Loïc Guitaut
2022-11-24 16:28:21 +01:00
committed by Loïc Guitaut
parent d2e9ea6193
commit 14d97f9cf1
16 changed files with 298 additions and 26 deletions

View File

@ -4,19 +4,11 @@ module Onebox
class Normalizer
attr_reader :data
def get(attr, length = nil, sanitize = true)
return nil if Onebox::Helpers.blank?(data)
def get(attr, *args)
value = data[attr]
return nil if Onebox::Helpers.blank?(value)
value = html_entities.decode(value)
value = Sanitize.fragment(value) if sanitize
value.strip!
value = Onebox::Helpers.truncate(value, length) unless length.nil?
value
return if value.blank?
return value.map { |v| sanitize_value(v, *args) } if value.is_a?(Array)
sanitize_value(value, *args)
end
def method_missing(attr, *args, &block)
@ -48,5 +40,13 @@ module Onebox
def html_entities
@html_entities ||= HTMLEntities.new
end
def sanitize_value(value, length = nil, sanitize = true)
value = html_entities.decode(value)
value = Sanitize.fragment(value) if sanitize
value.strip!
value = Onebox::Helpers.truncate(value, length) if length
value
end
end
end