Skip to content

Commit

Permalink
Add namespace attributes for images and videos (#20)
Browse files Browse the repository at this point in the history
* Add namespace attributes for images and videos

I kept running into errors like this when checking my sitemap that's chock full of videos:

> Namespace prefix video on thumbnail_loc is not defined

I checked Google's docs, and for both Images and Videos they say that we need to include these namespaces:
- https://developers.google.com/search/docs/advanced/sitemaps/video-sitemaps#xml-namespace
- https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps#xml-namespace

I'm sure there's something "smarter" that could be done to only add these when videos/images are present, but I don't think that the extra characters will be of much consequence.

I also wasn't sure if Bing has their own namespace or something, but I couldn't find anything on their documentation so I'm sort of assuming that the whole internet is letting Google define the namespaces.

* See if workflows will run

* Small tweak back to original to prompt workflows?

* Updates for Crystal format

* Fix specs with updated namespace inclusion
  • Loading branch information
stephendolan authored Jun 1, 2021
1 parent 54b1233 commit 193661a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ on:
push:
branches: [master]
pull_request:
branches: "*"
schedule:
- cron: "0 0 * * MON"
workflow_dispatch:

jobs:
check_format:
Expand Down
2 changes: 1 addition & 1 deletion spec/sitemapper_builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe Sitemapper::Builder do
XML

xml.should contain <<-XML
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
XML

xml.should contain <<-XML
Expand Down
8 changes: 5 additions & 3 deletions src/sitemapper/builder.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Sitemapper
class Builder
XMLNS_SCHEMA = "https://www.sitemaps.org/schemas/sitemap/0.9"
XMLNS_SCHEMA = "https://www.sitemaps.org/schemas/sitemap/0.9"
XMLNS_VIDEO_SCHEMA = "http://www.google.com/schemas/sitemap-video/1.1"
XMLNS_IMAGE_SCHEMA = "http://www.google.com/schemas/sitemap-image/1.1"

getter paginator

Expand All @@ -18,7 +20,7 @@ module Sitemapper
@paginator.total_pages.times do |page|
filename = filename_for_page(page)
doc = XML.build(indent: " ", version: "1.0", encoding: "UTF-8") do |xml|
xml.element("urlset", xmlns: XMLNS_SCHEMA) do
xml.element("urlset", xmlns: XMLNS_SCHEMA, "xmlns:video": XMLNS_VIDEO_SCHEMA, "xmlns:image": XMLNS_IMAGE_SCHEMA) do
@paginator.items(page + 1).each do |info|
build_xml_from_info(xml, info)
end
Expand All @@ -30,7 +32,7 @@ module Sitemapper

if @use_index
doc = XML.build(indent: " ", version: "1.0", encoding: "UTF-8") do |xml|
xml.element("sitemapindex", xmlns: XMLNS_SCHEMA) do
xml.element("sitemapindex", xmlns: XMLNS_SCHEMA, "xmlns:video": XMLNS_VIDEO_SCHEMA, "xmlns:image": XMLNS_IMAGE_SCHEMA) do
@sitemaps.each do |sm|
xml.element("sitemap") do
sitemap_name = sm["name"].to_s + (Sitemapper.config.compress ? ".gz" : "")
Expand Down

0 comments on commit 193661a

Please sign in to comment.