oliverbooth.dev/OliverBooth/Formatting/MarkdownFormatter.cs
Oliver Booth 0a9c2e82d5
refactor: combine sites into one
CORS was "cors"ing some issues (heh).

But also it is easier to maintain this way. Development was made much more difficult when I separated it. Combining it all also improves SEO
2023-08-13 17:34:38 +01:00

39 lines
1.1 KiB
C#

using Markdig;
using SmartFormat.Core.Extensions;
namespace OliverBooth.Formatting;
/// <summary>
/// Represents a SmartFormat formatter that formats markdown.
/// </summary>
public 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;
}
}