Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Fluid;
using Fluid.Values;
using OrchardCore.Localization;
using OrchardCore.Modules.Services;

namespace OrchardCore.Liquid.Filters;
Expand All @@ -16,7 +17,10 @@ public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments argu
{
var transliterateArg = arguments["transliterate"];
var transliterate = transliterateArg.IsNil() || transliterateArg.ToBooleanValue();
var slug = transliterate
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the two filters separate. I looked at the implementation of the service and the slugification logic is not impacted by the transliteration, these are two distinct operations. We should just do | transliterate | slugify

    public string Slugify(string text, bool transliterate)
    {
        if (transliterate && !string.IsNullOrEmpty(text))
        {
            text = text.Transliterate();
        }

        return Slugify(text);
    }

For the same reason this was extracted from the service in this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as I mentioned in my above comment

? _slugService.SlugifyAndTransliterate(input.ToStringValue())
: _slugService.Slugify(input.ToStringValue());

return new StringValue(_slugService.Slugify(input.ToStringValue(), transliterate));
return new StringValue(slug);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Module.Targets\OrchardCore.Module.Targets.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Shortcodes.Abstractions\OrchardCore.Shortcodes.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ResourceManagement\OrchardCore.ResourceManagement.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Abstractions\OrchardCore.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public override void ConfigureServices(IServiceCollection services)
.AddLiquidFilter<LocalTimeZoneFilter>("local")
.AddLiquidFilter<UtcTimeZoneFilter>("utc")
.AddLiquidFilter<SlugifyFilter>("slugify")
.AddLiquidFilter<TransliterateFilter>("transliterate")
Comment thread
sebastienros marked this conversation as resolved.
.AddLiquidFilter<LiquidFilter>("liquid")
.AddLiquidFilter<ContentUrlFilter>("href")
.AddLiquidFilter<AbsoluteUrlFilter>("absolute_url")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AnyAscii;
using Fluid;
using Fluid.Values;
using OrchardCore.Liquid;

namespace OrchardCore.Liquid.Filters;
namespace OrchardCore.Localization.Liquid.Filters;

public class TransliterateFilter : ILiquidFilter
{
Expand Down
4 changes: 4 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Localization/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Microsoft.Extensions.Options;
using OrchardCore.Admin.Models;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Liquid;
using OrchardCore.Localization.Drivers;
using OrchardCore.Localization.Liquid.Filters;
using OrchardCore.Localization.Models;
using OrchardCore.Localization.Services;
using OrchardCore.Modules;
Expand Down Expand Up @@ -34,6 +36,8 @@ public override void ConfigureServices(IServiceCollection services)
AddDataAnnotationsPortableObjectLocalization();

services.Replace(ServiceDescriptor.Singleton<ILocalizationFileLocationProvider, ModularPoFileLocationProvider>());

services.AddLiquidFilter<TransliterateFilter>("transliterate");
Comment thread
sebastienros marked this conversation as resolved.
}

/// <inheritdocs />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ResourceManagement\OrchardCore.ResourceManagement.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Shortcodes.Abstractions\OrchardCore.Shortcodes.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.XmlRpc.Abstractions\OrchardCore.XmlRpc.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Abstractions\OrchardCore.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Options;
using OrchardCore.Localization;
using OrchardCore.Modules.Services;

namespace OrchardCore.Media.Services;
Expand All @@ -18,11 +19,15 @@ public SlugifyMediaNameNormalizerService(

public string NormalizeFolderName(string folderName)
{
return _slugService.Slugify(folderName, _options.Transliterate);
return _options.Transliterate
? _slugService.SlugifyAndTransliterate(folderName)
: _slugService.Slugify(folderName);
}

public string NormalizeFileName(string fileName)
{
return _slugService.Slugify(Path.GetFileNameWithoutExtension(fileName), _options.Transliterate) + Path.GetExtension(fileName);
return _options.Transliterate
Comment thread
hishamco marked this conversation as resolved.
? _slugService.SlugifyAndTransliterate(Path.GetFileNameWithoutExtension(fileName))
: _slugService.Slugify(Path.GetFileNameWithoutExtension(fileName)) + Path.GetExtension(fileName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace OrchardCore.Localization;

/// <summary>
///
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Converts the non-Latin characters to their ASCII equivalents.
/// </summary>
public static string Transliterate(this string text) => AnyAscii.Transliteration.Transliterate(text);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using OrchardCore.Modules.Services;

namespace OrchardCore.Localization;

public static class TransliterationSlugServiceExtensions
{
/// <summary>
/// Converts the specified text to a URL-friendly slug after applying transliteration.
/// </summary>
/// <param name="slugService">The slug service used to generate the slug from the transliterated text.</param>
/// <param name="text">The input text to transliterate and convert to a slug. Cannot be null or empty.</param>
/// <returns>A slugified string representing the transliterated input text.</returns>
public static string SlugifyAndTransliterate(this ISlugService slugService, string text)
{
ArgumentException.ThrowIfNullOrEmpty(text);

return slugService.Slugify(text.Transliterate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ public interface ISlugService
/// <returns>The slug created from the input text.</returns>
string Slugify(string text);

/// <summary>
/// Transforms specified text to the form suitable for URL slugs,
/// optionally transliterating non-Latin characters to their ASCII equivalents first.
/// </summary>
/// <param name="text">The text to transform.</param>
/// <param name="transliterate">Whether to transliterate non-Latin characters before slugifying.</param>
/// <returns>The slug created from the input text.</returns>
string Slugify(string text, bool transliterate);

/// <summary>
/// Transforms specified text to a custom form generally not suitable for URL slugs.
/// Allows you to use a specified separator char.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AnyAscii" />
<PackageReference Include="JsonPath.Net" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" />
<PackageReference Include="ZString" />
Expand Down
11 changes: 0 additions & 11 deletions src/OrchardCore/OrchardCore/Modules/Services/SlugService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Globalization;
using System.Text;
using AnyAscii;
using Cysharp.Text;

namespace OrchardCore.Modules.Services;
Expand All @@ -10,16 +9,6 @@ public class SlugService : ISlugService
private const char Hyphen = '-';
private const int MaxLength = 1000;

public string Slugify(string text, bool transliterate)
{
if (transliterate && !string.IsNullOrEmpty(text))
{
text = text.Transliterate();
}

return Slugify(text);
}

public string Slugify(string text, char separator)
{
throw new NotImplementedException();
Expand Down
3 changes: 1 addition & 2 deletions src/OrchardCore/OrchardCore/OrchardCore.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- NuGet properties-->
Expand All @@ -14,7 +14,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AnyAscii" />
<PackageReference Include="ncrontab" />
<PackageReference Include="NodaTime" />
</ItemGroup>
Expand Down
10 changes: 2 additions & 8 deletions test/OrchardCore.Tests/Tokens.Content/SlugServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using OrchardCore.Localization;
using OrchardCore.Modules.Services;

namespace OrchardCore.Tests.Tokens.Content;
Expand Down Expand Up @@ -81,14 +82,7 @@ public void ShouldPreserveNonLatinCharacters(string input, string expected)
[Fact]
public void ShouldTransliterateWhenRequested()
{
var slug = _slugService.Slugify("Æneid", transliterate: true);
var slug = _slugService.SlugifyAndTransliterate("Æneid");
Assert.Equal("aeneid", slug);
}

[Fact]
public void ShouldNotTransliterateWhenDisabled()
{
var slug = _slugService.Slugify("Æneid", transliterate: false);
Assert.Equal("æneid", slug);
}
}
Loading