Socket
Book a DemoInstallSign in
Socket

truncato

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

truncato

0.7.13
bundlerRubygems
Version published
Maintainers
1
Created
Source

truncato

truncato is a Ruby library for truncating HTML strings keeping the markup valid.

Installing

In your Gemfile

gem 'truncato'

Usage

Truncato.truncate "<p>some text</p>", max_length: 4 #=> "<p>s...</p>"
Truncato.truncate "<p>some text</p>", max_length: 4, count_tags: false #=> "<p>some...</p>"

The configuration options are:

  • max_length: The size, in characters, to truncate (30 by default)
  • filtered_attributes: Array of attribute names that will be removed in the truncated string. This allows you to make the truncated string shorter by excluding the content of attributes you can discard in some given context, e.g HTML style attribute.
  • filtered_tags: Array of tags that will be removed in the truncated string. If a tag is excluded, all the nested tags under it will be excluded too.
  • count_tags: Boolean value indicating whether tags size should be considered when truncating (true by default)
  • tail_before_final_tag: Boolean value indicating whether to apply a tail before the final closing tag (false by default)
  • comments: Boolean value indicating whether to include comments in parsed results (false by default)
  • tail: The string to append when the truncation occurs ('...' by default)
  • count_tail: Boolean value indicating whether to include the tail within the bounds of the provided max length (false by default)

Performance

Truncato was designed with performance in mind. Its main motivation was that existing libs couldn't truncate a multiple-MB document into a few-KB one in a reasonable time. It uses the Nokogiri SAX parser.

There is a benchmark included that generates a synthetic XML of 4MB and truncates it to 400 KB. You can run the benchmark using

rake truncato:benchmark

There is a also a comparison benchmark that tests the previous data with other alternatives

rake truncato:vendor_compare

The results comparing truncato with other libs:

Truncatotruncate_htmlHTML Truncatorpeppercorn
Time for truncating a 4MB XML document to 4KB1.5 s20 s220 s232 s

Running the tests

rake spec

FAQs

Package last updated on 11 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.