Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to limit Slug Length #50

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ config.CollapseWhiteSpace = true;
// Remove everything that's not a letter, number, hyphen, dot, or underscore
config.DeniedCharactersRegex = @"[^a-zA-Z0-9\-\._]";

// No Character limit to the slug length
config.MaxLength = null;

// Create a helper instance with our new configuration
SlugHelper helper = new SlugHelper(config);
```
Expand Down Expand Up @@ -141,6 +144,12 @@ Type: _Boolean_. Default: **true**

This will condense multiple dashes (e.g. `foo---bar`) down to a single dash (`foo-bar`). This is useful to avoid scenarios like `foo & bar` becoming `foo--bar`.

#### MaxLength

Type: _int?_. Default: **null**

This will limit the length of the generated slug to the number of chars given by the parameter.

License
-------

Expand Down
13 changes: 13 additions & 0 deletions src/Slugify.Core/SlugHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public string GenerateSlug(string inputString)
CollapseDashes(sb);
}

if (Config.MaxLength.HasValue)
{
TrimToMaxLength(sb, Config.MaxLength.Value);
}

return sb.ToString();
}

Expand Down Expand Up @@ -209,6 +214,14 @@ protected static void CollapseDashes(StringBuilder sb)
}

protected static string DeleteCharacters(string str, Regex deniedCharactersRegex) => deniedCharactersRegex.Replace(str, string.Empty);

protected static void TrimToMaxLength(StringBuilder sb, int maxLength)
{
if (sb.Length > maxLength)
{
sb.Remove(maxLength, sb.Length - maxLength);
}
}
}
}

2 changes: 2 additions & 0 deletions src/Slugify.Core/SlugHelperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public HashSet<char> AllowedChars
}
public bool CollapseDashes { get; set; } = true;
public bool TrimWhitespace { get; set; } = true;

public int? MaxLength { get; set; }
}
}

15 changes: 15 additions & 0 deletions tests/Slugify.Core.Tests/SlugHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,21 @@ public void TestHandlingOfUnicodeCharacters()
Assert.Equal(expected, helper.GenerateSlug(original));
}

[Theory]
[InlineData(null, "abcdefghijgklmnopqrstuvwxy", "abcdefghijgklmnopqrstuvwxy")]
[InlineData(8, "abcdefghijgklmnopqrstuvwxy", "abcdefgh")]
[InlineData(8, "ab c d e fgh", "ab-c-d-e")]
[InlineData(7, "ab c d e", "ab-c-d")]
[InlineData(8, "ab c d ", "ab-c-d")]
public void MaxLengthGivenTrimsUnnecessaryChars(int? length, string input, string expected)
{
var helper = Create(new SlugHelperConfiguration()
{
MaxLength = length
});
Assert.Equal(expected, helper.GenerateSlug(input));
}

[Fact(Skip = "Is this actually a bug?")]
public void TurkishEncodingOfI()
{
Expand Down