Added per post excerpt_separator functionality, so you are able to
specify :excerpt_separator (as well as just :excerpt) key direct inside the post YAML, to make an excerpt based on the value in the post. Tests were also added.
This commit is contained in:
parent
d47b882af2
commit
54d0bf47e8
|
@ -12,6 +12,7 @@ gh-pages/
|
|||
site/_site/
|
||||
coverage
|
||||
.ruby-version
|
||||
.ruby-gemset
|
||||
.sass-cache
|
||||
tmp/stackprof-*
|
||||
.jekyll-metadata
|
||||
|
|
|
@ -105,7 +105,9 @@ module Jekyll
|
|||
#
|
||||
# Returns excerpt String
|
||||
def extract_excerpt(post_content)
|
||||
separator = site.config['excerpt_separator']
|
||||
separator = !post.excerpt_separator.empty? &&
|
||||
post.excerpt_separator ||
|
||||
site.config['excerpt_separator']
|
||||
head, _, tail = post_content.to_s.partition(separator)
|
||||
|
||||
"" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n")
|
||||
|
|
|
@ -23,6 +23,7 @@ module Jekyll
|
|||
ATTRIBUTES_FOR_LIQUID = EXCERPT_ATTRIBUTES_FOR_LIQUID + %w[
|
||||
content
|
||||
excerpt
|
||||
excerpt_separator
|
||||
]
|
||||
|
||||
# Post name validator. Post filenames must be like:
|
||||
|
@ -118,6 +119,13 @@ module Jekyll
|
|||
data.fetch('title') { titleized_slug }
|
||||
end
|
||||
|
||||
# Public: the Post excerpt_separator, from the YAML Front-Matter or empty string
|
||||
#
|
||||
# Returns the post excerpt_separator
|
||||
def excerpt_separator
|
||||
data.fetch('excerpt_separator') { "" }
|
||||
end
|
||||
|
||||
# Turns the post slug into a suitable title
|
||||
def titleized_slug
|
||||
slug.split('-').select {|w| w.capitalize! || w }.join(' ')
|
||||
|
@ -307,7 +315,7 @@ module Jekyll
|
|||
end
|
||||
|
||||
def generate_excerpt?
|
||||
!(site.config['excerpt_separator'].to_s.empty?)
|
||||
!site.config['excerpt_separator'].to_s.empty? || !excerpt_separator.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -171,6 +171,16 @@ your `excerpt_separator` to `""`.
|
|||
|
||||
Also, as with any output generated by Liquid tags, you can pass the `| strip_html` flag to remove any html tags in the output. This is particularly helpful if you wish to output a post excerpt as a `meta="description"` tag within the post `head`, or anywhere else having html tags along with the content is not desirable.
|
||||
|
||||
Additionally you are able to specify per-post `excerpt_separator` value if it is required just only the the selected post. Just specify the `excerpt_separator` with the same way as `excerpt` in the post's YAML head:
|
||||
|
||||
---
|
||||
excerpt_separator: <!--more-->
|
||||
---
|
||||
|
||||
Excerpt
|
||||
<!--more-->
|
||||
Out-of-excerpt
|
||||
|
||||
## Highlighting code snippets
|
||||
|
||||
Jekyll also has built-in support for syntax highlighting of code snippets using
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
layout: ~
|
||||
title: Post Excerpt Separator
|
||||
excerpt_separator: "\n---\n"
|
||||
---
|
||||
|
||||
First paragraph with [link ref][link].
|
||||
|
||||
Second paragraph
|
||||
|
||||
---
|
||||
|
||||
Third paragraph
|
||||
|
||||
[link]: http://www.jekyllrb.com/
|
|
@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "ensure post count is as expected" do
|
||||
assert_equal 44, @site.posts.size
|
||||
assert_equal 45, @site.posts.size
|
||||
end
|
||||
|
||||
should "insert site.posts into the index" do
|
||||
|
|
|
@ -397,6 +397,22 @@ class TestPost < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "with page's excerpt_separator setting" do
|
||||
setup do
|
||||
file = "2015-01-08-post-excerpt-separator.markdown"
|
||||
|
||||
@post.process(file)
|
||||
@post.read_yaml(@source, file)
|
||||
@post.transform
|
||||
end
|
||||
|
||||
should "respect given separator" do
|
||||
assert @post.excerpt.include?("First paragraph"), "contains first paragraph"
|
||||
assert @post.excerpt.include?("Second paragraph"), "contains second paragraph"
|
||||
assert !@post.excerpt.include?("Third paragraph"), "does not contains third paragraph"
|
||||
end
|
||||
end
|
||||
|
||||
context "with custom excerpt" do
|
||||
setup do
|
||||
file = "2013-04-11-custom-excerpt.markdown"
|
||||
|
|
Loading…
Reference in New Issue