From c122caa7c4b8d24dfe7142362186b7615b3249e2 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sun, 7 Sep 2014 10:30:50 -0400 Subject: [PATCH] Add :skip_link option for ignoring certain links in scrapers --- lib/docs/filters/core/internal_urls.rb | 1 + .../docs/filters/core/internal_urls_test.rb | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/docs/filters/core/internal_urls.rb b/lib/docs/filters/core/internal_urls.rb index 23a27f40..94439325 100644 --- a/lib/docs/filters/core/internal_urls.rb +++ b/lib/docs/filters/core/internal_urls.rb @@ -9,6 +9,7 @@ module Docs def update_links css('a').each do |link| + next if context[:skip_link].is_a?(Proc) && context[:skip_link].call(link) next unless url = to_internal_url(link['href']) link['href'] = internal_path_to(url) yield url if block_given? diff --git a/test/lib/docs/filters/core/internal_urls_test.rb b/test/lib/docs/filters/core/internal_urls_test.rb index 1ca7bc9c..f099f624 100644 --- a/test/lib/docs/filters/core/internal_urls_test.rb +++ b/test/lib/docs/filters/core/internal_urls_test.rb @@ -366,4 +366,44 @@ class InternalUrlsFilterTest < MiniTest::Spec end end end + + context "context[:skip_link] is a block" do + before do + @body = link_to context[:url] + end + + it "calls the block with each link" do + context[:skip_link] = ->(arg) { @arg = arg.try(:to_html); nil } + filter.call + assert_equal @body, @arg + end + + context "and the block returns true" do + before do + context[:skip_link] = ->(_) { true } + end + + it "doesn't include the link's url in :internal_urls" do + assert internal_urls.empty? + end + + it "doesn't replace the link's url" do + assert_equal @body, filter_output_string + end + end + + context "and the block returns false" do + before do + context[:skip_link] = ->(_) { false } + end + + it "includes the link's url in :internal_urls" do + refute internal_urls.empty? + end + + it "replaces the link's url" do + refute_equal @body, filter_output_string + end + end + end end