Use a page_by_destination lookup to make rerouting work both ways, in build and during preview server.
This commit is contained in:
parent
e27e0cdd44
commit
e136fab77c
5 changed files with 22 additions and 58 deletions
|
@ -15,5 +15,5 @@ Feature: Wildcards in Page helper
|
|||
Then I should see "Normal Layout"
|
||||
When I go to "/admin/"
|
||||
Then I should see "Admin Layout"
|
||||
When I go to "/admin/page.html"
|
||||
When I go to "/admin/page/"
|
||||
Then I should see "Admin Layout"
|
|
@ -370,13 +370,13 @@ class Middleman::Base
|
|||
|
||||
# Run before callbacks
|
||||
run_hook :before
|
||||
|
||||
# Return 404 if not in sitemap
|
||||
return not_found unless sitemap.exists?(@request_path)
|
||||
|
||||
|
||||
# Get the page object for this path
|
||||
sitemap_page = sitemap.page(@request_path)
|
||||
|
||||
sitemap_page = sitemap.page_by_destination(@request_path)
|
||||
|
||||
# Return 404 if not in sitemap
|
||||
return not_found unless sitemap_page
|
||||
|
||||
# Return 404 if this path is specifically ignored
|
||||
return not_found if sitemap_page.ignored?
|
||||
|
||||
|
|
|
@ -12,52 +12,8 @@ module Middleman::Extensions
|
|||
# Include methods
|
||||
app.send :include, InstanceMethods
|
||||
|
||||
# TODO: unify these by replacing the "before" thing with a
|
||||
# lookup by destination_path
|
||||
|
||||
# Before requests
|
||||
app.before do
|
||||
prefix = @original_path.sub(/\/$/, "")
|
||||
indexed_path = prefix + "/" + index_file
|
||||
extensioned_path = prefix + File.extname(index_file)
|
||||
|
||||
is_ignored = false
|
||||
fm_ignored = false
|
||||
|
||||
# If the sitemap knows about the path
|
||||
if sitemap.exists?(@original_path)
|
||||
# Inspect frontmatter
|
||||
d = sitemap.page(@original_path).data
|
||||
|
||||
# Allow the frontmatter to ignore a directory index
|
||||
if !d.nil? && d.has_key?("directory_index") && d["directory_index"] == false
|
||||
fm_ignored = true
|
||||
else
|
||||
next
|
||||
end
|
||||
else
|
||||
# Otherwise check this extension for list of ignored indexes
|
||||
if sitemap.exists?(extensioned_path)
|
||||
is_ignored = ignored_directory_indexes.include?(sitemap.page(extensioned_path))
|
||||
end
|
||||
end
|
||||
|
||||
# If we're going to remap to a directory index
|
||||
if !sitemap.exists?(indexed_path) && !is_ignored && !fm_ignored
|
||||
parts = @original_path.split("/")
|
||||
last_part = parts.last || ''
|
||||
last_part_ext = File.extname(last_part)
|
||||
|
||||
# Change the request
|
||||
if last_part_ext.blank?
|
||||
# This is a folder, redirect to index
|
||||
@request_path = extensioned_path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
app.after_configuration do
|
||||
# Basically does the same as above, but in build mode
|
||||
# Register a reroute transform that turns regular paths into indexed paths
|
||||
sitemap.reroute do |destination, page|
|
||||
new_index_path = "/#{index_file}"
|
||||
frontmatter_ignore = false
|
||||
|
@ -71,14 +27,14 @@ module Middleman::Extensions
|
|||
index_ext = File.extname(index_file)
|
||||
|
||||
# Only reroute if not ignored
|
||||
request_path = page.request_path
|
||||
path = page.path
|
||||
if ignored_directory_indexes.include? page
|
||||
destination
|
||||
elsif request_path == index_file || request_path.end_with?(new_index_path)
|
||||
elsif path == index_file || path.end_with?(new_index_path)
|
||||
destination
|
||||
elsif frontmatter_ignore
|
||||
destination
|
||||
elsif index_ext != File.extname(request_path)
|
||||
elsif index_ext != File.extname(path)
|
||||
destination
|
||||
else
|
||||
destination.chomp(File.extname(index_file)) + new_index_path
|
||||
|
|
|
@ -43,9 +43,9 @@ module Middleman::Sitemap
|
|||
# @return [String]
|
||||
def request_path
|
||||
if proxy?
|
||||
store.page(proxied_to).path
|
||||
store.page(proxied_to).destination_path
|
||||
else
|
||||
path
|
||||
destination_path
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -192,6 +192,7 @@ module Middleman::Sitemap
|
|||
# This path can be affected by proxy callbacks.
|
||||
# @return [String]
|
||||
def destination_path
|
||||
# TODO: memoize this value
|
||||
store.reroute_callbacks.inject(self.path) do |destination, callback|
|
||||
callback.call(destination, self)
|
||||
end
|
||||
|
|
|
@ -77,6 +77,13 @@ module Middleman::Sitemap
|
|||
path = normalize_path(path)
|
||||
@pages.fetch(path) { @pages[path] = ::Middleman::Sitemap::Page.new(self, path) }
|
||||
end
|
||||
|
||||
# Find a page given its destination path
|
||||
def page_by_destination(destination_path)
|
||||
# TODO: memoize this
|
||||
destination_path = normalize_path(destination_path)
|
||||
@pages.values.find {|p| p.destination_path == destination_path }
|
||||
end
|
||||
|
||||
# Loop over known pages
|
||||
# @yield [path, page]
|
||||
|
@ -86,7 +93,7 @@ module Middleman::Sitemap
|
|||
yield k, v
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Get all known paths
|
||||
# @return [Array<String>]
|
||||
def all_paths
|
||||
|
|
Loading…
Reference in a new issue