Add :follow_links option to not follow links on select pages

pull/22/head
Thibaut 11 years ago
parent 6ccfb396e3
commit 6c8eea1adb

@ -3,7 +3,7 @@ require 'set'
module Docs module Docs
class InternalUrlsFilter < Filter class InternalUrlsFilter < Filter
def call def call
internal_urls = Set.new internal_urls = Set.new if follow_links?
css('a').each do |link| css('a').each do |link|
next if skip_link?(link) next if skip_link?(link)
@ -17,13 +17,17 @@ module Docs
normalize_internal_url(url, subpath) normalize_internal_url(url, subpath)
link['href'] = internal_path_to(url) 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 end
result[:internal_urls] = internal_urls.to_a result[:internal_urls] = (internal_urls || []).to_a
doc doc
end end
def follow_links?
!(context[:follow_links] && context[:follow_links].call(self) == false)
end
def skip_link?(link) def skip_link?(link)
context[:skip_links] && context[:skip_links].call(link) context[:skip_links] && context[:skip_links].call(link)
end end

@ -159,6 +159,28 @@ class InternalUrlsFilterTest < MiniTest::Spec
assert_equal 1, internal_urls.length assert_equal 1, internal_urls.length
end end
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 end
context "when the base url is 'example.com'" do context "when the base url is 'example.com'" do

Loading…
Cancel
Save