philiprehberger_slug 0.5.0
philiprehberger_slug: ^0.5.0 copied to clipboard
Unicode-aware URL slug generator with transliteration and collision handling
philiprehberger_slug #
Unicode-aware URL slug generator with transliteration and collision handling
Requirements #
- Dart >= 3.8
Installation #
Add to your pubspec.yaml:
dependencies:
philiprehberger_slug: ^0.5.0
Then run:
dart pub get
Usage #
import 'package:philiprehberger_slug/slug.dart';
final slug = Slug.generate('Hello World!');
// => 'hello-world'
Unicode Transliteration #
Slug.generate('Ünïcödé Tëxt');
// => 'unicode-text'
Slug.generate('Café résumé');
// => 'cafe-resume'
Custom Separator #
Slug.generate('Hello World', separator: '_');
// => 'hello_world'
Max Length #
Slug.generate('A very long title that should be truncated', maxLength: 20);
// => 'a-very-long-title'
Unique Slugs #
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 Validation #
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
Collision Handling #
Slug.withSuffix('hello-world', 2);
// => 'hello-world-2'
Title Case Conversion #
Slug.toTitle('hello-world');
// => 'Hello World'
Slug.toTitle('hello_world', separator: '_');
// => 'Hello World'
API #
| 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 |
Development #
dart pub get
dart analyze --fatal-infos
dart test
Support #
If you find this project useful: