Getting started
editInstall the package
Add the Meziantou.Framework.Slug package to your project:
dotnet package add Meziantou.Framework.SlugThe examples of this documentation omit the using directives: they need using Meziantou.Framework;. They check their results with assertions such as Assert.Equal, which show what each call returns.
Create a slug
var result = Slug.Create("This is a text");
Assert.Equal("This-is-a-text", result);The default rules remove diacritics and punctuation:
// Diacritics are removed
Assert.Equal("Creme-brulee-a-la-francaise", Slug.Create("Crème brûlée à la française"));
// Other characters become a single separator, never at the start or the end
Assert.Equal("Hello-World-2024", Slug.Create(" Hello, World! (2024) "));
// Text written outside the allowed ranges gives an empty slug
Assert.Equal("", Slug.Create("Привет, мир"));Slug.Create(null)returnsnull.- The result is an empty string when no character of the text is allowed. With the default options, this is the case for any text written without Latin letters or digits, such as a Cyrillic, Greek, Arabic or CJK title. Handle the empty slug before you put it in a URL or a file path, or allow more characters.
- Text that is not well-formed Unicode, such as a string cut in the middle of a surrogate pair, does not throw.
Lower-case slugs
URLs are often in lower case. Set CasingTransformation:
var slug = Slug.Create("This is a test!");
Assert.Equal("This-is-a-test", slug);
var lowerSlug = Slug.Create("Hello World", new SlugOptions { CasingTransformation = CasingTransformation.ToLowerCase });
Assert.Equal("hello-world", lowerSlug);Next steps
- Options: the maximum length, the separator, the allowed characters, and custom replacements.