Skip to content

Commit

Permalink
Merge pull request #847 from nanoc/ilinks-exclude-source
Browse files Browse the repository at this point in the history
Add exclude_origins option to internal links checker
  • Loading branch information
denisdefreyne committed Apr 9, 2016
2 parents ed8eac5 + c96d085 commit d841775
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/nanoc/extra/checking/checks/internal_links.rb
Expand Up @@ -34,7 +34,7 @@ def valid?(href, origin)
return true if href == '.'

# Skip hrefs that are specified in the exclude configuration
return true if excluded?(href)
return true if excluded?(href, origin)

# Remove target
path = href.sub(/#.*$/, '')
Expand Down Expand Up @@ -65,9 +65,20 @@ def valid?(href, origin)
false
end

def excluded?(href)
excludes = @config.fetch(:checks, {}).fetch(:internal_links, {}).fetch(:exclude, [])
def excluded?(href, origin)
config = @config.fetch(:checks, {}).fetch(:internal_links, {})
excluded_target?(href, config) || excluded_origin?(origin, config)
end

def excluded_target?(href, config)
excludes = config.fetch(:exclude_targets, config.fetch(:exclude, []))
excludes.any? { |pattern| Regexp.new(pattern).match(href) }
end

def excluded_origin?(origin, config)
relative_origin = origin[@config[:output_dir].size..-1]
excludes = config.fetch(:exclude_origins, [])
excludes.any? { |pattern| Regexp.new(pattern).match(relative_origin) }
end
end
end
25 changes: 25 additions & 0 deletions test/extra/checking/checks/test_internal_links.rb
Expand Up @@ -63,6 +63,31 @@ def test_exclude
end
end

def test_exclude_targets
with_site do |site|
# Create check
check = Nanoc::Extra::Checking::Checks::InternalLinks.create(site)
site.config.update({ checks: { internal_links: { exclude_targets: ['^/excluded\d+'] } } })

# Test
assert check.send(:valid?, '/excluded1', 'output/origin')
assert check.send(:valid?, '/excluded2', 'output/origin')
assert !check.send(:valid?, '/excluded_not', 'output/origin')
end
end

def test_exclude_origins
with_site do |site|
# Create check
check = Nanoc::Extra::Checking::Checks::InternalLinks.create(site)
site.config.update({ checks: { internal_links: { exclude_origins: ['^/excluded'] } } })

# Test
assert check.send(:valid?, '/foo', 'output/excluded')
assert !check.send(:valid?, '/foo', 'output/not_excluded')
end
end

def test_unescape_url
with_site do |site|
FileUtils.mkdir_p('output/stuff')
Expand Down

0 comments on commit d841775

Please sign in to comment.