From c484d2fd888f1d1b35e0a5ab619bb0662bf0e56c Mon Sep 17 00:00:00 2001 From: Gabriel Grubba <70247653+Grubba27@users.noreply.github.com> Date: Fri, 11 Apr 2025 09:38:11 -0300 Subject: [PATCH] FEATURE: add override for crawler title and description tags (#32259) In https://github.com/discourse/discourse/pull/32159 we overrode the `og:` and `twitter:` title and description but for some crawlers, we need to override the `title` and `description` meta tag as well. --- app/helpers/application_helper.rb | 18 +++++ app/views/layouts/crawler.html.erb | 4 +- spec/helpers/application_helper_spec.rb | 93 +++++++++++++++++++------ 3 files changed, 92 insertions(+), 23 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 13b5ff3cd3f..13f82d96eab 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -270,6 +270,24 @@ module ApplicationHelper (request ? I18n.locale.to_s : SiteSetting.default_locale).sub("_", "-") end + def crawlable_title_content + DiscoursePluginRegistry.apply_modifier( + :meta_data_content, + content_for(:title) || SiteSetting.title, + :title, + { url: request.fullpath }, + ) + end + + def crawlable_description_content + DiscoursePluginRegistry.apply_modifier( + :meta_data_content, + @description_meta || SiteSetting.site_description, + :description, + { url: request.fullpath }, + ) + end + # Creates open graph and twitter card meta data def crawlable_meta_data(opts = nil) opts ||= {} diff --git a/app/views/layouts/crawler.html.erb b/app/views/layouts/crawler.html.erb index 699a0e61abf..dad9d4f7fd6 100644 --- a/app/views/layouts/crawler.html.erb +++ b/app/views/layouts/crawler.html.erb @@ -2,8 +2,8 @@ - <%= content_for?(:title) ? yield(:title) : SiteSetting.title %> - + <%= crawlable_title_content %> + <%= render partial: "layouts/head" %> <%= render partial: "common/discourse_stylesheet" %> <%= theme_lookup("head_tag") %> diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 65b661d0feb..2fe556380ea 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -743,33 +743,84 @@ RSpec.describe ApplicationHelper do ) end 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 + describe "#crawlable_title_content" do + it "returns the correct title" do + SiteSetting.title = "Test Title" + result = helper.crawlable_title_content + + expect(result).to include("Test Title") + end + + it "accepts a content argument" do + helper.stubs(:content_for?).with(:title).returns(true) + helper.stubs(:content_for).with(:title).returns("Custom Title") + + result = helper.crawlable_title_content + + expect(result).to include("Custom Title") + end + end + + describe "#crawlable_description_content" do + it "returns the correct description" do + SiteSetting.site_description = "Test Description" + result = helper.crawlable_description_content + + expect(result).to include("Test Description") + end + + it "accepts a content argument" do + @description_meta = "Custom Description" + + result = helper.crawlable_description_content + + expect(result).to include("Custom Description") + 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) } + 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) + 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( - "", + result = + helper.crawlable_meta_data( + description: "This is a test description", + title: "to be overridden", ) - expect(result).to include("") - end + + expect(result).to include( + "", + ) + expect(result).to include("") + end + + it "modifies the title tag" do + plugin.register_modifier(:meta_data_content, &block) + + title = helper.crawlable_title_content + + expect(title).to include("BIG TITLE") + end + + it "modifies the description tag" do + plugin.register_modifier(:meta_data_content, &block) + + description = helper.crawlable_description_content + + expect(description).to include(" - modified by plugin") end end