2023-08-11 21:51:16 +01:00
|
|
|
using Markdig;
|
2024-05-05 02:26:50 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-08-11 21:51:16 +01:00
|
|
|
using SmartFormat.Core.Extensions;
|
|
|
|
|
2024-05-05 02:26:50 +01:00
|
|
|
namespace OliverBooth.Extensions.SmartFormat;
|
2023-08-11 21:51:16 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a SmartFormat formatter that formats markdown.
|
|
|
|
/// </summary>
|
2023-08-13 13:27:44 +01:00
|
|
|
public sealed class MarkdownFormatter : IFormatter
|
2023-08-11 21:51:16 +01:00
|
|
|
{
|
|
|
|
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>();
|
2024-11-07 14:49:13 +00:00
|
|
|
formattingInfo.Write(Markdown.ToHtml(value, pipeline));
|
2023-08-11 21:51:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|