Make page excerpts consistent with doc excerpts (#8236)
Merge pull request 8236
This commit is contained in:
parent
c78ecc8d82
commit
ba29de02d4
|
@ -49,6 +49,7 @@ module Jekyll
|
||||||
|
|
||||||
process(name)
|
process(name)
|
||||||
read_yaml(PathManager.join(base, dir), name)
|
read_yaml(PathManager.join(base, dir), name)
|
||||||
|
generate_excerpt if site.config["page_excerpts"]
|
||||||
|
|
||||||
data.default_proc = proc do |_, key|
|
data.default_proc = proc do |_, key|
|
||||||
site.frontmatter_defaults.find(relative_path, type, key)
|
site.frontmatter_defaults.find(relative_path, type, key)
|
||||||
|
@ -185,14 +186,25 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def excerpt_separator
|
def excerpt_separator
|
||||||
@excerpt_separator ||= data["excerpt_separator"] || site.config["excerpt_separator"] || ""
|
@excerpt_separator ||= (data["excerpt_separator"] || site.config["excerpt_separator"]).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def excerpt
|
def excerpt
|
||||||
return if excerpt_separator.empty? || !site.config["page_excerpts"]
|
return @excerpt if defined?(@excerpt)
|
||||||
return data["excerpt"] unless self.class == Jekyll::Page && html?
|
|
||||||
|
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,15 +2,14 @@
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class PageExcerpt < Excerpt
|
class PageExcerpt < Excerpt
|
||||||
attr_reader :output, :doc
|
attr_reader :doc
|
||||||
alias_method :id, :relative_path
|
alias_method :id, :relative_path
|
||||||
|
|
||||||
# The Liquid representation of this instance is simply the rendered output string.
|
EXCERPT_ATTRIBUTES = (Page::ATTRIBUTES_FOR_LIQUID - %w(excerpt)).freeze
|
||||||
alias_method :to_liquid, :output
|
private_constant :EXCERPT_ATTRIBUTES
|
||||||
|
|
||||||
def initialize(doc)
|
def to_liquid
|
||||||
super
|
@to_liquid ||= doc.to_liquid(EXCERPT_ATTRIBUTES)
|
||||||
self.output = Renderer.new(site, self, site.site_payload).run
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_with_liquid?
|
def render_with_liquid?
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
---
|
---
|
||||||
---
|
---
|
||||||
|
|
||||||
@import "{{ site.skin | default: 'grid' }}";
|
@import "{{ site.skin | default: 'grid' }}";
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
title: Contact Information
|
||||||
|
---
|
||||||
|
|
||||||
|
## {{ page.title }}
|
||||||
|
|
||||||
|
In case of emergency, contact Mr. John Doe, 1234, Foo Road, Foo.
|
|
@ -816,7 +816,7 @@ class TestFilters < JekyllUnitTest
|
||||||
"The list of grouped items for '' is not an Array."
|
"The list of grouped items for '' is not an Array."
|
||||||
)
|
)
|
||||||
# adjust array.size to ignore symlinked page in Windows
|
# 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
|
assert_equal qty, g["items"].size
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1310,7 +1310,7 @@ class TestFilters < JekyllUnitTest
|
||||||
"The list of grouped items for '' is not an Array."
|
"The list of grouped items for '' is not an Array."
|
||||||
)
|
)
|
||||||
# adjust array.size to ignore symlinked page in Windows
|
# 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
|
assert_equal qty, g["items"].size
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -369,22 +369,39 @@ class TestPage < JekyllUnitTest
|
||||||
end
|
end
|
||||||
|
|
||||||
context "read-in by default" do
|
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")
|
page = setup_page("/contacts", "bar.html")
|
||||||
assert_nil page.to_liquid["excerpt"]
|
assert_nil page.to_liquid["excerpt"]
|
||||||
end
|
end
|
||||||
|
|
||||||
should "expose an excerpt to Liquid templates if site is configured to" do
|
context "in a site configured to generate page excerpts" do
|
||||||
configured_site = fixture_site("page_excerpts" => true)
|
setup { @configured_site = fixture_site("page_excerpts" => true) }
|
||||||
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
|
should "initialize excerpt eagerly but render only when needed" do
|
||||||
configured_site = fixture_site("page_excerpts" => true)
|
test_page = Jekyll::Page.new(@configured_site, source_dir, "contacts", "foo.md")
|
||||||
test_page = Jekyll::Page.new(configured_site, source_dir, "assets", "test-styles.scss")
|
assert_equal Jekyll::PageExcerpt, test_page.data["excerpt"].class
|
||||||
refute_equal ".half { width: 50%; }\n", test_page.to_liquid["excerpt"]
|
assert_equal String, test_page.excerpt.class
|
||||||
assert_nil test_page.to_liquid["excerpt"]
|
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" 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -232,6 +232,7 @@ class TestSite < JekyllUnitTest
|
||||||
environment.html
|
environment.html
|
||||||
exploit.md
|
exploit.md
|
||||||
foo.md
|
foo.md
|
||||||
|
foo.md
|
||||||
humans.txt
|
humans.txt
|
||||||
index.html
|
index.html
|
||||||
index.html
|
index.html
|
||||||
|
|
Loading…
Reference in New Issue