Make page excerpts consistent with doc excerpts (#8236)

Merge pull request 8236
This commit is contained in:
Ashwin Maroli 2020-06-17 18:56:53 +05:30 committed by GitHub
parent c78ecc8d82
commit ba29de02d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 27 deletions

View File

@ -49,6 +49,7 @@ module Jekyll
process(name)
read_yaml(PathManager.join(base, dir), name)
generate_excerpt if site.config["page_excerpts"]
data.default_proc = proc do |_, key|
site.frontmatter_defaults.find(relative_path, type, key)
@ -185,14 +186,25 @@ module Jekyll
end
def excerpt_separator
@excerpt_separator ||= data["excerpt_separator"] || site.config["excerpt_separator"] || ""
@excerpt_separator ||= (data["excerpt_separator"] || site.config["excerpt_separator"]).to_s
end
def excerpt
return if excerpt_separator.empty? || !site.config["page_excerpts"]
return data["excerpt"] unless self.class == Jekyll::Page && html?
return @excerpt if defined?(@excerpt)
data["excerpt"] ||= Jekyll::PageExcerpt.new(self).to_liquid
@excerpt = data["excerpt"]&.to_s
end
def generate_excerpt?
!excerpt_separator.empty? && self.class == Jekyll::Page && html?
end
private
def generate_excerpt
return unless generate_excerpt?
data["excerpt"] ||= Jekyll::PageExcerpt.new(self)
end
end
end

View File

@ -2,15 +2,14 @@
module Jekyll
class PageExcerpt < Excerpt
attr_reader :output, :doc
attr_reader :doc
alias_method :id, :relative_path
# The Liquid representation of this instance is simply the rendered output string.
alias_method :to_liquid, :output
EXCERPT_ATTRIBUTES = (Page::ATTRIBUTES_FOR_LIQUID - %w(excerpt)).freeze
private_constant :EXCERPT_ATTRIBUTES
def initialize(doc)
super
self.output = Renderer.new(site, self, site.site_payload).run
def to_liquid
@to_liquid ||= doc.to_liquid(EXCERPT_ATTRIBUTES)
end
def render_with_liquid?

View File

@ -0,0 +1,7 @@
---
title: Contact Information
---
## {{ page.title }}
In case of emergency, contact Mr. John Doe, 1234, Foo Road, Foo.

View File

@ -816,7 +816,7 @@ class TestFilters < JekyllUnitTest
"The list of grouped items for '' is not an Array."
)
# adjust array.size to ignore symlinked page in Windows
qty = Utils::Platforms.really_windows? ? 18 : 20
qty = Utils::Platforms.really_windows? ? 19 : 21
assert_equal qty, g["items"].size
end
end
@ -1310,7 +1310,7 @@ class TestFilters < JekyllUnitTest
"The list of grouped items for '' is not an Array."
)
# adjust array.size to ignore symlinked page in Windows
qty = Utils::Platforms.really_windows? ? 18 : 20
qty = Utils::Platforms.really_windows? ? 19 : 21
assert_equal qty, g["items"].size
end
end

View File

@ -369,24 +369,41 @@ class TestPage < JekyllUnitTest
end
context "read-in by default" do
should "not expose an excerpt to Liquid templates" do
should "not initialize excerpts by default" do
page = setup_page("contacts", "foo.md")
assert_nil page.excerpt
end
should "not expose an excerpt to Liquid templates by default" do
page = setup_page("/contacts", "bar.html")
assert_nil page.to_liquid["excerpt"]
end
should "expose an excerpt to Liquid templates if site is configured to" do
configured_site = fixture_site("page_excerpts" => true)
test_page = Jekyll::Page.new(configured_site, source_dir, "/contacts", "bar.html")
context "in a site configured to generate page excerpts" do
setup { @configured_site = fixture_site("page_excerpts" => true) }
should "initialize excerpt eagerly but render only when needed" do
test_page = Jekyll::Page.new(@configured_site, source_dir, "contacts", "foo.md")
assert_equal Jekyll::PageExcerpt, test_page.data["excerpt"].class
assert_equal String, test_page.excerpt.class
assert_equal(
"<h2 id=\"contact-information\">Contact Information</h2>\n",
test_page.excerpt
)
end
should "expose an excerpt to Liquid templates" do
test_page = Jekyll::Page.new(@configured_site, source_dir, "/contacts", "bar.html")
assert_equal "Contact Information\n", test_page.to_liquid["excerpt"]
end
should "not expose an excerpt for non-html pages even in a configured site" do
configured_site = fixture_site("page_excerpts" => true)
test_page = Jekyll::Page.new(configured_site, source_dir, "assets", "test-styles.scss")
should "not expose an excerpt for non-html pages" do
test_page = Jekyll::Page.new(@configured_site, source_dir, "assets", "test-styles.scss")
refute_equal ".half { width: 50%; }\n", test_page.to_liquid["excerpt"]
assert_nil test_page.to_liquid["excerpt"]
end
end
end
context "generated via plugin" do
setup do

View File

@ -232,6 +232,7 @@ class TestSite < JekyllUnitTest
environment.html
exploit.md
foo.md
foo.md
humans.txt
index.html
index.html