Merge pull request #3538 from willnorris/permalink

Improved permalinks for pages and collections
This commit is contained in:
Will Norris 2015-03-04 15:18:43 -08:00
commit f6f2626828
6 changed files with 140 additions and 12 deletions

View File

@ -170,7 +170,9 @@ module Jekyll
# #
# Returns the URL template to render collection's documents at. # Returns the URL template to render collection's documents at.
def url_template def url_template
metadata.fetch('permalink', "/:collection/:path:output_ext") metadata.fetch('permalink') do
Utils.add_permalink_suffix("/:collection/:path", site.permalink_style)
end
end end
# Extract options for this collection from the site configuration. # Extract options for this collection from the site configuration.

View File

@ -63,16 +63,12 @@ module Jekyll
# #
# Returns the template String. # Returns the template String.
def template def template
if site.permalink_style == :pretty if !html?
if index? && html?
"/:path/"
elsif html?
"/:path/:basename/"
else
"/:path/:basename:output_ext"
end
else
"/:path/:basename:output_ext" "/:path/:basename:output_ext"
elsif index?
"/:path/"
else
Utils.add_permalink_suffix("/:path/:basename", site.permalink_style)
end end
end end

View File

@ -158,5 +158,45 @@ module Jekyll
downcase downcase
end end
# Add an appropriate suffix to template so that it matches the specified
# permalink style.
#
# template - permalink template without trailing slash or file extension
# permalink_style - permalink style, either built-in or custom
#
# The returned permalink template will use the same ending style as
# specified in permalink_style. For example, if permalink_style contains a
# trailing slash (or is :pretty, which indirectly has a trailing slash),
# then so will the returned template. If permalink_style has a trailing
# ":output_ext" (or is :none, :date, or :ordinal) then so will the returned
# template. Otherwise, template will be returned without modification.
#
# Examples:
# add_permalink_suffix("/:basename", :pretty)
# # => "/:basename/"
#
# add_permalink_suffix("/:basename", :date)
# # => "/:basename:output_ext"
#
# add_permalink_suffix("/:basename", "/:year/:month/:title/")
# # => "/:basename/"
#
# add_permalink_suffix("/:basename", "/:year/:month/:title")
# # => "/:basename"
#
# Returns the updated permalink template
def add_permalink_suffix(template, permalink_style)
case permalink_style
when :pretty
template << "/"
when :date, :ordinal, :none
template << ":output_ext"
else
template << "/" if permalink_style.to_s.end_with?("/")
template << ":output_ext" if permalink_style.to_s.end_with?(":output_ext")
end
template
end
end end
end end

View File

@ -219,7 +219,31 @@ class TestDocument < JekyllUnitTest
assert_equal "/slides/test/example-slide-1", @document.url assert_equal "/slides/test/example-slide-1", @document.url
end end
should "produce the right destination" do should "produce the right destination file" do
assert_equal @dest_file, @document.destination(dest_dir)
end
end
context "a document in a collection with pretty permalink style" do
setup do
@site = fixture_site({
"collections" => {
"slides" => {
"output" => true,
}
},
})
@site.permalink_style = :pretty
@site.process
@document = @site.collections["slides"].docs[0]
@dest_file = dest_dir("slides/example-slide-1/index.html")
end
should "produce the right URL" do
assert_equal "/slides/example-slide-1/", @document.url
end
should "produce the right destination file" do
assert_equal @dest_file, @document.destination(dest_dir) assert_equal @dest_file, @document.destination(dest_dir)
end end
end end

View File

@ -46,7 +46,7 @@ class TestPage < JekyllUnitTest
should "create index url based on filename" do should "create index url based on filename" do
@page = setup_page('/contacts', 'index.html') @page = setup_page('/contacts', 'index.html')
assert_equal "/contacts/index.html", @page.url assert_equal "/contacts/", @page.url
end end
end end
@ -129,6 +129,58 @@ class TestPage < JekyllUnitTest
end end
end end
context "with date permalink style" do
setup do
@site.permalink_style = :date
end
should "return url and destination correctly" do
@page = setup_page('contacts.html')
@dest_file = dest_dir("contacts.html")
assert_equal '/contacts.html', @page.url
assert_equal @dest_file, @page.destination(dest_dir)
end
end
context "with custom permalink style with trailing slash" do
setup do
@site.permalink_style = "/:title/"
end
should "return url and destination correctly" do
@page = setup_page('contacts.html')
@dest_file = dest_dir("contacts/index.html")
assert_equal '/contacts/', @page.url
assert_equal @dest_file, @page.destination(dest_dir)
end
end
context "with custom permalink style with file extension" do
setup do
@site.permalink_style = "/:title:output_ext"
end
should "return url and destination correctly" do
@page = setup_page('contacts.html')
@dest_file = dest_dir("contacts.html")
assert_equal '/contacts.html', @page.url
assert_equal @dest_file, @page.destination(dest_dir)
end
end
context "with custom permalink style with no extension" do
setup do
@site.permalink_style = "/:title"
end
should "return url and destination correctly" do
@page = setup_page('contacts.html')
@dest_file = dest_dir("contacts.html")
assert_equal '/contacts', @page.url
assert_equal @dest_file, @page.destination(dest_dir)
end
end
context "with any other permalink style" do context "with any other permalink style" do
should "return dir correctly" do should "return dir correctly" do
@site.permalink_style = nil @site.permalink_style = nil

View File

@ -166,4 +166,18 @@ class TestUtils < JekyllUnitTest
end end
end end
context "The \`Utils.add_permalink_suffix\` method" do
should "handle built-in permalink styles" do
assert_equal "/:basename/", Utils.add_permalink_suffix("/:basename", :pretty)
assert_equal "/:basename:output_ext", Utils.add_permalink_suffix("/:basename", :date)
assert_equal "/:basename:output_ext", Utils.add_permalink_suffix("/:basename", :ordinal)
assert_equal "/:basename:output_ext", Utils.add_permalink_suffix("/:basename", :none)
end
should "handle custom permalink styles" do
assert_equal "/:basename/", Utils.add_permalink_suffix("/:basename", "/:title/")
assert_equal "/:basename:output_ext", Utils.add_permalink_suffix("/:basename", "/:title:output_ext")
assert_equal "/:basename", Utils.add_permalink_suffix("/:basename", "/:title")
end
end
end end