Permalink
Browse files

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

  • Loading branch information...
1 parent ab33b06 commit 3f534f91865b2adb8c2b360c36c082551fb88499 @webignition webignition committed Jul 13, 2012
Showing with 63 additions and 5 deletions.
  1. +20 −1 README.md
  2. +43 −4 only_first_p.rb
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/
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

0 comments on commit 3f534f9

Please sign in to comment.