striptags 0.1.1

Strip HTML tags from a string, with optional allowed tags and a replacement. A faithful port of the striptags npm package. Zero dependencies, no_std.
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented4 out of 8 items with examples
  • Size
  • Source code size: 29.95 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 196.99 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/striptags
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

striptags

All Contributors

Crates.io Documentation CI License

Strip HTML tags from a string — with optional allowed tags and a replacement for removed tags. A faithful Rust port of the striptags npm package: the same plaintext/html/comment state machine, including its handling of comments, quoted attributes, and stray </>. Zero dependencies and #![no_std].

use striptags::{strip_tags, strip_tags_with};

assert_eq!(strip_tags("<p>Hello <b>world</b></p>"), "Hello world");
assert_eq!(strip_tags_with("<p>Hi</p><a>x</a>", &["a"], ""), "Hi<a>x</a>");
assert_eq!(strip_tags_with("<p>Hi</p>", &[], "\n"), "\nHi\n");

Why striptags?

Pulling readable text out of HTML — for search indexing, previews, plain-text emails, or light sanitization — is a common need, and the canonical JS implementation handles the tricky bits (comments, > inside quoted attributes, unterminated tags, a < b). This ports it faithfully.

[dependencies]
striptags = "0.1"

Note: like the original, this is a fast tag remover, not a security sanitizer. For untrusted HTML where XSS matters, use a dedicated sanitizer such as ammonia.

API

Item Purpose
strip_tags(html) Remove all tags
strip_tags_with(html, allowed, replacement) Keep allowed tags; replace removed ones
StripTags Streaming stripper that keeps state across feed calls

Behavior

  • Allowed tag names are matched against the lower-cased tag name, so pass them in lower case (&["p", "a"]).
  • HTML comments (<!-- … -->) are removed entirely (no replacement emitted).
  • A > inside a quoted attribute is ignored; a stray < (with a space) is kept as text; an unterminated tag at end of input produces no output.

Contributors ✨

This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

License

Licensed under either of Apache-2.0 or MIT at your option.