Merge pull request #530 from rumpuslabs/trailing_slash
Add trailing_slash option for prettier urls
This commit is contained in:
commit
9371eb2057
40
middleman-core/features/strip_url.feature
Normal file
40
middleman-core/features/strip_url.feature
Normal file
|
@ -0,0 +1,40 @@
|
|||
Feature: Strip the index_file from urls
|
||||
|
||||
Scenario: Default behaviour, strip with trailing slash
|
||||
Given the Server is running at "strip-url-app"
|
||||
When I go to "/"
|
||||
Then I should see "URL: '/'"
|
||||
When I go to "/index.html"
|
||||
Then I should see "URL: '/'"
|
||||
When I go to "/other.html"
|
||||
Then I should see "URL: '/other.html'"
|
||||
When I go to "/subdir/index.html"
|
||||
Then I should see "URL: '/subdir/'"
|
||||
|
||||
Scenario: Trailing slash off
|
||||
Given a fixture app "strip-url-app"
|
||||
And a file named "config.rb" with:
|
||||
"""
|
||||
set :trailing_slash, false
|
||||
"""
|
||||
And the Server is running
|
||||
When I go to "/"
|
||||
Then I should see "URL: '/'"
|
||||
When I go to "/other.html"
|
||||
Then I should see "URL: '/other.html'"
|
||||
When I go to "/subdir/index.html"
|
||||
Then I should see "URL: '/subdir'"
|
||||
|
||||
Scenario: Strip index off
|
||||
Given a fixture app "strip-url-app"
|
||||
And a file named "config.rb" with:
|
||||
"""
|
||||
set :strip_index_file, false
|
||||
"""
|
||||
And the Server is running
|
||||
When I go to "/"
|
||||
Then I should see "URL: '/index.html'"
|
||||
When I go to "/other.html"
|
||||
Then I should see "URL: '/other.html'"
|
||||
When I go to "/subdir/index.html"
|
||||
Then I should see "URL: '/subdir/index.html'"
|
0
middleman-core/fixtures/strip-url-app/config.rb
Normal file
0
middleman-core/fixtures/strip-url-app/config.rb
Normal file
1
middleman-core/fixtures/strip-url-app/source/index.html.erb
Executable file
1
middleman-core/fixtures/strip-url-app/source/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
|||
URL: '<%= current_page.url %>'
|
1
middleman-core/fixtures/strip-url-app/source/other.html.erb
Executable file
1
middleman-core/fixtures/strip-url-app/source/other.html.erb
Executable file
|
@ -0,0 +1 @@
|
|||
URL: '<%= current_page.url %>'
|
1
middleman-core/fixtures/strip-url-app/source/subdir/index.html.erb
Executable file
1
middleman-core/fixtures/strip-url-app/source/subdir/index.html.erb
Executable file
|
@ -0,0 +1 @@
|
|||
URL: '<%= current_page.url %>'
|
|
@ -93,6 +93,14 @@ module Middleman
|
|||
# @return [String]
|
||||
set :index_file, "index.html"
|
||||
|
||||
# Whether to strip the index file name off links to directory indexes
|
||||
# @return [Boolean]
|
||||
set :strip_index_file, true
|
||||
|
||||
# Whether to include a trailing slash when stripping the index file
|
||||
# @return [Boolean]
|
||||
set :trailing_slash, true
|
||||
|
||||
# Location of javascripts within source.
|
||||
# @return [String]
|
||||
set :js_dir, "javascripts"
|
||||
|
|
|
@ -141,8 +141,12 @@ module Middleman
|
|||
# just foo. Best for linking.
|
||||
# @return [String]
|
||||
def url
|
||||
File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/',
|
||||
destination_path.sub(/\/#{Regexp.escape(app.index_file)}$/, '/'))
|
||||
url_path = destination_path
|
||||
if app.strip_index_file
|
||||
url_path = url_path.sub(/(^|\/)#{Regexp.escape(app.index_file)}$/,
|
||||
app.trailing_slash ? '/' : '')
|
||||
end
|
||||
File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/', url_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -34,8 +34,8 @@ module Middleman
|
|||
|
||||
resources.each do |resource|
|
||||
# Check if it would be pointless to reroute
|
||||
next if resource.path == index_file ||
|
||||
resource.path.end_with?(new_index_path) ||
|
||||
next if resource.destination_path == index_file ||
|
||||
resource.destination_path.end_with?(new_index_path) ||
|
||||
File.extname(index_file) != resource.ext
|
||||
|
||||
# Check if frontmatter turns directory_index off
|
||||
|
|
Loading…
Reference in a new issue