philiprehberger-slug
URL-friendly slug generator with Unicode transliteration, configurable separators, word-boundary truncation, and collision-aware uniqueness.
Requirements
- Ruby >= 3.1
Installation
Add to your Gemfile:
gem "philiprehberger-slug"Then run:
bundle installOr install directly:
gem install philiprehberger-slugUsage
require "philiprehberger/slug"
Philiprehberger::Slug.generate("Hello World!") # => "hello-world"
Philiprehberger::Slug.generate("Héllo Wörld") # => "hello-world"
Philiprehberger::Slug.generate("Привет мир") # => "privet-mir"Custom Separator
Philiprehberger::Slug.generate("Hello World", separator: "_") # => "hello_world"Max Length
Truncates at the nearest word boundary:
Philiprehberger::Slug.generate("Hello Beautiful World", max: 16) # => "hello-beautiful"Uniqueness
Pass a callable that returns true if the slug already exists:
Philiprehberger::Slug.generate("My Post", unique: ->(s) { Post.exists?(slug: s) })
# => "my-post" or "my-post-2" if "my-post" existsTransliteration
Philiprehberger::Slug.transliterate("café résumé") # => "cafe resume"API
| Method | Description |
|---|---|
Slug.generate(string, separator: "-", max: nil, unique: nil) |
Generate a URL-safe slug |
Slug.transliterate(string) |
Unicode to ASCII transliteration |
Development
bundle install
bundle exec rspec # Run tests
bundle exec rubocop # Check code styleLicense
MIT