Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ jobs:
run: |
JEKYLL_ENV=production bundle exec jekyll build
JEKYLL_ENV=production bundle exec jekyll doctor
# SKIP_BUILD=true bundle exec rake test
# NOTE: サイトが仕上がったら、上記テストを走らせると自動検知できる。
# ただ初期は自動検知の通知が多すぎるので手動で実行するのが吉。
python test-directory-structure.py
SKIP_BUILD=true bundle exec rake test

# Deploy job is triggered only pushed to main branch && CI passed
deploy:
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ task default: 'test'
# HTML Proofer helps to detect broken links.
# https://github.com/gjtorikian/html-proofer
require 'html-proofer'
require_relative '_tests/custom_checks'

task test: [:build] do
options = {
checks: ['Links', 'Images', 'Scripts', 'OpenGraph', 'Favicon'],
checks: ['Links', 'Images', 'Scripts', 'OpenGraph', 'Favicon', 'CustomChecks'],
allow_hash_href: false,
disable_external: true,
enforce_https: true,
Expand Down
54 changes: 54 additions & 0 deletions _tests/custom_checks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# See the following 'Custom Tests' section to add tests ;)
# https://github.com/gjtorikian/html-proofer#custom-tests

require 'json'
require 'yaml'

class CustomChecks < ::HTMLProofer::Check
BASE_PATH = '_site'

def run
current_filename = @runner.current_filename
puts "\tchecking ... " + current_filename[5..].split('.').first

check_directory_structure
end

# Check directory structure to ensure all pages are generated as index.html
# This ensures proper routing for both /foobar/ and /foobar pathes.
def check_directory_structure
allowed_names = ['index.html', '404.html']
current_file = @runner.current_filename

# Only check HTML files
return unless current_file.end_with?('.html')

# Get the filename
filename = File.basename(current_file)

# Skip if allowed filenames or redirect files
return if allowed_names.include?(filename)
return if is_redirect_file?(current_file)

# Report failure for non-index HTML files
add_failure(
<<~ERROR_MESSAGE
index.html でない HTML ファイルが生成されました:
\s File: #{current_file}
\s すべてのページは index.html として生成される必要があります。
\s これにより /path/ と /path の両方でルーティングが可能になります。
ERROR_MESSAGE
)
end

# Check if it's a redirect or not by meta tags
def is_redirect_file?(filepath)
return false unless File.exist?(filepath)

content = File.read(filepath)
# Check for meta refresh redirect tag
content.include?('<meta http-equiv="refresh"') ||
content.include?("<meta http-equiv='refresh'") ||
content.include?('<meta http-equiv=refresh')
end
end
79 changes: 0 additions & 79 deletions test-directory-structure.py

This file was deleted.