Slug

ceramic.Slug (Class)

URL-safe string generator that converts text into slugs suitable for URLs and filenames.

The Slug class transforms strings by replacing special characters, accented letters, and spaces with URL-safe equivalents. It handles a comprehensive set of Unicode characters, converting them to their ASCII equivalents where possible.

Features:

  • Converts accented characters to ASCII equivalents (é→e, ñ→n, etc.)
  • Replaces spaces with hyphens or custom separators
  • Removes or replaces special characters
  • Handles currency symbols, mathematical symbols, and various scripts
  • Optionally converts to lowercase
  • Customizable character removal and replacement rules

Example usage:

// Basic usage
var slug = Slug.encode("Hello World!"); // "hello-world"
var slug2 = Slug.encode("Café Résumé"); // "cafe-resume"

// Custom options
var options:SlugOptions = {
    lower: false,        // Keep original case
    replacement: "_",    // Use underscore instead of hyphen
    remove: ~/[.]/g     // Remove only dots
};
var slug3 = Slug.encode("Hello.World", options); // "Hello_World"

// Handling special characters
Slug.encode("Price: $100"); // "price-dollar100"
Slug.encode("Love ♥ Haxe"); // "love-love-haxe"

This is particularly useful for:

  • Creating SEO-friendly URLs from page titles
  • Generating safe filenames from user input
  • Creating identifiers from human-readable text

Based on the popular JavaScript slugify library by simov.

Static Members

RE_SLUG_REMOVE_CHARS: EReg

Default regex pattern for characters to remove from slugs. Removes: $ * + ~ . ( ) ' " ! \ : @ ? §


DEFAULT_OPTIONS: SlugOptions

Default options for slug generation. Uses lowercase conversion, standard character removal, and hyphen replacement.


charMap: Map

Comprehensive character mapping table for converting Unicode characters to ASCII equivalents. Maps Unicode code points to their slug-safe string representations. Includes accented letters, currency symbols, Greek letters, Cyrillic, and more.


safe: EReg

Regex pattern for characters considered safe in slugs. Matches anything that is NOT a word character, space, or special safe character.


encode(str: String, ?options: SlugOptions): String

Converts a string into a URL-safe slug.

The encoding process:

  1. Replaces Unicode characters with ASCII equivalents using charMap
  2. Trims whitespace from beginning and end
  3. Removes unwanted characters based on options.remove pattern
  4. Replaces spaces with the specified replacement character
  5. Optionally converts to lowercase
Name Type Default Description
str String The string to convert into a slug
options SlugOptions (optional) Optional configuration for slug generation. If null, uses DEFAULT_OPTIONS.
Returns Description
String The URL-safe slug version of the input string