This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
html_check_hook.rb
51 lines (43 loc) · 1.66 KB
/
html_check_hook.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
# frozen_string_literal: true
# The hook runs html-proofer with options defined in the
# _config.checks.yml file
#
# For more details about html-proofer, refer to: https://github.com/gjtorikian/html-proofer
# For more details about Jekyll hooks, refer to: https://jekyllrb.com/docs/plugins/hooks/
#
require 'html-proofer'
require 'yaml'
require_relative '../lib/double_slash_check.rb'
Jekyll::Hooks.register :site, :post_write do |site|
# Do nothing unless 'site.check_links' is set
next unless site.config['check_links']
# Do not exit when html-proofer raises an error
begin
# Check 'ignore_urls' in '_config.checks.yml'
# and add 'excludes' from Jekyll configuration.
#
checks_config = YAML.load_file('_config.checks.yml')
ignore_urls = checks_config.dig('html-proofer', :ignore_urls)
jekyll_excludes = site.config['exclude']
jekyll_excludes_as_regex =
jekyll_excludes.map do |item|
Regexp.new Regexp.escape(item)
end
if ignore_urls
ignore_urls.push(jekyll_excludes_as_regex).flatten!.uniq!
else
checks_config['html-proofer'][:ignore_urls] = jekyll_excludes_as_regex
end
# Read configuration options for html-proofer
options = checks_config['html-proofer']
# Run html-proofer to check the jekyll destination directory
HTMLProofer.check_directory('_site', options).run
# Show the message when html-proofer fails.
# Expected that it fails when it finds broken links.
rescue StandardError => e
puts e
puts 'Fix the broken links before you push the changes to remote branch.'.blue
end
end