Skip to content
A Jekyll plugin that truncates HTML while preserving markup structure.
Latest commit 822e287 Oct 27, 2013 @MattHall Merge pull request #9 from sionide21/patch-2
Fix plugin to produce valid HTML
Failed to load latest commit information.
LICENSE Add MIT license. Resolves #5 Jun 2, 2013 Fix capitalization in README Oct 27, 2013
html_filters.rb Do not expect full HTML document Oct 27, 2013


The truncator requires Nokogiri to parse out the HTML string.


If you're using Jekyll, add the html_filters.rb file to your _plugins directory - this will give you the helper truncatehtml as a Liquid filter. In your views, you can use this function in the same way as you would use the normal truncate filter:

page.content | truncatehtml: 500

How it works

Given a snippet of HTML, we use Nokogiri to parse it into a tree. This takes care of all of the messiness of dealing with HTML, and gives us back a tree of nodes representing the parsed snippet.

Now that we have the tree of nodes, we can traverse it depth-first. All text nodes are leaf nodes, so when we encounter one, we can count the length of the text.

Once we have all of the text we need, we continue traversing, but instead of counting text lengths, we delete all of the nodes we see after we've reached our maximum length.

After that, we have a tree of nodes that represent the truncated tree, and the can output the appropriate HTML.

Something went wrong with that request. Please try again.