|
| 1 | +// ------------------------------------------------------------------------ |
| 2 | +// This file is licensed to you under the MIT License. |
| 3 | +// ------------------------------------------------------------------------ |
| 4 | + |
| 5 | +using BlazorApps.Services; |
| 6 | +using Markdig; |
| 7 | +using Microsoft.AspNetCore.Components; |
| 8 | +using Microsoft.FluentUI.AspNetCore.Components; |
| 9 | +using Microsoft.JSInterop; |
| 10 | + |
| 11 | +namespace BlazorApps.Components; |
| 12 | + |
| 13 | +public partial class MarkdownSection : FluentComponentBase |
| 14 | +{ |
| 15 | + private IJSObjectReference _jsModule = default!; |
| 16 | + private bool _markdownChanged = false; |
| 17 | + private string? _content; |
| 18 | + private string? _fromAsset; |
| 19 | + |
| 20 | + [Inject] |
| 21 | + protected IJSRuntime JSRuntime { get; set; } = default!; |
| 22 | + |
| 23 | + [Inject] |
| 24 | + private IStaticAssetService StaticAssetService { get; set; } = default!; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Gets or sets the Markdown content |
| 28 | + /// </summary> |
| 29 | + [Parameter] |
| 30 | + public string? Content |
| 31 | + { |
| 32 | + get => _content; |
| 33 | + set |
| 34 | + { |
| 35 | + if (_content is not null && !_content.Equals(value)) |
| 36 | + { |
| 37 | + _markdownChanged = true; |
| 38 | + } |
| 39 | + _content = value; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets or sets asset to read the Markdown from |
| 45 | + /// </summary> |
| 46 | + [Parameter] |
| 47 | + public string? FromAsset |
| 48 | + { |
| 49 | + get => _fromAsset; |
| 50 | + set |
| 51 | + { |
| 52 | + if (_fromAsset is not null && !_fromAsset.Equals(value)) |
| 53 | + { |
| 54 | + _markdownChanged = true; |
| 55 | + } |
| 56 | + _fromAsset = value; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Parameter] |
| 61 | + public EventCallback OnContentConverted { get; set; } |
| 62 | + |
| 63 | + public MarkupString HtmlContent { get; private set; } |
| 64 | + |
| 65 | + protected override void OnInitialized() |
| 66 | + { |
| 67 | + if (Content is null && string.IsNullOrEmpty(FromAsset)) |
| 68 | + { |
| 69 | + throw new ArgumentException("You need to provide either Content or FromAsset parameter"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 74 | + { |
| 75 | + if (firstRender) |
| 76 | + { |
| 77 | + // import code for highlighting code blocks |
| 78 | + _jsModule = await JSRuntime.InvokeAsync<IJSObjectReference>("import", |
| 79 | + "./Components/MarkdownSection.razor.js"); |
| 80 | + } |
| 81 | + |
| 82 | + if (firstRender || _markdownChanged) |
| 83 | + { |
| 84 | + _markdownChanged = false; |
| 85 | + |
| 86 | + // create markup from markdown source |
| 87 | + HtmlContent = await MarkdownToMarkupStringAsync(); |
| 88 | + StateHasChanged(); |
| 89 | + |
| 90 | + // notify that content converted from markdown |
| 91 | + if (OnContentConverted.HasDelegate) |
| 92 | + { |
| 93 | + await OnContentConverted.InvokeAsync(); |
| 94 | + } |
| 95 | + await _jsModule.InvokeVoidAsync("highlight"); |
| 96 | + await _jsModule.InvokeVoidAsync("addCopyButton"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Converts markdown, provided in Content or from markdown file stored as a static asset, to MarkupString for rendering. |
| 102 | + /// </summary> |
| 103 | + /// <returns>MarkupString</returns> |
| 104 | + private async Task<MarkupString> MarkdownToMarkupStringAsync() |
| 105 | + { |
| 106 | + string? markdown; |
| 107 | + if (string.IsNullOrEmpty(FromAsset)) |
| 108 | + { |
| 109 | + markdown = Content; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + markdown = await StaticAssetService.GetAsync(FromAsset); |
| 114 | + } |
| 115 | + |
| 116 | + return ConvertToMarkupString(markdown); |
| 117 | + } |
| 118 | + private static MarkupString ConvertToMarkupString(string? value) |
| 119 | + { |
| 120 | + if (!string.IsNullOrWhiteSpace(value)) |
| 121 | + { |
| 122 | + var builder = new MarkdownPipelineBuilder() |
| 123 | + .UseAdvancedExtensions() |
| 124 | + .Use<MarkdownSectionPreCodeExtension>(); |
| 125 | + |
| 126 | + var pipeline = builder.Build(); |
| 127 | + |
| 128 | + // Convert markdown string to HTML |
| 129 | + var html = Markdown.ToHtml(value, pipeline); |
| 130 | + |
| 131 | + // Return sanitized HTML as a MarkupString that Blazor can render |
| 132 | + return new MarkupString(html); |
| 133 | + } |
| 134 | + |
| 135 | + return new MarkupString(); |
| 136 | + } |
| 137 | +} |
0 commit comments