@using Markdig
@using AngleSharp
@if (!string.IsNullOrEmpty(Style))
{
<div class="markdown-body" style="@Style"> @((MarkupString)_htmlContent)</div>
}
else
{
<div class="markdown-body"> @((MarkupString)_htmlContent)</div>
}
@code {
[Parameter]
public string Markdown { get; set; }
[Parameter]
public string? Style { get; set; }
[Inject]
IJSRuntime JsRuntime { get; set; }
private string _htmlContent = "";
protected override async Task OnParametersSetAsync()
{
var pipeline = new MarkdownPipelineBuilder()
.UseAutoLinks()
.UsePipeTables()
.UseTaskLists()
.UseEmphasisExtras()
.UseFooters()
.UseCitations()
.UseMathematics()
.UseAutoIdentifiers()
.Build();
var html = Markdig.Markdown.ToHtml(Markdown ?? string.Empty, pipeline);
var context = BrowsingContext.New(Configuration.Default);
var document = await context.OpenAsync(y => y.Content(html));
var links = document.GetElementsByTagName("a");
links.ForEach(y =>
{
var href = y.GetAttribute("href");
if (href != null && !href.StartsWith("javascript", StringComparison.CurrentCultureIgnoreCase))
{
y.SetAttribute("target", "_blank");
}
});
var images = document.GetElementsByTagName("img");
images.ForEach(y =>
{
var src = y.GetAttribute("src");
if (src == $"{Program.CdnBaseAddress}/core/loaders/tail-spin.svg") return;
y.SetAttribute("data-src", src ?? $"{Program.CdnBaseAddress}/core/images/notfound.png");
y.SetAttribute("class","lazy");
y.SetAttribute("src", $"{Program.CdnBaseAddress}/core/loaders/tail-spin.svg");
});
_htmlContent = document.Body?.InnerHtml ?? "";
//await JsRuntime.InvokeVoidAsync("Prism.highlightAll");
await base.OnParametersSetAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await JsRuntime.InvokeVoidAsync("showLazyImage");
await JsRuntime.InvokeVoidAsync("Prism.highlightAll");
await base.OnAfterRenderAsync(firstRender);
}
}