fix: fix markdown formatting inside templates
This commit is contained in:
parent
6f7fa67135
commit
47b648f327
38
OliverBooth/Formatting/MarkdownFormatter.cs
Normal file
38
OliverBooth/Formatting/MarkdownFormatter.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,6 @@ using Markdig;
|
|||||||
using NLog;
|
using NLog;
|
||||||
using NLog.Extensions.Logging;
|
using NLog.Extensions.Logging;
|
||||||
using OliverBooth.Data;
|
using OliverBooth.Data;
|
||||||
using OliverBooth.Markdown;
|
|
||||||
using OliverBooth.Markdown.Template;
|
using OliverBooth.Markdown.Template;
|
||||||
using OliverBooth.Markdown.Timestamp;
|
using OliverBooth.Markdown.Timestamp;
|
||||||
using OliverBooth.Middleware;
|
using OliverBooth.Middleware;
|
||||||
|
@ -32,6 +32,7 @@ public sealed class TemplateService
|
|||||||
_formatter.AddExtensions(new DefaultSource());
|
_formatter.AddExtensions(new DefaultSource());
|
||||||
_formatter.AddExtensions(new ReflectionSource());
|
_formatter.AddExtensions(new ReflectionSource());
|
||||||
_formatter.AddExtensions(new DateFormatter());
|
_formatter.AddExtensions(new DateFormatter());
|
||||||
|
_formatter.AddExtensions(new MarkdownFormatter(serviceProvider));
|
||||||
|
|
||||||
_serviceProvider = serviceProvider;
|
_serviceProvider = serviceProvider;
|
||||||
_webContextFactory = webContextFactory;
|
_webContextFactory = webContextFactory;
|
||||||
@ -51,7 +52,6 @@ public sealed class TemplateService
|
|||||||
public string RenderTemplate(TemplateInline templateInline)
|
public string RenderTemplate(TemplateInline templateInline)
|
||||||
{
|
{
|
||||||
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
|
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
|
||||||
MarkdownPipeline? markdownPipeline = _serviceProvider.GetService<MarkdownPipeline>();
|
|
||||||
|
|
||||||
using WebContext webContext = _webContextFactory.CreateDbContext();
|
using WebContext webContext = _webContextFactory.CreateDbContext();
|
||||||
ArticleTemplate? template = webContext.ArticleTemplates.Find(templateInline.Name);
|
ArticleTemplate? template = webContext.ArticleTemplates.Find(templateInline.Name);
|
||||||
@ -60,21 +60,13 @@ public sealed class TemplateService
|
|||||||
return $"{{{{{templateInline.Name}}}}}";
|
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];
|
Span<byte> randomBytes = stackalloc byte[20];
|
||||||
Random.NextBytes(randomBytes);
|
Random.NextBytes(randomBytes);
|
||||||
|
|
||||||
var formatted = new
|
var formatted = new
|
||||||
{
|
{
|
||||||
ArgumentList = arguments,
|
templateInline.ArgumentList,
|
||||||
ArgumentString = string.Join("|", arguments),
|
templateInline.ArgumentString,
|
||||||
templateInline.Params,
|
templateInline.Params,
|
||||||
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
|
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
|
||||||
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
|
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
|
||||||
@ -82,7 +74,7 @@ public sealed class TemplateService
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Markdig.Markdown.ToHtml(_formatter.Format(template.FormatString, formatted));
|
return _formatter.Format(template.FormatString, formatted);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user