#slug #slugify #macro #word

slugify-rs

A rust library to generate slugs from strings

2 releases

0.0.3 Apr 28, 2022
0.0.2 Apr 28, 2022
0.0.1 Apr 27, 2022

#889 in Text processing

Download history 1179/week @ 2024-07-20 1235/week @ 2024-07-27 927/week @ 2024-08-03 918/week @ 2024-08-10 1536/week @ 2024-08-17 1794/week @ 2024-08-24 2375/week @ 2024-08-31 2863/week @ 2024-09-07 3544/week @ 2024-09-14 3022/week @ 2024-09-21 3053/week @ 2024-09-28 6002/week @ 2024-10-05 4428/week @ 2024-10-12 4467/week @ 2024-10-19 3924/week @ 2024-10-26 2686/week @ 2024-11-02

16,540 downloads per month
Used in 7 crates (via kaspa-wallet-core)

Custom license

21KB
385 lines

Slugify-rs

A utility macro for flexible slug genereation that handles unicode.

The slugify! macro implements a flexible slug generator, allowing for stop words, custom separator and maximum length options. The macro provides both a simple interface with sane default parameters but also allows the parameters to be overriden when needed.

Features:

  • Unicode strings support (phonetic conversion).
  • Support for custom slug separator.
  • Stop words filtering.
  • Slug maximum length support.
  • Add randomness to the generated slug to avoid unique constraint from acting

Usage

This crate is on crates.io and can be used by adding slugify to the dependencies in your project’s Cargo.toml

[dependencies]
slugify-rs = "0.0.3"

Examples Basic slug generation

assert_eq!(slugify!("hello world"), "hello-world");

// Using a custom separator
assert_eq!(slugify!("hello world", separator = "."), "hello.world");
assert_eq!(slugify!("hello world", separator = " "), "hello world");
assert_eq!(slugify!("hello world", separator = ""), "helloworld");

// Stop words filtering
assert_eq!(slugify!("the quick brown fox jumps over the lazy dog", stop_words = "the,fox"), "quick-brown-jumps-over-lazy-dog");

// Maximum length
assert_eq!(slugify!("hello world", max_length = 5), "hello");
assert_eq!(slugify!("the hello world", stop_words = "the", max_length = 5), "hello");

// Random values added to string through nanoid
// Default randomness string length is 5.
assert_eq!(slugify!("hello world", randomness=true).len(), "hello-world".len()+5);
assert_eq!(slugify!("hello world", randomness=true,randomness_length=8).len(), "hello-world".len()+8);

// Phonetic Conversion and accented text
assert_eq!(slugify!("影師嗎"), "ying-shi-ma");
assert_eq!(slugify!("Æúű--cool?"), "aeuu-cool");
assert_eq!(slugify!("Nín hǎo. Wǒ shì zhōng guó rén"), "nin-hao-wo-shi-zhong-guo-ren");

// Passing multiple optional parameters.
// NOTE: the order of optional parameters matters: stop_words, separator and then max_length. All of them are optional, however when specifying more than one optional parameter, this order must be adhered.

assert_eq!(slugify!("the hello world", stop_words = "the", separator = "-"), "hello-world");
assert_eq!(slugify!("the hello world", separator = ".", max_length = 10), "the.hello");
assert_eq!(slugify!("the hello world", stop_words = "the", max_length = 5), "hello");
assert_eq!(slugify!("the hello world", stop_words = "the", separator = "-", max_length = 20), "hello-world");

Info

This slug was forked from the original slugify crate by @mattgathu

Dependencies

~495KB