SlugOptions Class
Provides options for customizing slug generation.
var options = new SlugOptions
{
MaximumLength = 20,
CasingTransformation = CasingTransformation.ToLowerCase,
};
var slug = Slug.Create("This is a text", options);
Assert.Equal("this-is-a-text", slug);public class SlugOptionsConstructors
| Name | Description |
|---|---|
| SlugOptions() | Initializes a new instance of the SlugOptions class with default settings. |
SlugOptions()
SlugOptions.csInitializes a new instance of the SlugOptions class with default settings.
public SlugOptions()Properties
| Name | Description |
|---|---|
| AllowedRanges | Gets the list of allowed Unicode character ranges in the generated slug. |
| CanEndWithSeparator | Gets or sets a value indicating whether the generated slug can end with a separator. |
| CasingTransformation | Gets or sets the case transformation to apply to the slug. |
| Culture | Gets or sets the culture to use for case transformations. When null, uses invariant culture. |
| MaximumLength | Gets or sets the maximum length of the generated slug. Default is 80. A value less than or equal to zero means the slug is not truncated. |
| Separator | Gets or sets the separator string used between words. Default is "-". |
AllowedRanges
SlugOptions.csGets the list of allowed Unicode character ranges in the generated slug.
public IList<UnicodeRange> AllowedRanges { get; }Property value
Remarks
An empty list allows every character instead of none, so the slug is returned unfiltered. The list has no setter: add to it, or call ICollection<T>.Clear and then add, to change which characters are kept. Clearing it and adding nothing is what makes the generation permissive.
CanEndWithSeparator
SlugOptions.csCasingTransformation
SlugOptions.csGets or sets the case transformation to apply to the slug.
public CasingTransformation CasingTransformation { get; set; }Property value
Culture
SlugOptions.csGets or sets the culture to use for case transformations. When null, uses invariant culture.
public CultureInfo? Culture { get; set; }Property value
MaximumLength
SlugOptions.csGets or sets the maximum length of the generated slug. Default is 80. A value less than or equal to zero means the slug is not truncated.
public int MaximumLength { get; set; }Property value
Remarks
The limit applies to the returned slug and is never exceeded. A slug is only cut between characters, so it never ends with an incomplete surrogate pair, a partial SlugOptions.Separator, or a character stripped of the combining marks that follow it. Because those units are kept whole, a slug can end up slightly shorter than the limit.
Truncation makes slugs lossy: two different inputs that agree up to the limit produce the same slug, as do inputs that differ only by characters this class removes. A slug is therefore never unique. Callers that key on one must enforce uniqueness themselves, and must not use it as a security boundary.
Separator
SlugOptions.csGets or sets the separator string used between words. Default is "-".
public string Separator { get; set; }Property value
Exceptions
ArgumentNullExceptionvalueisnull.
Remarks
The separator should be made of characters that SlugOptions.IsAllowed rejects. When a separator character can also come from the input, a trailing occurrence of it is indistinguishable from a separator this class emitted.
Methods
| Name | Description |
|---|---|
| IsAllowed(Rune) | Determines whether the specified character is allowed in the slug. |
| Replace(Rune) | Replaces a rune with its transformed version based on the configured casing transformation. |
IsAllowed(Rune)
SlugOptions.csDetermines whether the specified character is allowed in the slug.
Parameters
characterRune- The character to check.
Returns
bool true if the character is allowed; otherwise, false.
Remarks
Every character is allowed when SlugOptions.AllowedRanges is empty. The check runs on the character read from the input, before SlugOptions.Replace and SlugOptions.CasingTransformation are applied to it.
Replace(Rune)
SlugOptions.csConstants
| Name | Description |
|---|---|
| DefaultMaximumLength | The default maximum length for generated slugs (80 characters). |
| DefaultSeparator | The default separator used between words ("-"). |
DefaultMaximumLength
The default maximum length for generated slugs (80 characters).
public const int DefaultMaximumLength = 80DefaultSeparator
The default separator used between words ("-").
public const string DefaultSeparator = "-"