Skip to content

Commit

Permalink
Escape URLs in sitemaps (fixes #1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jan 12, 2017
1 parent b631e0f commit fa9e369
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/nanoc.rb
Expand Up @@ -39,6 +39,7 @@ def self.on_windows?
require 'thread'
require 'time'
require 'yaml'
require 'uri'
require 'English'

# Load Nanoc
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/helpers/xml_sitemap.rb
Expand Up @@ -30,7 +30,7 @@ def xml_sitemap(params = {})
reps.reject! { |r| !select_proc[r] } if select_proc
reps.sort_by { |r| r.name.to_s }.each do |rep|
xml.url do
xml.loc @config[:base_url] + rep.path
xml.loc URI.escape(@config[:base_url] + rep.path)
xml.lastmod item[:mtime].__nanoc_to_iso8601_date unless item[:mtime].nil?
xml.changefreq item[:changefreq] unless item[:changefreq].nil?
xml.priority item[:priority] unless item[:priority].nil?
Expand Down
27 changes: 27 additions & 0 deletions test/helpers/test_xml_sitemap.rb
Expand Up @@ -186,6 +186,33 @@ def test_sorted
end
end

def test_url_escape
if_have 'builder', 'nokogiri' do
# Create items
@items = Nanoc::Int::IdentifiableCollection.new({})
item = Nanoc::ItemWithRepsView.new(Nanoc::Int::Item.new('some content 1', {}, '/george/'), @view_context)
@items << item
create_item_rep(item.unwrap, :default, '/cool projects/проверка')

# Create sitemap item
@item = Nanoc::ItemWithRepsView.new(Nanoc::Int::Item.new('sitemap content', {}, '/sitemap/'), @view_context)

# Create site
@config = Nanoc::ConfigView.new({ base_url: 'http://example.com' }, nil)

# Build sitemap
res = xml_sitemap(items: @items)

# Check
doc = Nokogiri::XML(res)
urlsets = doc.css('> urlset')
assert_equal 1, urlsets.size
urls = urlsets.css('> url')
assert_equal 1, urls.size
assert_equal 'http://example.com/cool%20projects/%D0%BF%D1%80%D0%BE%D0%B2%D0%B5%D1%80%D0%BA%D0%B0', urls[0].css('> loc').inner_text
end
end

protected

def create_item_rep(item, name, path)
Expand Down

0 comments on commit fa9e369

Please sign in to comment.