Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Make output of 'Read more' link, and the link text, configurable #1

Merged
merged 2 commits into from

2 participants

Jon Cram Sebastian Zuchmanski
Jon Cram

I needed to output the first paragraph of a post but without the 'read more' link.

I've updated the plugin to make the 'read more' link, and associated link text, configurable in _config.yml. The readme has been updated accordingly.

Sebastian Zuchmanski
Owner

Great job.

Thank you.

Sebastian Zuchmanski sebcioz merged commit 05e7369 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 63 additions and 5 deletions.
  1. +20 −1 README.md
  2. +43 −4 only_first_p.rb
21 README.md
View
@@ -4,9 +4,28 @@ The truncator requires [Nokogiri][4] to parse out the HTML string.
## Usage
-If you're using Jekyll, add the only_first_p.rb file to your _plugins directory - this will give you the helper `only_first_p` as a [Liquid][3] filter. In your views, you can use this function in order to *show only first paragpaph of page/post*
+If you're using Jekyll, add the only_first_p.rb file to your _plugins directory - this will give you the helper `only_first_p` as a [Liquid][3] filter. In your views, you can use this function in order to *show only first paragraph of page/post*
page | only_first_p
+## Default output
+
+ <p>
+ {first paragraph content}
+ </p><a class="readmore" href="{post-url}">Read more</a>
+
+## Configuration
+
+You can choose whether to output a 'Read more' link and the link text to use.
+
+Update your _config.yml as required.
+
+ # Only First P Plugin
+ only_first_p:
+ show_read_more_link: true
+ read_more_link_text: 'Read more'
+
+The above is the default configuration and will be used if nothing is specified. Modify as needed.
+
[3]:http://liquidmarkup.org
[4]:http://nokogiri.org/
47 only_first_p.rb
View
@@ -1,14 +1,53 @@
require 'nokogiri'
module Jekyll
- module AssetFilter
- def only_first_p(post)
+ module AssetFilter
+ @@only_first_p_config = nil
+ @@only_first_p_default_config = {
+ "show_read_more_link" => true,
+ "read_more_link_text" => "Read more"
+ }
+
+ def only_first_p(post)
output = "<p>"
output << Nokogiri::HTML(post["content"]).at_css("p").inner_html
- output << %{</p><a class="readmore" href="#{post["url"]}">Read more</a>}
+ output << %{</p>}
+
+ if only_first_p_config()['show_read_more_link']
+ output << %{<a class="readmore" href="#{post["url"]}">}
+ output << only_first_p_config()['read_more_link_text']
+ output << %{</a>}
+ end
output
- end
+ end
+
+ def only_first_p_config
+ if @@only_first_p_config == nil
+ jekyll_configuration = Jekyll.configuration({})
+
+ if jekyll_configuration['only_first_p'] == nil
+ @@only_first_p_config = @@only_first_p_default_config
+ else
+ if jekyll_configuration['only_first_p'].kind_of?(Object)
+ @@only_first_p_config = {}
+
+ @@only_first_p_default_config.each.each do |key,value|
+ if jekyll_configuration['only_first_p'][key] == nil
+ @@only_first_p_config[key] = value
+ else
+ @@only_first_p_config[key] = jekyll_configuration['only_first_p'][key]
+ end
+ end
+ else
+ @@only_first_p_config = @@only_first_p_default_config
+ end
+ end
+ end
+
+ @@only_first_p_config
+ end
+
end
end
Something went wrong with that request. Please try again.