diff --git a/OliverBooth/Markdown/Template/TemplateInlineParser.cs b/OliverBooth/Markdown/Template/TemplateInlineParser.cs index 55874d5..4c3d7b5 100644 --- a/OliverBooth/Markdown/Template/TemplateInlineParser.cs +++ b/OliverBooth/Markdown/Template/TemplateInlineParser.cs @@ -112,7 +112,7 @@ public sealed class TemplateInlineParser : InlineParser out bool hasValue) { var isEscaped = false; - + int startIndex = index; for (; index < argumentSpan.Length; index++) { diff --git a/OliverBooth/Services/TemplateService.cs b/OliverBooth/Services/TemplateService.cs index 1a1b2b6..f0bef6e 100644 --- a/OliverBooth/Services/TemplateService.cs +++ b/OliverBooth/Services/TemplateService.cs @@ -1,7 +1,9 @@ +using System.Buffers.Binary; +using Markdig; +using Markdig.Syntax; using Microsoft.EntityFrameworkCore; using OliverBooth.Data; using OliverBooth.Data.Web; -using OliverBooth.Markdown; using OliverBooth.Markdown.Template; using SmartFormat; using SmartFormat.Extensions; @@ -13,20 +15,24 @@ namespace OliverBooth.Services; /// public sealed class TemplateService { + private static readonly Random Random = new(); + private readonly IServiceProvider _serviceProvider; private readonly IDbContextFactory _webContextFactory; private readonly SmartFormatter _formatter; /// /// Initializes a new instance of the class. /// + /// The . /// The factory. - public TemplateService(IDbContextFactory webContextFactory) + public TemplateService(IServiceProvider serviceProvider, IDbContextFactory webContextFactory) { _formatter = Smart.CreateDefaultSmartFormat(); _formatter.AddExtensions(new DefaultSource()); _formatter.AddExtensions(new ReflectionSource()); _formatter.AddExtensions(new DateFormatter()); - + + _serviceProvider = serviceProvider; _webContextFactory = webContextFactory; Current = this; } @@ -44,6 +50,8 @@ 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); if (template is null) @@ -51,11 +59,24 @@ 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 { - templateInline.ArgumentList, - templateInline.ArgumentString, + ArgumentList = arguments, + ArgumentString = string.Join("|", arguments), templateInline.Params, + RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]), + RandomGuid = new Guid(randomBytes[4..]).ToString("N"), }; try