slugify method

String slugify()

Converts the string to a URL-safe slug.

Removes accents, special characters, and excess whitespace, then joins words with hyphens. Case is converted to lowercase. Perfect for generating URL paths from user input or document titles.

Example:

'Hello World!'.slugify()           // 'hello-world'
'Café au Lait'.slugify()           // 'cafe-au-lait'
'foo  bar   baz'.slugify()         // 'foo-bar-baz'

Implementation

String slugify() {
  return normaliseAccents()
        .replaceAll(RegExp(r"[\'/]"), '')
        .replaceAll(RegExp(r'[^\w\s\-._]'), ' ')
        .replaceAll(RegExp(r'[\-_.]'), ' ').trim()
        .replaceAll(RegExp(r'\s+'), '-')
        .toLowerCase();
}