Skip to content

Commit 1d77754

Browse files
committed
Merge test-directory-structure test into HTML Proofer's custom checks
1 parent c554734 commit 1d77754

File tree

3 files changed

+57
-80
lines changed

3 files changed

+57
-80
lines changed

Rakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ task default: 'test'
33
# HTML Proofer helps to detect broken links.
44
# https://github.com/gjtorikian/html-proofer
55
require 'html-proofer'
6+
require_relative '_tests/custom_checks'
7+
68
task test: [:build] do
79
options = {
8-
checks: ['Links', 'Images', 'Scripts', 'OpenGraph', 'Favicon'],
10+
checks: ['Links', 'Images', 'Scripts', 'OpenGraph', 'Favicon', 'CustomChecks'],
911
allow_hash_href: false,
1012
disable_external: true,
1113
enforce_https: true,

_tests/custom_checks.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# See the following 'Custom Tests' section to add tests ;)
2+
# https://github.com/gjtorikian/html-proofer#custom-tests
3+
4+
require 'json'
5+
require 'yaml'
6+
7+
class CustomChecks < ::HTMLProofer::Check
8+
BASE_PATH = '_site'
9+
10+
def run
11+
current_filename = @runner.current_filename
12+
puts "\tchecking ... " + current_filename[5..].split('.').first
13+
14+
check_directory_structure
15+
end
16+
17+
# Check directory structure to ensure all pages are generated as index.html
18+
# This ensures proper routing for both /foobar/ and /foobar pathes.
19+
def check_directory_structure
20+
allowed_names = ['index.html', '404.html']
21+
current_file = @runner.current_filename
22+
23+
# Only check HTML files
24+
return unless current_file.end_with?('.html')
25+
26+
# Get the filename
27+
filename = File.basename(current_file)
28+
29+
# Skip if allowed filenames or redirect files
30+
return if allowed_names.include?(filename)
31+
return if is_redirect_file?(current_file)
32+
33+
# Report failure for non-index HTML files
34+
add_failure(
35+
<<~ERROR_MESSAGE
36+
index.html でない HTML ファイルが生成されました:
37+
\s File: #{current_file}
38+
\s すべてのページは index.html として生成される必要があります。
39+
\s これにより /path/ と /path の両方でルーティングが可能になります。
40+
ERROR_MESSAGE
41+
)
42+
end
43+
44+
# Check if it's a redirect or not by meta tags
45+
def is_redirect_file?(filepath)
46+
return false unless File.exist?(filepath)
47+
48+
content = File.read(filepath)
49+
# Check for meta refresh redirect tag
50+
content.include?('<meta http-equiv="refresh"') ||
51+
content.include?("<meta http-equiv='refresh'") ||
52+
content.include?('<meta http-equiv=refresh')
53+
end
54+
end

test-directory-structure.py

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)