Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generating sitemap_index files #300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/jekyll/jekyll-sitemap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ class JekyllSitemap < Jekyll::Generator
# Main plugin action, called by Jekyll-core
def generate(site)
@site = site

@config = site.config["jekyll_sitemap"]
@config = {} unless @config.is_a?(Hash) && @config["index"].is_a?(Hash)

@index_filename = @config.dig("index", "filename") || "sitemap_index.xml"
@index_entries = @config.dig("index", "linked_sitemaps")

if @index_entries.is_a?(Array)
@priority_sitemap = @index_filename
@site.pages << sitemap_index unless file_exists?("sitemap_index.xml")
else
@priority_sitemap = "sitemap.xml"
end

@site.pages << sitemap unless file_exists?("sitemap.xml")
@site.pages << robots unless file_exists?("robots.txt")
end
Expand Down Expand Up @@ -45,6 +59,16 @@ def destination_path(file = "sitemap.xml")
@site.in_dest_dir(file)
end

def sitemap_index
index = PageWithoutAFile.new(@site, __dir__, "", "sitemap_index.xml")
index.content = File.read(source_path("sitemap_index.xml")).gsub(MINIFY_REGEX, "")
index.data["layout"] = nil
index.data["permalink"] = "/#{@index_filename}"
index.data["linked_sitemaps"] = @index_entries
index.data["xsl"] = file_exists?("sitemap_index.xsl")
index
end

def sitemap
site_map = PageWithoutAFile.new(@site, __dir__, "", "sitemap.xml")
site_map.content = File.read(source_path).gsub(MINIFY_REGEX, "")
Expand All @@ -58,6 +82,7 @@ def robots
robots = PageWithoutAFile.new(@site, __dir__, "", "robots.txt")
robots.content = File.read(source_path("robots.txt"))
robots.data["layout"] = nil
robots.data["priority_sitemap"] = @priority_sitemap
robots
end

Expand Down
2 changes: 1 addition & 1 deletion lib/robots.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Sitemap: {{ "sitemap.xml" | absolute_url }}
Sitemap: {{ page.priority_sitemap | absolute_url }}
13 changes: 13 additions & 0 deletions lib/sitemap_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
{% if page.xsl %}<?xml-stylesheet type="text/xsl" href="{{ page.xsl | absolute_url }}"?>{% endif %}
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- comment %}The first entry is always the sitemap generated for current site.{% endcomment %}
<sitemap>
<loc>{{ 'sitemap.xml' | absolute_url }}</loc>
</sitemap>
{% for entry in page.linked_sitemaps %}
<sitemap>
<loc>{{ entry | absolute_url }}</loc>
</sitemap>
{% endfor %}
</sitemapindex>
118 changes: 118 additions & 0 deletions spec/jekyll-sitemap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,122 @@
end
end
end

describe "Sitemap Index" do
let(:custom_config) { {} }
let(:config) do
Jekyll.configuration(
Jekyll::Utils.deep_merge_hashes(
overrides, custom_config
)
)
end
let(:index_filename) { "sitemap_index.xml" }
let(:index_entries) do
[
"repo1/sitemap.xml",
"repo2/sitemap.xml",
"repo3/custom-sitemap.xml",
].map { |e| "https://username.github.io/#{e}" }
end
let(:index_contents) { File.read(dest_dir(index_filename)) }
let(:robots_contents) { File.read(dest_dir("robots.txt")) }

context "with default configuration" do
it "does not generate a sitemap_index.xml file" do
expect(File.exist?(dest_dir("sitemap_index.xml"))).to_not be_truthy
end

it "generates a sitemap.xml file" do
expect(File.exist?(dest_dir("sitemap.xml"))).to be_truthy
end

it "generates a robots.txt file" do
expect(File.exist?(dest_dir("robots.txt"))).to be_truthy
expect(robots_contents).to match("Sitemap: http://example.org/sitemap.xml")
end
end

context "with improper configuration" do
let(:custom_config) do
{
"jekyll_sitemap" => {
"index" => "www.example.org/sitemap_index.xml",
},
}
end

it "does not generate a sitemap_index.xml file" do
expect(File.exist?(dest_dir("sitemap_index.xml"))).to_not be_truthy
end

it "generates a sitemap.xml file" do
expect(File.exist?(dest_dir("sitemap.xml"))).to be_truthy
end

it "generates a robots.txt file" do
expect(File.exist?(dest_dir("robots.txt"))).to be_truthy
expect(robots_contents).to match("Sitemap: http://example.org/sitemap.xml")
end
end

context "with proper configuration - I" do
let(:custom_config) do
{
"baseurl" => "bass",
"jekyll_sitemap" => {
"index" => {
"linked_sitemaps" => [],
},
},
}
end

it "generates a sitemap_index.xml file" do
expect(File.exist?(dest_dir("sitemap_index.xml"))).to be_truthy
end

it "generates a sitemap.xml file" do
expect(File.exist?(dest_dir("sitemap.xml"))).to be_truthy
end

it "generates a robots.txt file" do
expect(File.exist?(dest_dir("robots.txt"))).to be_truthy
expect(robots_contents).to match("Sitemap: http://example.org/bass/sitemap_index.xml")
end
end

context "with proper configuration - II" do
let(:index_filename) { "sitemap-index.xml" }
let(:custom_config) do
{
"url" => "https://username.github.io",
"jekyll_sitemap" => {
"index" => {
"filename" => index_filename,
"linked_sitemaps" => index_entries,
},
},
}
end

it "generates a sitemap-index.xml file" do
expect(File.exist?(dest_dir("sitemap_index.xml"))).to_not be_truthy
expect(File.exist?(dest_dir("sitemap-index.xml"))).to be_truthy

expect(index_contents).to match("<loc>https://username.github.io/sitemap.xml</loc>")
expect(index_contents).to match("<loc>https://username.github.io/repo1/sitemap.xml</loc>")
expect(index_contents).to match("<loc>https://username.github.io/repo3/custom-sitemap.xml</loc>")
end

it "generates a sitemap.xml file" do
expect(File.exist?(dest_dir("sitemap.xml"))).to be_truthy
end

it "generates a robots.txt file" do
expect(File.exist?(dest_dir("robots.txt"))).to be_truthy
expect(robots_contents).to match("Sitemap: https://username.github.io/sitemap-index.xml")
end
end
end
end