link_to with an absolute path that corresponds to a page path will get rewritten to the page's output URL
This commit is contained in:
parent
593cdb27aa
commit
05a769d762
|
@ -35,4 +35,21 @@ Feature: Directory Index
|
||||||
Scenario: Preview ignored file
|
Scenario: Preview ignored file
|
||||||
Given the Server is running at "indexable-app"
|
Given the Server is running at "indexable-app"
|
||||||
When I go to "/leave_me_alone/"
|
When I go to "/leave_me_alone/"
|
||||||
Then I should see "File Not Found"
|
Then I should see "File Not Found"
|
||||||
|
|
||||||
|
Scenario: Link_to knows about directory indexes
|
||||||
|
Given a fixture app "indexable-app"
|
||||||
|
And a file named "source/link_to.html.erb" with:
|
||||||
|
"""
|
||||||
|
link_to: <%= link_to "Needs Index", "/needs_index.html" %>
|
||||||
|
explicit_link_to: <%= link_to "Explicit", "/needs_index/index.html" %>
|
||||||
|
unknown_link_to: <%= link_to "Unknown", "/unknown.html" %>
|
||||||
|
relative_link_to: <%= link_to "Relative", "needs_index.html" %>
|
||||||
|
"""
|
||||||
|
And the Server is running at "indexable-app"
|
||||||
|
When I go to "/link_to/"
|
||||||
|
Then I should see 'link_to: <a href="/needs_index/">Needs Index</a>'
|
||||||
|
Then I should see 'explicit_link_to: <a href="/needs_index/index.html">Explicit</a>'
|
||||||
|
Then I should see 'unknown_link_to: <a href="/unknown.html">Unknown</a>'
|
||||||
|
# Relative links aren't touched
|
||||||
|
Then I should see 'relative_link_to: <a href="needs_index.html">Relative</a>'
|
|
@ -88,19 +88,31 @@ module Middleman::CoreExtensions::DefaultHelpers
|
||||||
# @param [String] source The path to the file
|
# @param [String] source The path to the file
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def asset_path(kind, source)
|
def asset_path(kind, source)
|
||||||
return source if source =~ /^http/
|
return source if source =~ /^http/
|
||||||
asset_folder = case kind
|
asset_folder = case kind
|
||||||
when :css then css_dir
|
when :css then css_dir
|
||||||
when :js then js_dir
|
when :js then js_dir
|
||||||
when :images then images_dir
|
when :images then images_dir
|
||||||
else kind.to_s
|
else kind.to_s
|
||||||
end
|
end
|
||||||
source = source.to_s.gsub(/\s/, '')
|
source = source.to_s.gsub(/\s/, '')
|
||||||
ignore_extension = (kind == :images) # don't append extension
|
ignore_extension = (kind == :images) # don't append extension
|
||||||
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
|
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
|
||||||
result_path = source if source =~ %r{^/} # absolute path
|
result_path = source if source =~ %r{^/} # absolute path
|
||||||
result_path ||= asset_url(source, asset_folder)
|
result_path ||= asset_url(source, asset_folder)
|
||||||
"#{result_path}"
|
"#{result_path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def link_to(*args, &block)
|
||||||
|
url_arg_index = block_given? ? 0 : 1
|
||||||
|
if url = args[url_arg_index]
|
||||||
|
# Only try to work with absolute URLs
|
||||||
|
if url.start_with? '/'
|
||||||
|
resource = sitemap.find_resource_by_path(url)
|
||||||
|
args[url_arg_index] = resource.url if resource
|
||||||
|
end
|
||||||
|
end
|
||||||
|
super(*args, &block)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue