Unified · Split
| @@ -3,7 +3,7 @@ require 'set' · 2 unmodified lines | |||
| 3 | module Docs | 3 | module Docs |
| 4 | class InternalUrlsFilter < Filter | 4 | class InternalUrlsFilter < Filter |
| 5 | def call | 5 | def call |
| 6 | internal_urls = Set.new | 6 | internal_urls = Set.new if follow_links? |
| 7 | 7 | ||
| 8 | css('a').each do |link| | 8 | css('a').each do |link| |
| 9 | next if skip_link?(link) | 9 | next if skip_link?(link) |
| @@ -17,13 +17,17 @@ module Docs · 7 unmodified lines | |||
| 17 | normalize_internal_url(url, subpath) | 17 | normalize_internal_url(url, subpath) |
| 18 | 18 | ||
| 19 | link['href'] = internal_path_to(url) | 19 | link['href'] = internal_path_to(url) |
| 20 | internal_urls << url.merge!(fragment: nil).to_s | 20 | internal_urls << url.merge!(fragment: nil).to_s if internal_urls |
| 21 | end | 21 | end |
| 22 | 22 | ||
| 23 | result[:internal_urls] = internal_urls.to_a | 23 | result[:internal_urls] = (internal_urls || []).to_a |
| 24 | doc | 24 | doc |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | def follow_links? | ||
| 28 | !(context[:follow_links] && context[:follow_links].call(self) == false) | ||
| 29 | end | ||
| 30 | |||
| 27 | def skip_link?(link) | 31 | def skip_link?(link) |
| 28 | context[:skip_links] && context[:skip_links].call(link) | 32 | context[:skip_links] && context[:skip_links].call(link) |
| 29 | end | 33 | end |
| @@ -159,6 +159,28 @@ class InternalUrlsFilterTest < MiniTest::Spec · 158 unmodified lines | |||
| 159 | assert_equal 1, internal_urls.length | 159 | assert_equal 1, internal_urls.length |
| 160 | end | 160 | end |
| 161 | end | 161 | end |
| 162 | |||
| 163 | context "when context[:follow_links] is a block" do | ||
| 164 | before do | ||
| 165 | @body = link_to context[:url] | ||
| 166 | end | ||
| 167 | |||
| 168 | it "calls the block with the filter instance" do | ||
| 169 | context[:follow_links] = ->(arg) { @arg = arg; nil } | ||
| 170 | filter.call | ||
| 171 | assert_equal filter, @arg | ||
| 172 | end | ||
| 173 | |||
| 174 | it "is empty when the block returns false" do | ||
| 175 | context[:follow_links] = ->(_) { false } | ||
| 176 | assert_empty internal_urls | ||
| 177 | end | ||
| 178 | |||
| 179 | it "is the default when the block returns true" do | ||
| 180 | context[:follow_links] = ->(_) { true } | ||
| 181 | refute_empty internal_urls | ||
| 182 | end | ||
| 183 | end | ||
| 162 | end | 184 | end |
| 163 | 185 | ||
| 164 | context "when the base url is 'example.com'" do | 186 | context "when the base url is 'example.com'" do |