A platform for community discussion. Free, open, simple.
FEATURE: add override for crawler title and description tags (#32259)
Gabriel Grubbac484d2fd888f
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 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 datadef crawlable_meta_data(opts = nil)opts ||= {}diff --git a/app/views/layouts/crawler.html.erb b/app/views/layouts/crawler.html.erbindex 699a0e61abf..dad9d4f7fd6 100644--- a/app/views/layouts/crawler.html.erb+++ b/app/views/layouts/crawler.html.erb@@ -2,8 +2,8 @@<html lang="<%= html_lang %>"><head><meta charset="utf-8">- <title><%= content_for?(:title) ? yield(:title) : SiteSetting.title %></title>- <meta name="description" content="<%= @description_meta || SiteSetting.site_description %>">+ <title><%= crawlable_title_content %></title>+ <meta name="description" content="<%= crawlable_description_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.rbindex 65b661d0feb..2fe556380ea 100644--- a/spec/helpers/application_helper_spec.rb+++ b/spec/helpers/application_helper_spec.rb@@ -743,34 +743,85 @@ RSpec.describe ApplicationHelper do)endend+ 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+ describe "#crawlable_title_content" do+ it "returns the correct title" do+ SiteSetting.title = "Test Title"+ result = helper.crawlable_title_content- after { DiscoursePluginRegistry.unregister_modifier(plugin, :meta_data_content, &block) }+ expect(result).to include("Test Title")+ end- it "allows the plugin to modify the meta tags" do- plugin.register_modifier(:meta_data_content, &block)+ 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_meta_data(- description: "This is a test description",- title: "to be overridden",- )+ result = helper.crawlable_title_content- 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\" />")+ 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+ contentendend++ 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++ 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")+ endenddescribe "discourse_color_scheme_stylesheets" do