mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 09:13:38 +08:00
14d97f9cf1
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.
60 lines
1.6 KiB
Ruby
60 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Onebox
|
|
module Engine
|
|
class DiscourseTopicOnebox
|
|
include Engine
|
|
include StandardEmbed
|
|
include LayoutSupport
|
|
|
|
matches_regexp(%r{/t/.*(/\d+)?})
|
|
|
|
def data
|
|
@data ||= {
|
|
categories: categories,
|
|
link: link,
|
|
article_published_time: published_time.strftime("%-d %b %y"),
|
|
article_published_time_title: published_time.strftime("%I:%M%p - %d %B %Y"),
|
|
domain: html_entities.decode(raw[:site_name].truncate(80, separator: " ")),
|
|
description: html_entities.decode(raw[:description].truncate(250, separator: " ")),
|
|
title: html_entities.decode(raw[:title].truncate(80, separator: " ")),
|
|
image: image,
|
|
render_tags?: render_tags?,
|
|
render_category_block?: render_category_block?,
|
|
}.reverse_merge(raw)
|
|
end
|
|
alias verified_data data
|
|
|
|
private
|
|
|
|
def categories
|
|
Array
|
|
.wrap(raw[:article_sections])
|
|
.map
|
|
.with_index { |name, index| { name: name, color: raw[:article_section_colors][index] } }
|
|
end
|
|
|
|
def published_time
|
|
@published_time ||= Time.parse(raw[:published_time])
|
|
end
|
|
|
|
def html_entities
|
|
@html_entities ||= HTMLEntities.new
|
|
end
|
|
|
|
def image
|
|
image = Onebox::Helpers.get_absolute_image_url(raw[:image], @url)
|
|
Onebox::Helpers.normalize_url_for_output(html_entities.decode(image))
|
|
end
|
|
|
|
def render_tags?
|
|
raw[:article_tags].present?
|
|
end
|
|
|
|
def render_category_block?
|
|
render_tags? || categories.present?
|
|
end
|
|
end
|
|
end
|
|
end
|