diff --git a/lib/docs/filters/core/internal_urls.rb b/lib/docs/filters/core/internal_urls.rb index d37435a5..54d27280 100644 --- a/lib/docs/filters/core/internal_urls.rb +++ b/lib/docs/filters/core/internal_urls.rb @@ -3,7 +3,7 @@ require 'set' module Docs class InternalUrlsFilter < Filter def call - internal_urls = Set.new + internal_urls = Set.new if follow_links? css('a').each do |link| next if skip_link?(link) @@ -17,13 +17,17 @@ module Docs normalize_internal_url(url, subpath) link['href'] = internal_path_to(url) - internal_urls << url.merge!(fragment: nil).to_s + internal_urls << url.merge!(fragment: nil).to_s if internal_urls end - result[:internal_urls] = internal_urls.to_a + result[:internal_urls] = (internal_urls || []).to_a doc end + def follow_links? + !(context[:follow_links] && context[:follow_links].call(self) == false) + end + def skip_link?(link) context[:skip_links] && context[:skip_links].call(link) end diff --git a/test/lib/docs/filters/core/internal_urls_test.rb b/test/lib/docs/filters/core/internal_urls_test.rb index d67b4ba6..637306be 100644 --- a/test/lib/docs/filters/core/internal_urls_test.rb +++ b/test/lib/docs/filters/core/internal_urls_test.rb @@ -159,6 +159,28 @@ class InternalUrlsFilterTest < MiniTest::Spec assert_equal 1, internal_urls.length end end + + context "when context[:follow_links] is a block" do + before do + @body = link_to context[:url] + end + + it "calls the block with the filter instance" do + context[:follow_links] = ->(arg) { @arg = arg; nil } + filter.call + assert_equal filter, @arg + end + + it "is empty when the block returns false" do + context[:follow_links] = ->(_) { false } + assert_empty internal_urls + end + + it "is the default when the block returns true" do + context[:follow_links] = ->(_) { true } + refute_empty internal_urls + end + end end context "when the base url is 'example.com'" do