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