# frozen_string_literal: true
module CategoryBadge
def self.html_for(category, opts = nil)
opts = opts || {}
# If there is no category, bail
return "" if category.blank?
# By default hide uncategorized
return "" if category.uncategorized? && !opts[:show_uncategorized]
extra_classes = "#{opts[:extra_classes]}"
result = +""
# parent class
parent_category =
Category.find_by(id: category.parent_category_id) unless category.parent_category_id.nil?
has_parent_class = parent_category ? "--has-parent" : ""
# category name
class_names = "badge-category #{has_parent_class}"
description = category.description_text ? "title='#{category.description_text}'" : ""
category_url =
opts[:absolute_url] ? "#{Discourse.base_url_no_prefix}#{category.url}" : category.url
# styles
styles = {
"--category-badge-color": "##{category.color}",
"--category-badge-text-color": "##{category.text_color}",
}
styles["--parent-category-badge-color"] = "##{parent_category.color}" if parent_category
style_value = styles.map { |k, v| "#{k}: #{ERB::Util.html_escape(v)};" }.join(" ")
# category badge structure
result << ""
result << ""
result << ERB::Util.html_escape(category.name)
result << ""
# wrapping link
result =
""
result.html_safe
end
end