Make Liquid TextEncoder configurable (#4380)
This commit is contained in:
parent
6329a3b805
commit
9d4add767b
|
|
@ -1,3 +1,5 @@
|
|||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using Elsa.EntityFrameworkCore.Extensions;
|
||||
using Elsa.EntityFrameworkCore.Modules.Identity;
|
||||
using Elsa.EntityFrameworkCore.Modules.Management;
|
||||
|
|
@ -9,6 +11,7 @@ using Elsa.MongoDb.Modules.Identity;
|
|||
using Elsa.MongoDb.Modules.Management;
|
||||
using Elsa.MongoDb.Modules.Runtime;
|
||||
using Elsa.WorkflowServer.Web;
|
||||
using Fluid;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Proto.Persistence.Sqlite;
|
||||
using Proto.Persistence.SqlServer;
|
||||
|
|
@ -115,7 +118,7 @@ services
|
|||
.UseWorkflowsApi(api => api.AddFastEndpointsAssembly<Program>())
|
||||
.UseRealTimeWorkflows()
|
||||
.UseJavaScript(js => js.JintOptions = options => options.AllowClrAccess = true)
|
||||
.UseLiquid()
|
||||
.UseLiquid(liquid => liquid.FluidOptions = options => options.Encoder = NullEncoder.Default)
|
||||
.UseHttp(http => http.HttpEndpointAuthorizationHandler = sp => sp.GetRequiredService<AllowAnonymousHttpEndpointAuthorizationHandler>())
|
||||
.UseEmail(email => email.ConfigureOptions = options => configuration.GetSection("Smtp").Bind(options));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ using Elsa.Liquid.Contracts;
|
|||
using Elsa.Liquid.Expressions;
|
||||
using Elsa.Liquid.Filters;
|
||||
using Elsa.Liquid.Handlers;
|
||||
using Elsa.Liquid.Options;
|
||||
using Elsa.Liquid.Providers;
|
||||
using Elsa.Liquid.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
|
@ -28,9 +29,16 @@ public class LiquidFeature : FeatureBase
|
|||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the Fluid options.
|
||||
/// </summary>
|
||||
public Action<FluidOptions> FluidOptions { get; set; } = _ => { };
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Configure()
|
||||
public override void Apply()
|
||||
{
|
||||
Services.Configure(FluidOptions);
|
||||
|
||||
Services
|
||||
.AddHandlersFrom<ConfigureLiquidEngine>()
|
||||
.AddSingleton<IExpressionSyntaxProvider, LiquidExpressionSyntaxProvider>()
|
||||
|
|
|
|||
|
|
@ -1,10 +1,31 @@
|
|||
using System.Text.Encodings.Web;
|
||||
using Elsa.Liquid.Services;
|
||||
using Fluid;
|
||||
|
||||
namespace Elsa.Liquid.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Options for configuring the Liquid templating engine.
|
||||
/// </summary>
|
||||
public class FluidOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// A dictionary of filter registrations.
|
||||
/// </summary>
|
||||
public Dictionary<string, Type> FilterRegistrations { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// A list of parser configurations.
|
||||
/// </summary>
|
||||
public IList<Action<LiquidParser>> ParserConfiguration { get; } = new List<Action<LiquidParser>>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether to allow access to the configuration object.
|
||||
/// </summary>
|
||||
public bool AllowConfigurationAccess { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default encoder to use when rendering a template.
|
||||
/// </summary>
|
||||
public TextEncoder Encoder { get; set; } = NullEncoder.Default;
|
||||
}
|
||||
|
|
@ -4,8 +4,12 @@ using Microsoft.Extensions.Options;
|
|||
|
||||
namespace Elsa.Liquid.Services;
|
||||
|
||||
/// <summary>
|
||||
/// A parser for the Liquid templating engine.
|
||||
/// </summary>
|
||||
public class LiquidParser : FluidParser
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public LiquidParser(IOptions<FluidOptions> options)
|
||||
{
|
||||
foreach (var configuration in options.Value.ParserConfiguration)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using System.Text.Encodings.Web;
|
||||
using Elsa.Expressions.Models;
|
||||
using Elsa.Expressions.Models;
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Liquid.Contracts;
|
||||
using Elsa.Liquid.Notifications;
|
||||
|
|
@ -22,7 +21,7 @@ public class LiquidTemplateManager : ILiquidTemplateManager
|
|||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public LiquidTemplateManager(LiquidParser parser, IMemoryCache memoryCache, INotificationSender notificationSender, IOptions<FluidOptions> options, IServiceProvider serviceProvider)
|
||||
public LiquidTemplateManager(LiquidParser parser, IMemoryCache memoryCache, INotificationSender notificationSender, IOptions<FluidOptions> options)
|
||||
{
|
||||
_parser = parser;
|
||||
_memoryCache = memoryCache;
|
||||
|
|
@ -38,9 +37,10 @@ public class LiquidTemplateManager : ILiquidTemplateManager
|
|||
|
||||
var result = GetCachedTemplate(template);
|
||||
var templateContext = await CreateTemplateContextAsync(expressionExecutionContext, cancellationToken);
|
||||
var encoder = _options.Encoder;
|
||||
templateContext.AddFilters(_options, expressionExecutionContext.ServiceProvider);
|
||||
|
||||
return await result.RenderAsync(templateContext, HtmlEncoder.Default);
|
||||
return await result.RenderAsync(templateContext, encoder);
|
||||
}
|
||||
|
||||
private IFluidTemplate GetCachedTemplate(string source)
|
||||
|
|
|
|||
Loading…
Reference in a new issue