Skip to content

Commit

Permalink
Remove trailing hyphens count from a slug (#12433)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored Sep 16, 2022
1 parent c5f0a54 commit f42211c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,25 @@ public string Slugify(string text)
}
}

return new string(slug.AsSpan()[..Math.Min(slug.Length, MaxLength)]).Normalize(NormalizationForm.FormC);
var length = Math.Min(slug.Length - GetTrailingHyphenCount(slug.AsSpan()), MaxLength);

return new string(slug.AsSpan()[..length]).Normalize(NormalizationForm.FormC);
}

private static int GetTrailingHyphenCount(ReadOnlySpan<char> input)
{
var hyphenCount = 0;
for (var i = input.Length - 1; i >= 0; i--)
{
if (input[i] != Hyphen)
{
break;
}

++hyphenCount;
}

return hyphenCount;
}
}
}
10 changes: 10 additions & 0 deletions test/OrchardCore.Tests/Tokens.Content/SlugServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public void ShouldChangeDotSymbolsToHyphans()
Assert.Equal("a-d", slug);
}

[Theory]
[InlineData("Smith, John B.")]
[InlineData("Smith, John B...")]
public void ShouldRemoveHyphansFromEnd(string input)
{
// Act
var slug = _slugService.Slugify(input);
Assert.Equal("smith-john-b", slug);
}

[Fact]
public void ShouldMakeSureFunkycharactersAndHyphansOnlyReturnSingleHyphan()
{
Expand Down

0 comments on commit f42211c

Please sign in to comment.