fix: fix markdown formatting inside templates

This commit is contained in:
Oliver Booth 2023-08-11 21:51:16 +01:00
parent 6f7fa67135
commit 47b648f327
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
3 changed files with 42 additions and 13 deletions

View File

@ -0,0 +1,38 @@
using Markdig;
using SmartFormat.Core.Extensions;
namespace OliverBooth.Formatting;
/// <summary>
/// Represents a SmartFormat formatter that formats markdown.
/// </summary>
internal sealed class MarkdownFormatter : IFormatter
{
private readonly IServiceProvider _serviceProvider;
/// <summary>
/// Initializes a new instance of the <see cref="MarkdownFormatter" /> class.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
public MarkdownFormatter(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
/// <inheritdoc />
public bool CanAutoDetect { get; set; } = true;
/// <inheritdoc />
public string Name { get; set; } = "markdown";
/// <inheritdoc />
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
if (formattingInfo.CurrentValue is not string value)
return false;
var pipeline = _serviceProvider.GetService<MarkdownPipeline>();
formattingInfo.Write(Markdig.Markdown.ToHtml(value, pipeline));
return true;
}
}

View File

@ -4,7 +4,6 @@ using Markdig;
using NLog;
using NLog.Extensions.Logging;
using OliverBooth.Data;
using OliverBooth.Markdown;
using OliverBooth.Markdown.Template;
using OliverBooth.Markdown.Timestamp;
using OliverBooth.Middleware;

View File

@ -32,6 +32,7 @@ public sealed class TemplateService
_formatter.AddExtensions(new DefaultSource());
_formatter.AddExtensions(new ReflectionSource());
_formatter.AddExtensions(new DateFormatter());
_formatter.AddExtensions(new MarkdownFormatter(serviceProvider));
_serviceProvider = serviceProvider;
_webContextFactory = webContextFactory;
@ -51,7 +52,6 @@ public sealed class TemplateService
public string RenderTemplate(TemplateInline templateInline)
{
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
MarkdownPipeline? markdownPipeline = _serviceProvider.GetService<MarkdownPipeline>();
using WebContext webContext = _webContextFactory.CreateDbContext();
ArticleTemplate? template = webContext.ArticleTemplates.Find(templateInline.Name);
@ -60,21 +60,13 @@ public sealed class TemplateService
return $"{{{{{templateInline.Name}}}}}";
}
string[] arguments = templateInline.ArgumentList.ToArray();
for (var index = 0; index < arguments.Length; index++)
{
MarkdownDocument document = Markdig.Markdown.Parse(arguments[index], markdownPipeline);
string result = document.ToHtml(markdownPipeline);
arguments[index] = result;
}
Span<byte> randomBytes = stackalloc byte[20];
Random.NextBytes(randomBytes);
var formatted = new
{
ArgumentList = arguments,
ArgumentString = string.Join("|", arguments),
templateInline.ArgumentList,
templateInline.ArgumentString,
templateInline.Params,
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
@ -82,7 +74,7 @@ public sealed class TemplateService
try
{
return Markdig.Markdown.ToHtml(_formatter.Format(template.FormatString, formatted));
return _formatter.Format(template.FormatString, formatted);
}
catch
{