Unicode-aware URL slug generator with transliteration and collision handling
dart pub add philiprehberger_slugUnicode-aware URL slug generator with transliteration and collision handling
Add to your pubspec.yaml:
dependencies:
philiprehberger_slug: ^0.5.0
Then run:
dart pub get
import 'package:philiprehberger_slug/slug.dart';
final slug = Slug.generate('Hello World!');
// => 'hello-world'
Slug.generate('Ünïcödé Tëxt');
// => 'unicode-text'
Slug.generate('Café résumé');
// => 'cafe-resume'
Slug.generate('Hello World', separator: '_');
// => 'hello_world'
Slug.generate('A very long title that should be truncated', maxLength: 20);
// => 'a-very-long-title'
import 'package:philiprehberger_slug/philiprehberger_slug.dart';
final slug = await Slug.unique(
'Hello World',
exists: (candidate) => database.slugExists(candidate),
);
// Returns "hello-world", "hello-world-1", "hello-world-2", etc.
Slug.isSlug('hello-world'); // => true
Slug.isSlug('Hello World!'); // => false
Slug.isSlug('hello_world', separator: '_'); // => true
Slug.isSlug(''); // => false
Slug.isSlug('-leading'); // => false
Slug.isSlug('double--dash'); // => false
Slug.withSuffix('hello-world', 2);
// => 'hello-world-2'
Slug.toTitle('hello-world');
// => 'Hello World'
Slug.toTitle('hello_world', separator: '_');
// => 'Hello World'
| Method | Description |
|---|---|
Slug.generate(input, {separator, maxLength}) | Generate a URL-safe slug from any string |
Slug.unique(input, {separator, maxLength, exists}) | Generate a collision-free slug with async callback |
Slug.isSlug(input, {separator}) | Check if a string is already a valid slug |
Slug.toTitle(slug, {separator}) | Convert a slug back to title case |
Slug.withSuffix(slug, suffix, {separator}) | Append a numeric suffix for collision avoidance |
dart pub get
dart analyze --fatal-infos
dart test
If you find this project useful: