|
| 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 |
0 commit comments