Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
A Ruby gem that truncates HTML strings to a word or character count while preserving valid markup, powered by Nokogiri.
HTML Truncator is a Ruby gem that truncates HTML-formatted strings to a specified number of words or characters while preserving valid markup, using Nokogiri for parsing instead of regex.
HTML Truncator is a Ruby gem, authored by Bruno Michel, that shortens HTML strings to a given word count while keeping tags balanced and valid. It takes an HTML-formatted string plus an integer word count as input and returns the truncated HTML with an ellipsis inserted at a safe position. The entire public API is one class method — HTML_Truncator.truncate — plus three configurable class attributes. The gem is released under the MIT license and distributed as html_truncator on RubyGems.
HTML Truncator exposes a single truncate method whose behavior is controlled by three configurable class attributes, plus optional arguments for the length mode and the ellipsis.
:length_in_chars => true to truncate by characters instead of words, backing off to the nearest word boundary so words are never cut in half.<a href="/more-to-read">...</a>., . : ; ! ? — are removed so the excerpt ends cleanly.<i>.ellipsable_tags, self_closing_tags, and punctuation_chars; for example, deleting "img" from self-closing tags makes images excluded from word counting..html_truncated? on the returned string tells you whether truncation actually happened.Nokogiri::HTML::DocumentFragment can be truncated directly with document.truncate(12, options), merging your own options with HTML_Truncator::DEFAULT_OPTIONS.HTML Truncator suits any Ruby project that needs to produce short, valid HTML excerpts from longer HTML strings.
truncate helper, which can leave unbalanced or incomplete tags, with a parser-based truncator that returns valid HTML.The documented examples cover preview generation, custom read-more ellipses, and integration with already-parsed Nokogiri documents.
read more anchor link so truncated entries link back to the full content.truncate method on the document fragment.Install the gem with gem install html_truncator, or add gem "html_truncator", "~>0.2" to a Gemfile. Then call HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 3). The gem parses the input with Nokogiri, counts only real words, drops punctuation right before the cut, places the ellipsis inside a suitable tag, and serializes the result back to a string.
The author compares HTML Truncator with Rails' truncate helper and several open-source truncation solutions.
truncate helper — its own documentation warns that truncating text containing HTML tags may produce invalid HTML with unbalanced or incomplete tags.Yes. The gem is released under the MIT license. The source itself says "Copying is an act of love. Please copy and share."
Run gem install html_truncator, or add gem "html_truncator", "~>0.2" to your Gemfile when using Bundler.
No. Only real words count toward the truncation limit; tags and attributes are ignored, so a heavily formatted string truncates by its visible text.
Yes. Pass the option :length_in_chars => true. The truncation backs off to the immediately preceding word boundary, so it never cuts a word in half.
The string is returned unmodified with no ellipsis added, so short content is never altered.
