Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make page excerpts consistent with doc excerpts #8236

Merged
merged 3 commits into from Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/jekyll/page.rb
Expand Up @@ -49,6 +49,7 @@ def initialize(site, base, dir, name)

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)
Expand Down Expand Up @@ -185,14 +186,25 @@ def write?
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
11 changes: 5 additions & 6 deletions lib/jekyll/page_excerpt.rb
Expand Up @@ -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?
Expand Down
8 changes: 4 additions & 4 deletions test/source/assets/test-styles.scss
@@ -1,4 +1,4 @@
---
---
@import "{{ site.skin | default: 'grid' }}";
---
---

@import "{{ site.skin | default: 'grid' }}";
7 changes: 7 additions & 0 deletions test/source/contacts/foo.md
@@ -0,0 +1,7 @@
---
title: Contact Information
---

## {{ page.title }}

In case of emergency, contact Mr. John Doe, 1234, Foo Road, Foo.
4 changes: 2 additions & 2 deletions test/test_filters.rb
Expand Up @@ -816,7 +816,7 @@ def to_liquid
"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
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def to_liquid
"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
Expand Down
39 changes: 28 additions & 11 deletions test/test_page.rb
Expand Up @@ -369,22 +369,39 @@ def do_render(page)
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")
assert_equal "Contact Information\n", test_page.to_liquid["excerpt"]
end
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")
refute_equal ".half { width: 50%; }\n", test_page.to_liquid["excerpt"]
assert_nil test_page.to_liquid["excerpt"]
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

Expand Down
1 change: 1 addition & 0 deletions test/test_site.rb
Expand Up @@ -232,6 +232,7 @@ def generate(site)
environment.html
exploit.md
foo.md
foo.md
humans.txt
index.html
index.html
Expand Down