oliverbooth.dev/OliverBooth.Common/Formatting/MarkdownFormatter.cs

42 lines
1.2 KiB
C#
Raw Normal View History

using Markdig;
using Microsoft.Extensions.DependencyInjection;
using SmartFormat.Core.Extensions;
namespace OliverBooth.Common.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)
2024-02-25 14:11:33 +00:00
{
return false;
2024-02-25 14:11:33 +00:00
}
var pipeline = _serviceProvider.GetService<MarkdownPipeline>();
formattingInfo.Write(Markdig.Markdown.ToHtml(value, pipeline));
return true;
}
}