DEV: Add plugin API to add to robots.txt (#17378)

This plugin API can be used to add to robots.txt. The event handler
receives the complete robots information before it is converted into
robots.txt.
This commit is contained in:
Bianca Nenciu 2022-07-12 20:52:55 +03:00 committed by GitHub
parent 2e7e48d982
commit 09f1ef6b05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -84,6 +84,8 @@ class RobotsTxtController < ApplicationController
result[:agents] << { name: 'Googlebot', disallow: deny_paths_googlebot }
end
DiscourseEvent.trigger(:robots_info, result)
result
end
end

View File

@ -161,5 +161,25 @@ RSpec.describe RobotsTxtController do
expect(response.body).not_to include(sitemap_line)
end
end
describe 'plugins' do
let(:event_handler) do
Proc.new { |robots_info| robots_info[:agents] << { name: 'Test', disallow: ['/test/'] } }
end
before do
DiscourseEvent.on(:robots_info, &event_handler)
end
after do
DiscourseEvent.off(:robots_info, &event_handler)
end
it 'can add to robots.txt' do
get '/robots.txt'
expect(response.parsed_body).to include("User-agent: Test\nDisallow: /test/")
end
end
end
end