Make link_to ignore (and preserve) query string and anchor in URLs/paths.
This commit is contained in:
parent
1f3650ccea
commit
377aa41a23
2 changed files with 22 additions and 4 deletions
|
@ -91,3 +91,16 @@ Feature: link_to helper
|
|||
When I go to "/link_to.html"
|
||||
Then I should see '<a href="/foo/needs_index.html">Needs Index</a>'
|
||||
|
||||
Scenario: link_to preserves query string and anchor and isn't messed up by them
|
||||
Given a fixture app "indexable-app"
|
||||
And a file named "source/link_to.html.erb" with:
|
||||
"""
|
||||
<%= link_to "Needs Index Anchor", "/needs_index.html#foo" %>
|
||||
<%= link_to "Needs Index Query", "/needs_index.html?foo" %>
|
||||
<%= link_to "Needs Index Query and Anchor", "/needs_index.html?foo#foo" %>
|
||||
"""
|
||||
And the Server is running at "indexable-app"
|
||||
When I go to "/link_to/"
|
||||
Then I should see '<a href="/needs_index/#foo">Needs Index Anchor</a>'
|
||||
Then I should see '<a href="/needs_index/?foo">Needs Index Query</a>'
|
||||
Then I should see '<a href="/needs_index/?foo#foo">Needs Index Query and Anchor</a>'
|
||||
|
|
|
@ -130,11 +130,14 @@ module Middleman
|
|||
# Handle relative urls
|
||||
current_source_dir = Pathname('/' + current_resource.path).dirname
|
||||
|
||||
path = Pathname(url)
|
||||
uri = URI(url)
|
||||
url_path = uri.path
|
||||
|
||||
url = current_source_dir.join(path).to_s if path.relative?
|
||||
path = Pathname(url_path)
|
||||
|
||||
resource = sitemap.find_resource_by_path(url)
|
||||
url_path = current_source_dir.join(path).to_s if path.relative?
|
||||
|
||||
resource = sitemap.find_resource_by_path(url_path)
|
||||
|
||||
# Allow people to turn on relative paths for all links with config[:relative_links] = true
|
||||
# but still override on a case by case basis with the :relative parameter.
|
||||
|
@ -159,7 +162,9 @@ module Middleman
|
|||
new_url = resource.url
|
||||
end
|
||||
|
||||
args[url_arg_index] = new_url
|
||||
uri.path = new_url
|
||||
|
||||
args[url_arg_index] = uri.to_s
|
||||
else
|
||||
raise "No resource exists at #{url}" if relative
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue