Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add modifier for meta_data_content in ApplicationHelper (#32159)

Gabriel Grubba80e6d3d8c579
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 24a30d7a459..13b5ff3cd3f 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -307,6 +307,8 @@ module ApplicationHelper
%i[url title description].each do |property|
if opts[property].present?
content = (property == :url ? opts[property] : gsub_emoji_to_unicode(opts[property]))
+ content =
+ DiscoursePluginRegistry.apply_modifier(:meta_data_content, content, property, opts)
result << tag(:meta, { property: "og:#{property}", content: content }, nil, true)
result << tag(:meta, { name: "twitter:#{property}", content: content }, nil, true)
end
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index 588656fd7ed..65b661d0feb 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -743,6 +743,34 @@ RSpec.describe ApplicationHelper do
)
end
end
+
+ describe "when a plugin registers the :meta_data_content modifier" do
+ let!(:plugin) { Plugin::Instance.new }
+ let!(:block) do
+ ->(content, property, opts) do
+ content << " - modified by plugin" if property == :description
+ content = "BIG TITLE" if property == :title
+ content
+ end
+ end
+
+ after { DiscoursePluginRegistry.unregister_modifier(plugin, :meta_data_content, &block) }
+
+ it "allows the plugin to modify the meta tags" do
+ plugin.register_modifier(:meta_data_content, &block)
+
+ result =
+ helper.crawlable_meta_data(
+ description: "This is a test description",
+ title: "to be overridden",
+ )
+
+ expect(result).to include(
+ "<meta property=\"og:description\" content=\"This is a test description - modified by plugin\" />",
+ )
+ expect(result).to include("<meta property=\"og:title\" content=\"BIG TITLE\" />")
+ end
+ end
end
describe "discourse_color_scheme_stylesheets" do