I’d like to make changes to the Jekyll Only First Paragraph plugin to make the generation of a ‘read more ‘ link a configurable option.
To do this I’d need to be able to access the Jekyll site config inside the plugin’s AssetFilter. With the configuration available I can make the change. I don’t know how to make the site configuration available to the plugin.
The code below demonstrates where I’d like site.config available:
require 'nokogiri'
module Jekyll
module AssetFilter
def only_first_p(post)
# site.config needs to be available here to modify the output based on the configuration
output = "<p>"
output << Nokogiri::HTML(post["content"]).at_css("p").inner_html
output << %{</p><a class="readmore" href="#{post["url"]}">Read more</a>}
output
end
end
end
Liquid::Template.register_filter(Jekyll::AssetFilter)
Can this be achieved?
Overview
You can access Jekyll config options in plugins with:
If the config key contains nested levels, the format is:
Example
If a _config.yml contains:
A basic example that simply outputs those values would look like:
Of course, you would want to put checks in that verify that the config key/values are defined before trying to use them. That’s left as an exercise for the reader.
Another Possible Option
The “Liquid filters” section of the Jekyll Plugins Wiki Page contains the following:
I haven’t spent the time to get that to work, but it might be worth checking out as well.