1
0
Эх сурвалжийг харах

Add :skip_link option for ignoring certain links in scrapers

Thibaut 11 жил өмнө
parent
commit
c122caa7c4

+ 1 - 0
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?

+ 40 - 0
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