Skip to content

Commit

Permalink
Revert "hack?"
Browse files Browse the repository at this point in the history
This reverts commit 72ba881.
  • Loading branch information
jedwin3210 committed Sep 22, 2024
1 parent 72ba881 commit 09bf85f
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions spec/support/jekyll.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# frozen_string_literal: true

require 'jekyll'
require 'yaml'

# Tools to build / compile the Jekyll site and extract the sitemap
def site_config
# Consider appending jekyll-sitemap to the plugins here, instead of in _config.
# TODO(template): We should standardize the build for specs
# Consider simplifying baseurl
# Consider forcing the desination folder
# Override the local URL too? Would it break the sitemap?
# Note: Config keys must be strings and thus use => style hashes.
@site_config ||= Jekyll.configuration({
'destination' => DESTINATION,
'url' => '',
'baseurl' => '',
'future' => true,
'SKIP_CONFIG_VALIDATION' => 'true',
'sass' => { 'quiet_deps' => true }
})
@site_config ||= Jekyll.configuration({ 'sass' => { 'quiet_deps' => true } })
end

def build_jekyll_site!
Expand All @@ -24,24 +19,47 @@ def build_jekyll_site!
puts 'Site build complete.'
end

# Turn a page path into a symbol to used in specs.
# e.g. rspec --tag index
def path_to_sym(path)
return :index if path == '/'

path.gsub(/^\//, '').gsub(/\/$/, '').gsub('/', '-').to_sym
end

def load_sitemap
# Ensure that you have called build_jekyll_site! first.
sitemap_text = File.read("#{DESTINATION}/sitemap.xml")
sitemap_links = sitemap_text.scan(%r{<loc>(.+)</loc>})
sitemap_links.filter_map do |link, *_unused|
# Ensure that you have called
sitemap_text = File.read('_site/sitemap.xml')
sitemap_links = sitemap_text.scan(%r{<loc>.+</loc>})
sitemap_links.filter_map do |link|
link = link.gsub("<loc>#{site_config['url']}", '').gsub('</loc>', '')
# Skip non-html pages
# Are there other pages that should be audited for accessibility?
# (FUTURE?) Are there other pages that should be audited for accessibility?
# (e.g. PDFs, documents. They'd need a different checker.)
next unless link.end_with?('.html') || link.end_with?('/')

link
end.sort
end

# Start a local Rack server
# https://nts.strzibny.name/how-to-test-static-sites-with-rspec-capybara-and-webkit/
class StaticSite
attr_reader :root, :server

def initialize(root)
@root = root
@server = Rack::Files.new(root)
end

def call(env)
# Remove the /baseurl prefix, which is present in all URLs, but not in the file system.
path = "_site#{env['PATH_INFO'].gsub(site_config['baseurl'], '/')}"

env['PATH_INFO'] = if path.end_with?('/') && exists?("#{path}index.html")
"#{path}index.html"
elsif !exists?(path) && exists?("#{path}.html")
"#{path}.html"
else
path
end

server.call(env)
end

def exists?(path)
File.exist?(File.join(root, path))
end
end

0 comments on commit 09bf85f

Please sign in to comment.