mirror of
https://github.com/discourse/discourse.git
synced 2025-05-29 01:31:35 +08:00
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:
@ -270,6 +270,24 @@ module ApplicationHelper
|
|||||||
(request ? I18n.locale.to_s : SiteSetting.default_locale).sub("_", "-")
|
(request ? I18n.locale.to_s : SiteSetting.default_locale).sub("_", "-")
|
||||||
end
|
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
|
# Creates open graph and twitter card meta data
|
||||||
def crawlable_meta_data(opts = nil)
|
def crawlable_meta_data(opts = nil)
|
||||||
opts ||= {}
|
opts ||= {}
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
<html lang="<%= html_lang %>">
|
<html lang="<%= html_lang %>">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title><%= content_for?(:title) ? yield(:title) : SiteSetting.title %></title>
|
<title><%= crawlable_title_content %></title>
|
||||||
<meta name="description" content="<%= @description_meta || SiteSetting.site_description %>">
|
<meta name="description" content="<%= crawlable_description_content %>">
|
||||||
<%= render partial: "layouts/head" %>
|
<%= render partial: "layouts/head" %>
|
||||||
<%= render partial: "common/discourse_stylesheet" %>
|
<%= render partial: "common/discourse_stylesheet" %>
|
||||||
<%= theme_lookup("head_tag") %>
|
<%= theme_lookup("head_tag") %>
|
||||||
|
@ -743,33 +743,84 @@ RSpec.describe ApplicationHelper do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "when a plugin registers the :meta_data_content modifier" do
|
describe "#crawlable_title_content" do
|
||||||
let!(:plugin) { Plugin::Instance.new }
|
it "returns the correct title" do
|
||||||
let!(:block) do
|
SiteSetting.title = "Test Title"
|
||||||
->(content, property, opts) do
|
result = helper.crawlable_title_content
|
||||||
content << " - modified by plugin" if property == :description
|
|
||||||
content = "BIG TITLE" if property == :title
|
expect(result).to include("Test Title")
|
||||||
content
|
end
|
||||||
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
|
||||||
|
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
|
it "allows the plugin to modify the meta tags" do
|
||||||
plugin.register_modifier(:meta_data_content, &block)
|
plugin.register_modifier(:meta_data_content, &block)
|
||||||
|
|
||||||
result =
|
result =
|
||||||
helper.crawlable_meta_data(
|
helper.crawlable_meta_data(
|
||||||
description: "This is a test description",
|
description: "This is a test description",
|
||||||
title: "to be overridden",
|
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
|
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")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user