From f30c47524dd372166f6db05d954c1e2e62e072c5 Mon Sep 17 00:00:00 2001 From: Andy Jones Date: Wed, 15 Jun 2022 11:33:35 +0100 Subject: [PATCH] Added functionality for sitemap --- Gemfile | 3 +++ Gemfile.lock | 2 ++ config.rb | 2 ++ source/sitemap.txt.erb | 1 + source/sitemap.xml.builder | 16 ++++++++++++++++ 5 files changed, 24 insertions(+) create mode 100644 source/sitemap.txt.erb create mode 100644 source/sitemap.xml.builder diff --git a/Gemfile b/Gemfile index afef363..a392743 100644 --- a/Gemfile +++ b/Gemfile @@ -10,3 +10,6 @@ gem 'tzinfo-data', platforms: [:mswin, :mingw, :x64_mingw, :jruby] # Include the tech docs gem gem 'govuk_tech_docs' + + +gem "builder", "~> 3.0" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 8b1cd3a..4a2a86c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,6 +12,7 @@ GEM autoprefixer-rails (10.4.7.0) execjs (~> 2) backports (3.23.0) + builder (3.2.4) chronic (0.10.2) chunky_png (1.4.0) coffee-script (2.4.1) @@ -183,6 +184,7 @@ PLATFORMS x64-mingw32 DEPENDENCIES + builder (~> 3.0) govuk_tech_docs tzinfo-data wdm (~> 0.1.0) diff --git a/config.rb b/config.rb index 1c8274e..d4a5334 100644 --- a/config.rb +++ b/config.rb @@ -15,4 +15,6 @@ activate :basic_panel activate :back_to_top +config[:host] = "https://apply-the-service-standard.education.gov.uk" + GovukTechDocs.configure(self) diff --git a/source/sitemap.txt.erb b/source/sitemap.txt.erb new file mode 100644 index 0000000..1f75330 --- /dev/null +++ b/source/sitemap.txt.erb @@ -0,0 +1 @@ +<% sitemap.resources.each do |page| %><%= "#{URI.join(config[:host], page.url)}\n" if page.url !~ /\.(css|js|eot|svg|woff|ttf|png|jpg|jpeg|gif|keep)$/ %><% end %> \ No newline at end of file diff --git a/source/sitemap.xml.builder b/source/sitemap.xml.builder new file mode 100644 index 0000000..45c17f3 --- /dev/null +++ b/source/sitemap.xml.builder @@ -0,0 +1,16 @@ +xml.instruct! +xml.urlset 'xmlns' => "http://www.sitemaps.org/schemas/sitemap/0.9" do + sitemap.resources.select { |page| page.destination_path =~ /\.html/ && page.data.noindex != true }.each do |page| + xml.url do + xml.loc URI.join(config[:host], page.destination_path) + last_mod = if page.path.start_with?('articles/') + File.mtime(page.source_file).to_time + else + Time.now + end + xml.lastmod last_mod.iso8601 + xml.changefreq page.data.changefreq || "monthly" + xml.priority page.data.priority || "0.5" + end + end +end \ No newline at end of file