Skip to content

Commit bef8b9e

Browse files
authored
excerpt first paragraph (#338)
1 parent 68e085f commit bef8b9e

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source "https://rubygems.org"
22

33
gem "jekyll"
4+
gem "nokogiri"
45
group :jekyll_plugins do
56
gem "jekyll-paginate"
67
gem "jekyll-seo-tag"

_layouts/home.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ <h3>{{ post.title }}</h3>
1919
{% include author.html %}
2020
</div>
2121
<hr>
22-
<div class="snippet snippet-2">
23-
<p>{{ post.content | strip_html | truncatewords:50 }}</p>
22+
<div class="snippet">
23+
<p>{{ post.content | first_paragraph | strip_html}}</p>
2424
</div>
2525
</div>
2626
</a>

_plugins/first_paragraph.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Jekyll plugin that adds the `first_paragraph` Liquid filter, which returns
3+
# only the first paragraph of an HTML string.
4+
#
5+
6+
require 'nokogiri'
7+
8+
9+
module Jekyll
10+
module AssetFilter
11+
def first_paragraph(html)
12+
paragraph = Nokogiri::HTML(html).css('p').first.to_html
13+
end
14+
end
15+
end
16+
17+
18+
Liquid::Template.register_filter(Jekyll::AssetFilter)

0 commit comments

Comments
 (0)