mirror of https://github.com/freeCodeCamp/devdocs
This filter traverses all <a> tags and replaces its url for an url poiting to a path of an existant documentation.pull/1495/head
parent
e9d7849412
commit
38e2b107a2
@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Docs
|
||||
class ExternalUrlsFilter < Filter
|
||||
|
||||
def call
|
||||
if context[:external_urls]
|
||||
|
||||
root = path_to_root
|
||||
|
||||
css('a').each do |node|
|
||||
|
||||
next unless anchorUrl = node['href']
|
||||
|
||||
# avoid links already converted to internal links
|
||||
next if anchorUrl.match?(/\.\./)
|
||||
|
||||
if context[:external_urls].is_a?(Proc)
|
||||
node['href'] = context[:external_urls].call(anchorUrl)
|
||||
next
|
||||
end
|
||||
|
||||
url = URI(anchorUrl)
|
||||
|
||||
context[:external_urls].each do |host, name|
|
||||
if url.host.to_s.match?(host)
|
||||
node['href'] = root + name + url.path.to_s + '#' + url.fragment.to_s
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
doc
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in new issue