From bb9462f12fd9d68a86017c6f174b3dd1d817d6e3 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Thu, 1 Oct 2015 13:58:00 -0700 Subject: [PATCH] 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 --- features/post_excerpts.feature | 2 +- lib/jekyll/excerpt.rb | 6 +++++- test/test_excerpt.rb | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/features/post_excerpts.feature b/features/post_excerpts.feature index e9d048bd..c66c527b 100644 --- a/features/post_excerpts.feature +++ b/features/post_excerpts.feature @@ -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 "

content for entry1.

" in "_site/index.html" - And I should see "

content for entry1.

\n\n" in "_site/2007/12/31/entry1.html" + And I should see "

content for entry1.

\n" in "_site/2007/12/31/entry1.html" diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index 347be217..3d463944 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -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 diff --git a/test/test_excerpt.rb b/test/test_excerpt.rb index ecd42148..a5025cda 100644 --- a/test/test_excerpt.rb +++ b/test/test_excerpt.rb @@ -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