Slugify Service
One way to generate a valid URL, using data already obtained, is to make a slug. This service was in an earlier version named SuggestionService. But sometimes the service returned values that already was in use so it is replaced by the SlugifyService.
When creating a slug, the SlugifyService is used for converting text to an url. Eg. the name of the "about us" page will get the url-segment "about-us". Another common area of use, is slugifying the title on a blog to a url.
/// <summary>
/// Slugify service will create a slug of the input text.
/// </summary>
[Service(ServiceType = typeof(SlugifyService), Lifetime = DependencyLifetime.Singleton)]
[RequireServiceImplementation]
public abstract class SlugifyService
{
/// <summary>
/// Make the slug from the specified text.
/// </summary>
/// <param name="cultureInfo">Culture info that should be used for the slug.</param>
/// <param name="text">Input text that should be used for the slug.</param>
/// <returns>Returns the slugified version of the text.</returns>
public abstract string Slugify([NotNull] CultureInfo cultureInfo, string text);
}
The main method Slugify will create a slug of the given input text. CutureInfo is added to the method as a place holder for future use, current implementation does not make any difference between cultures yet.
These rule checks are executed in the default implementation of SlugifyService:
- Invalid Characters from UrlValidatorConfig.DefaultInvalidCharacters (static property of UrlValidatorConfig) and a running instance of urlValidatorconfig.InvalidCharacters (extendable) will be replace with "-".
- [åääâáàÅÄ] is replaced with "a".
- [öóòôöÖôóòØ] is replaced with "o"
- [éèêëÉËÊÈ€] is replaced with "e"
- [Ææ] is replaced with "ae"
- [üûúùÜÛÚÙ] is replaced with "u"
- "--" is replaced with "-"
- "__" is replaced with "_"
- If input string has extension match with UrlValidatiorConfig.DefaultInvalidExtensions or instance urlValidatorConfig.InvalidExtensions then that extension will be truncated.
Input |
Output |
"test with space" |
"test-with-space" |
"a.b,c;d:e+[f]g{s}w-" |
"a-b-c-d-e-f-g-s-w" |
"a--b" |
"a-b" |
"a__b" |
"a_b" |
"a/\\\.,;: +*'§½""<>\|!?$&=@#%\{\}\^\~\[\]`´¨¤—–‘’“”b" |
"a-b" |
"\u2007 \u00A0 \u0020 \u202F \u2060 \uFEFF" |
"" |