diff --git a/OliverBooth/Formatting/MarkdownFormatter.cs b/OliverBooth/Formatting/MarkdownFormatter.cs new file mode 100644 index 0000000..860188e --- /dev/null +++ b/OliverBooth/Formatting/MarkdownFormatter.cs @@ -0,0 +1,38 @@ +using Markdig; +using SmartFormat.Core.Extensions; + +namespace OliverBooth.Formatting; + +/// +/// Represents a SmartFormat formatter that formats markdown. +/// +internal sealed class MarkdownFormatter : IFormatter +{ + private readonly IServiceProvider _serviceProvider; + + /// + /// Initializes a new instance of the class. + /// + /// The . + public MarkdownFormatter(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + /// + public bool CanAutoDetect { get; set; } = true; + + /// + public string Name { get; set; } = "markdown"; + + /// + public bool TryEvaluateFormat(IFormattingInfo formattingInfo) + { + if (formattingInfo.CurrentValue is not string value) + return false; + + var pipeline = _serviceProvider.GetService(); + formattingInfo.Write(Markdig.Markdown.ToHtml(value, pipeline)); + return true; + } +} diff --git a/OliverBooth/Program.cs b/OliverBooth/Program.cs index f8b1c10..53f30b4 100644 --- a/OliverBooth/Program.cs +++ b/OliverBooth/Program.cs @@ -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; diff --git a/OliverBooth/Services/TemplateService.cs b/OliverBooth/Services/TemplateService.cs index e678c9d..1d93466 100644 --- a/OliverBooth/Services/TemplateService.cs +++ b/OliverBooth/Services/TemplateService.cs @@ -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(); 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 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 {