Whole-post excerpts should match the post content

When a post does not contain an excerpt_separator, meaning the excerpt
includes the entire post, the excerpt should contain exactly the post
content.

This is desirable both from a correctness standpoint, that the excerpt
should not introduce any new content, and more practically to allow fast
and easy detection of whole-post excerpts in Liquid templates using
`post.excerpt == post.content`.  A common use-case is deciding whether
to render "Read More" links on a page containing post excerpts.

This commit does exactly that.  It avoids adding additional newlines to
the excerpt content when the excerpt includes the whole post and adds
tests to ensure that this behavior is correct and preserved going
forward.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
This commit is contained in:
Kevin Locke 2015-10-01 13:58:00 -07:00
parent 9f4d4bbae0
commit bb9462f12f
3 changed files with 25 additions and 2 deletions

View File

@ -47,4 +47,4 @@ Feature: Post excerpts
And the _site/2007/12/31 directory should exist
And the "_site/2007/12/31/entry1.html" file should exist
And I should see "<p>content for entry1.</p>" in "_site/index.html"
And I should see "<html><head></head><body><p>content for entry1.</p>\n\n</body></html>" in "_site/2007/12/31/entry1.html"
And I should see "<html><head></head><body><p>content for entry1.</p>\n</body></html>" in "_site/2007/12/31/entry1.html"

View File

@ -107,7 +107,11 @@ module Jekyll
def extract_excerpt(post_content)
head, _, tail = post_content.to_s.partition(post.excerpt_separator)
"" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n")
if tail.empty?
head
else
"" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n")
end
end
end
end

View File

@ -124,4 +124,23 @@ class TestExcerpt < JekyllUnitTest
end
end
end
context "A whole-post excerpt" do
setup do
clear_dest
@site = Site.new(site_configuration)
@post = setup_post("2008-02-02-published.markdown")
@excerpt = @post.send :extract_excerpt
end
should "be generated" do
assert_equal true, @excerpt.is_a?(Jekyll::Excerpt)
end
context "#content" do
should "match the post content" do
assert_equal @post.content, @excerpt.content
end
end
end
end