The project is in a healthy, maintained state
Generate URL-safe slugs from any string with built-in Unicode transliteration, configurable separators, word-boundary truncation, and collision-aware uniqueness.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-slug

Tests Gem Version License

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 install

Or install directly:

gem install philiprehberger-slug

Usage

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" exists

Transliteration

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 style

License

MIT