Sidio.Text.Slugify is a simple NuGet package that converts a string to an URL-friendly string, also called a slug.
For example, the string Hello, World!
will be converted to hello-world
.
Add the NuGet package to your project.
services.AddSlugifier();
// - or -
services.AddSlugifier(options => {
options.AddDefaultProcessors = false;
options.Processors.Add(new LowerCaseProcessor());
});
public class MyClass()
{
public MyClass(ISlugifier slugifier)
{
var slug = slugifier.Slugify("Hello, World!");
}
}
var slugifier = Slugifier.Create();
var slug = slugifier.Slugify("Hello, World!");
Using the extension method is not recommended because it creates a new instance of
the Slugifier
class every time it is called.
using Sidio.Text.Slugify.Extensions;
var slug = "Hello, World!".Slugify();
You can implement your own processors by implementing the SlugifyProcessor
class. For example:
public sealed class RemoveExclamationMarkProcessor : SlugifyProcessor
{
protected override string ProcessInput(string input)
{
return input.Replace("!", string.Empty);
}
// run this processor first
public override int Order => 0; // or int.MaxValue for last
}