lib/nanoc/extra/checking/checks/internal_links.rb in nanoc-3.6.11 vs lib/nanoc/extra/checking/checks/internal_links.rb in nanoc-3.7.0
- old
+ new
@@ -7,10 +7,13 @@
# A check that verifies that all internal links point to a location that exists.
class InternalLinks < ::Nanoc::Extra::Checking::Check
# Starts the validator. The results will be printed to stdout.
#
+ # Internal links that match a regexp pattern in `@config[:checks][:internal_links][:exclude]` will
+ # be skipped.
+ #
# @return [void]
def run
# TODO de-duplicate this (duplicated in external links check)
filenames = output_filenames.select { |f| File.extname(f) == '.html' }
hrefs_with_filenames = ::Nanoc::Extra::LinkCollector.new(filenames, :internal).filenames_per_href
@@ -30,10 +33,13 @@
def valid?(href, origin)
# Skip hrefs that point to self
# FIXME this is ugly and won’t always be correct
return true if href == '.'
+ # Skip hrefs that are specified in the exclude configuration
+ return true if self.excluded?(href)
+
# Remove target
path = href.sub(/#.*$/, '')
return true if path.empty?
# Remove query string
@@ -56,9 +62,14 @@
# Check whether directory with index file exists
return true if File.directory?(path) && @site.config[:index_filenames].any? { |fn| File.file?(File.join(path, fn)) }
# Nope :(
false
+ end
+
+ def excluded?(href)
+ excludes = @site.config.fetch(:checks, {}).fetch(:internal_links, {}).fetch(:exclude, [])
+ excludes.any? { |pattern| Regexp.new(pattern).match(href) }
end
end
end