Skip to content

Commit

Permalink
Improve SeoUtils add charsWhiteList
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Feb 14, 2025
1 parent 539d770 commit ad684b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/DNTPersianUtils.Core.Tests/SeoUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ public class SeoUtilsTests
{
[DataTestMethod]
[DataRow("\"چگونه می\u200cتوان ریسک\u200cهای معامله را کاهش داد؟\"",
"چگونه-می\u200cتوان-ریسک\u200cهای-معامله-را-کاهش-داد")]
"چگونه-می\u200cتوان-ریسک\u200cهای-معامله-را-کاهش-داد")]
[DataRow("براي «آزمايش»--- است!", "برای-آزمایش-است")]
public void Test_GetPostSlug_Works(string text, string expectedSlug)
{
Assert.AreEqual(expectedSlug, text.GetPostSlug());
}
=> Assert.AreEqual(expectedSlug, text.GetPostSlug());

[DataTestMethod]
[DataRow("C++", "C++")]
[DataRow("(C++)", "C++")]
public void Test_GetCleanedTag_Works(string text, string expectedTag)
=> Assert.AreEqual(expectedTag, text.GetCleanedTag());
}
2 changes: 1 addition & 1 deletion src/DNTPersianUtils.Core/DNTPersianUtils.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>DNTPersianUtils.Core is a collection of Persian helper extension methods.</Description>
<VersionPrefix>6.5.0</VersionPrefix>
<VersionPrefix>6.6.0</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;net5.0;netstandard2.0;netstandard1.3;netstandard2.1;net46;</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">net5.0;netstandard2.1;netstandard1.3;netstandard2.0</TargetFrameworks>
Expand Down
26 changes: 23 additions & 3 deletions src/DNTPersianUtils.Core/SeoUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public static class SeoUtils
'«',
'»',
'|',
'+',
',',
'<',
'>'
Expand All @@ -92,6 +91,7 @@ public static class SeoUtils
/// </param>
/// <param name="convertToLower">آيا تبديل به حروف كوچك شود؟</param>
/// <param name="replacementChar">در اينجا ليستي از حروف خاص، با - جايگزين خواهند شد</param>
/// <param name="charsWhiteList">اين ليست اختياري از ليست جايگزين‌ها حذف مي‌شود</param>
/// <returns></returns>
public static string? GetPostSlug(
#if NET6_0 || NET7_0 || NET8_0 || NET9_0
Expand All @@ -100,7 +100,8 @@ public static class SeoUtils
this string? postTitle,
ISet<char>? replacements = null,
bool convertToLower = true,
char replacementChar = '-')
char replacementChar = '-',
params IEnumerable<char>? charsWhiteList)
{
if (string.IsNullOrWhiteSpace(postTitle))
{
Expand All @@ -109,6 +110,8 @@ public static class SeoUtils

replacements ??= PersianSlugReplacements;

ApplyWhiteList(replacements, charsWhiteList);

postTitle = postTitle.Trim();

if (convertToLower)
Expand Down Expand Up @@ -157,6 +160,7 @@ public static class SeoUtils
/// </param>
/// <param name="convertToLower">آيا تبديل به حروف كوچك شود؟</param>
/// <param name="replacementChar">در اينجا ليستي از حروف خاص، با _ جايگزين خواهند شد</param>
/// <param name="charsWhiteList">اين ليست اختياري از ليست جايگزين‌ها حذف مي‌شود</param>
/// <returns></returns>
public static string? GetCleanedTag(
#if NET6_0 || NET7_0 || NET8_0 || NET9_0
Expand All @@ -165,10 +169,26 @@ public static class SeoUtils
this string? tag,
ISet<char>? replacements = null,
bool convertToLower = false,
char replacementChar = '_')
char replacementChar = '_',
params IEnumerable<char>? charsWhiteList)
{
replacements ??= PersianTagReplacements;

ApplyWhiteList(replacements, charsWhiteList);

return tag.GetPostSlug(replacements, convertToLower, replacementChar);
}

private static void ApplyWhiteList(ISet<char> replacements, IEnumerable<char>? charsWhiteList)
{
if (charsWhiteList is null)
{
return;
}

foreach (var character in charsWhiteList)
{
replacements.Remove(character);
}
}
}

0 comments on commit ad684b2

Please sign in to comment.