Skip to content

Commit

Permalink
Fix and improve external_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterEnoc committed Mar 19, 2021
1 parent 38e2b10 commit 843e2c7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
8 changes: 7 additions & 1 deletion assets/javascripts/lib/page.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,22 @@ onclick = (event) ->
link = $.eventTarget(event)
link = link.parentNode while link and link.tagName isnt 'A'

if link and not link.target and isSameOrigin(link.href)
if link and not link.target and isSameOrigin(link.href) and isSameOriginDifferentDoc(link)
event.preventDefault()
path = link.pathname + link.search + link.hash
path = path.replace /^\/\/+/, '/' # IE11 bug
page.show(path)

return

isSameOrigin = (url) ->
url.indexOf("#{location.protocol}//#{location.hostname}") is 0

isSameOriginDifferentDoc = (url) ->
console.log(url.pathname)
console.log(location.pathname)
url.pathname == location.pathname

updateCanonicalLink = ->
@canonicalLink ||= document.head.querySelector('link[rel="canonical"]')
@canonicalLink.setAttribute('href', "https://#{location.host}#{location.pathname}")
Expand Down
10 changes: 0 additions & 10 deletions lib/docs/core/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,5 @@ def clean_path(path)
path = path.gsub %r{\+}, '_plus_'
path
end

def path_to_root
if subpath == ''
return '../'
else
previous_dirs = subpath.scan(/\//)
return '../' * previous_dirs.length
end
end

end
end
4 changes: 1 addition & 3 deletions lib/docs/filters/core/external_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class ExternalUrlsFilter < Filter
def call
if context[:external_urls]

root = path_to_root

css('a').each do |node|

next unless anchorUrl = node['href']
Expand All @@ -24,7 +22,7 @@ def call

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
node['href'] = '/' + name + url.path.to_s + '#' + url.fragment.to_s
end
end

Expand Down

0 comments on commit 843e2c7

Please sign in to comment.