mirror of
https://github.com/freeCodeCamp/devdocs
synced 2024-11-16 19:48:10 +01:00
Add :skip_link option for ignoring certain links in scrapers
This commit is contained in:
parent
f4b736eb9c
commit
c122caa7c4
2 changed files with 41 additions and 0 deletions
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue