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 SlugOptions

Constructors

NameDescription
SlugOptions()Initializes a new instance of the SlugOptions class with default settings.

SlugOptions()

SlugOptions.cs

Initializes a new instance of the SlugOptions class with default settings.

public SlugOptions()

Properties

NameDescription
AllowedRangesGets the list of allowed Unicode character ranges in the generated slug.
CanEndWithSeparatorGets or sets a value indicating whether the generated slug can end with a separator.
CasingTransformationGets or sets the case transformation to apply to the slug.
CultureGets or sets the culture to use for case transformations. When null, uses invariant culture.
MaximumLengthGets 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.
SeparatorGets or sets the separator string used between words. Default is "-".

AllowedRanges

SlugOptions.cs

Gets the list of allowed Unicode character ranges in the generated slug.

public IList<UnicodeRange> AllowedRanges { get; }

Property value

IList<UnicodeRange>

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.cs

Gets or sets a value indicating whether the generated slug can end with a separator.

public bool CanEndWithSeparator { get; set; }

Property value

bool

CasingTransformation

SlugOptions.cs

Gets or sets the case transformation to apply to the slug.

public CasingTransformation CasingTransformation { get; set; }

Property value

CasingTransformation

Gets or sets the culture to use for case transformations. When null, uses invariant culture.

public CultureInfo? Culture { get; set; }

Property value

CultureInfo?

MaximumLength

SlugOptions.cs

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.

public int MaximumLength { get; set; }

Property value

int

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.

Gets or sets the separator string used between words. Default is "-".

public string Separator { get; set; }

Property value

string

Exceptions

ArgumentNullException
value is null.

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

NameDescription
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.cs

Determines whether the specified character is allowed in the slug.

public virtual bool IsAllowed(Rune character)

Parameters

character Rune
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.cs

Replaces a rune with its transformed version based on the configured casing transformation.

public virtual string Replace(Rune rune)

Parameters

rune Rune
The rune to replace.

Returns

string The transformed string representation of the rune.

Constants

NameDescription
DefaultMaximumLengthThe default maximum length for generated slugs (80 characters).
DefaultSeparatorThe default separator used between words ("-").

DefaultMaximumLength

The default maximum length for generated slugs (80 characters).

public const int DefaultMaximumLength = 80

DefaultSeparator

The default separator used between words ("-").

public const string DefaultSeparator = "-"