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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Jul 13, 2012
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
21 changes: 20 additions & 1 deletion README.md
Expand Up @@ -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 changes: 43 additions & 4 deletions only_first_p.rb
@@ -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

Expand Down