Skip to content

Commit

Permalink
make sure trailing slashes are chomped from the host. Fixes #31 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink authored Sep 1, 2023
1 parent eb81247 commit 09a0a30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions spec/sitemapper_builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,16 @@ describe Sitemapper::Builder do
XML
end
end

describe "#generate_index" do
it "trims the trailing slash from the host" do
Sitemapper.configure { |c| c.sitemap_host = "https://sitemaps.myapp.com/" }
builder = Sitemapper::Builder.new(host: "http://food.com", max_urls: 100, use_index: true)
builder.add("/burgers")
builder.generate
data = builder.generate_index
data["data"].should contain("https://sitemaps.myapp.com/sitemap.xml")
data["data"].should contain("https://sitemaps.myapp.com/sitemap_index.xml")
end
end
end
6 changes: 5 additions & 1 deletion src/sitemapper/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module Sitemapper
@sitemaps.each do |sm|
xml.element("sitemap") do
sitemap_name = sm["name"].to_s + (Sitemapper.config.compress ? ".gz" : "")
sitemap_url = [(Sitemapper.config.sitemap_host || @host), sitemap_name].join('/')
sitemap_url = [host_for_sitemap, sitemap_name].join('/')

xml.element("loc") { xml.text sitemap_url }
xml.element("lastmod") { xml.text Time.utc.to_s("%FT%X%:z") }
Expand All @@ -54,6 +54,10 @@ module Sitemapper
{"name" => filename, "data" => doc}
end

private def host_for_sitemap : String
(Sitemapper.config.sitemap_host || @host).chomp('/')
end

private def build_xml_for_page(items)
XML.build(indent: " ", version: "1.0", encoding: "UTF-8") do |xml|
xml.element("urlset", xmlns: XMLNS_SCHEMA, "xmlns:video": XMLNS_VIDEO_SCHEMA, "xmlns:image": XMLNS_IMAGE_SCHEMA, "xmlns:xsi": XMLNS_XSI, "xsi:schemaLocation": XSI_SCHEMA_LOCATION) do
Expand Down

0 comments on commit 09a0a30

Please sign in to comment.