mirror of
https://github.com/jekyll/jekyll.git
synced 2026-01-31 01:38:24 -05:00
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>
147 lines
4.6 KiB
Ruby
147 lines
4.6 KiB
Ruby
require 'helper'
|
|
|
|
class TestExcerpt < JekyllUnitTest
|
|
def setup_post(file)
|
|
Post.new(@site, source_dir, '', file)
|
|
end
|
|
|
|
def do_render(post)
|
|
layouts = { "default" => Layout.new(@site, source_dir('_layouts'), "simple.html")}
|
|
post.render(layouts, {"site" => {"posts" => []}})
|
|
end
|
|
|
|
context "With extraction disabled" do
|
|
setup do
|
|
clear_dest
|
|
@site = Site.new(site_configuration('excerpt_separator' => ''))
|
|
@post = setup_post("2013-07-22-post-excerpt-with-layout.markdown")
|
|
end
|
|
|
|
should "not be generated" do
|
|
excerpt = @post.send(:extract_excerpt)
|
|
assert_equal true, excerpt.empty?
|
|
end
|
|
end
|
|
|
|
context "An extracted excerpt" do
|
|
setup do
|
|
clear_dest
|
|
@site = Site.new(site_configuration)
|
|
@post = setup_post("2013-07-22-post-excerpt-with-layout.markdown")
|
|
@excerpt = @post.send :extract_excerpt
|
|
end
|
|
|
|
context "#include(string)" do
|
|
|
|
setup do
|
|
@excerpt.output = "Here is a fake output stub"
|
|
end
|
|
|
|
should "return true only if an excerpt output contains a specified string" do
|
|
assert @excerpt.include?("fake output")
|
|
refute @excerpt.include?("real output")
|
|
end
|
|
end
|
|
|
|
context "#id" do
|
|
should "contain the UID for the post" do
|
|
assert_equal @excerpt.id, "#{@post.id}/#excerpt"
|
|
end
|
|
should "return a string" do
|
|
assert_same @post.id.class, String
|
|
end
|
|
end
|
|
|
|
context "#to_s" do
|
|
should "return its content if no output present" do
|
|
assert_equal @excerpt.content, @excerpt.to_s
|
|
end
|
|
|
|
should "return its output if output present" do
|
|
@excerpt.output = "Fake Output"
|
|
assert_equal @excerpt.output, @excerpt.to_s
|
|
end
|
|
end
|
|
|
|
context "#inspect" do
|
|
should "contain the excerpt id as a shorthand string identifier" do
|
|
assert_equal @excerpt.inspect, "<Excerpt: #{@excerpt.id}>"
|
|
end
|
|
|
|
should "return a string" do
|
|
assert_same @post.id.class, String
|
|
end
|
|
end
|
|
|
|
context "#to_liquid" do
|
|
should "contain the proper page data to mimick the post liquid" do
|
|
assert_equal "Post Excerpt with Layout", @excerpt.to_liquid["title"]
|
|
assert_equal "/bar/baz/z_category/mixedcase/2013/07/22/post-excerpt-with-layout.html", @excerpt.to_liquid["url"]
|
|
assert_equal Time.parse("2013-07-22"), @excerpt.to_liquid["date"]
|
|
assert_equal %w[bar baz z_category MixedCase], @excerpt.to_liquid["categories"]
|
|
assert_equal %w[first second third jekyllrb.com], @excerpt.to_liquid["tags"]
|
|
assert_equal "_posts/2013-07-22-post-excerpt-with-layout.markdown", @excerpt.to_liquid["path"]
|
|
end
|
|
|
|
should "consider inheritance" do
|
|
klass = Class.new(Jekyll::Post)
|
|
assert_gets_called = false
|
|
klass.send(:define_method, :assert_gets_called) { assert_gets_called = true }
|
|
klass.const_set(:EXCERPT_ATTRIBUTES_FOR_LIQUID, Jekyll::Post::EXCERPT_ATTRIBUTES_FOR_LIQUID + ['assert_gets_called'])
|
|
post = klass.new(@site, source_dir, '', "2008-02-02-published.markdown")
|
|
Jekyll::Excerpt.new(post).to_liquid
|
|
|
|
assert assert_gets_called, 'assert_gets_called did not get called on post.'
|
|
end
|
|
end
|
|
|
|
context "#content" do
|
|
|
|
context "before render" do
|
|
should "be the first paragraph of the page" do
|
|
assert_equal "First paragraph with [link ref][link].\n\n[link]: http://www.jekyllrb.com/", @excerpt.content
|
|
end
|
|
|
|
should "contain any refs at the bottom of the page" do
|
|
assert @excerpt.content.include?("[link]: http://www.jekyllrb.com/")
|
|
end
|
|
end
|
|
|
|
context "after render" do
|
|
setup do
|
|
@rendered_post = @post.dup
|
|
do_render(@rendered_post)
|
|
@extracted_excerpt = @rendered_post.send :extracted_excerpt
|
|
end
|
|
|
|
should "be the first paragraph of the page" do
|
|
assert_equal "<p>First paragraph with <a href=\"http://www.jekyllrb.com/\">link ref</a>.</p>\n\n", @extracted_excerpt.content
|
|
end
|
|
|
|
should "link properly" do
|
|
assert @extracted_excerpt.content.include?("http://www.jekyllrb.com/")
|
|
end
|
|
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
|