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.
This commit is contained in:
Gabriel Grubba
2025-04-11 09:38:11 -03:00
committed by GitHub
parent 7103078fa1
commit c484d2fd88
3 changed files with 92 additions and 23 deletions

View File

@ -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 ||= {}

View File

@ -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") %>

View File

@ -743,6 +743,42 @@ RSpec.describe ApplicationHelper do
)
end
end
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 }
@ -770,6 +806,21 @@ RSpec.describe ApplicationHelper do
)
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")
end
end