|
| 1 | +using System.Globalization; |
| 2 | +using Jint; |
| 3 | + |
| 4 | +namespace Bit.BlazorUI; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// BitMarkdownViewer is a SEO friendly Blazor wrapper around the famous markedjs library. |
| 8 | +/// <see href="https://github.com/markedjs/marked"/> |
| 9 | +/// </summary> |
| 10 | +public partial class BitMarkdownViewer : BitComponentBase |
| 11 | +{ |
| 12 | + private string? _html; |
| 13 | + private static string? _markedScriptText; |
| 14 | + private readonly CancellationTokenSource _cts = new(); |
| 15 | + private static readonly SemaphoreSlim _markedScriptReadTextSemaphore = new(1, 1); |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + [Inject] private IJSRuntime _js { get; set; } = default!; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The Markdown string value to render as an html element. |
| 25 | + /// </summary> |
| 26 | + [Parameter, CallOnSet(nameof(OnMarkdownSet))] |
| 27 | + public string? Markdown { get; set; } |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + protected override string RootElementClass => "bit-mdv"; |
| 32 | + |
| 33 | + protected override async Task OnInitializedAsync() |
| 34 | + { |
| 35 | + if (_js.IsRuntimeInvalid()) // prerendering |
| 36 | + { |
| 37 | + try |
| 38 | + { |
| 39 | + await RunJint(); |
| 40 | + } |
| 41 | + catch (FileNotFoundException ex) when (ex.FileName?.StartsWith("Jint") is true) |
| 42 | + { |
| 43 | + Console.Error.WriteLine("Please install `Jint` nuget package on the server project."); |
| 44 | + } |
| 45 | + catch (Exception ex) |
| 46 | + { |
| 47 | + Console.Error.WriteLine(ex.Message); |
| 48 | + } |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + var scriptPath = "_content/Bit.BlazorUI.Extras/marked/marked-15.0.7.js"; |
| 53 | + if ((await _js.BitMarkdownViewerCheckScriptLoaded(scriptPath)) is false) |
| 54 | + { |
| 55 | + await _js.BitExtrasInitScripts([scriptPath]); |
| 56 | + } |
| 57 | + |
| 58 | + await ParseAndRender(); |
| 59 | + } |
| 60 | + |
| 61 | + await base.OnInitializedAsync(); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + private async Task RunJint() |
| 67 | + { |
| 68 | + if (Markdown.HasNoValue()) return; |
| 69 | + |
| 70 | + await Task.Run(async () => |
| 71 | + { |
| 72 | + await ReadMarkedScriptText(); |
| 73 | + if (_markedScriptText.HasNoValue()) return; |
| 74 | + |
| 75 | + using var engine = new Engine(options => |
| 76 | + { |
| 77 | + options.Strict(); |
| 78 | + options.CancellationToken(_cts.Token); |
| 79 | + options.Culture(CultureInfo.CurrentUICulture); |
| 80 | + }).Execute(_markedScriptText!); |
| 81 | + |
| 82 | + var fn = engine.Evaluate("marked.parse").AsFunctionInstance(); |
| 83 | + |
| 84 | + _html = fn.Call(Markdown).AsString(); |
| 85 | + |
| 86 | + await InvokeAsync(StateHasChanged); |
| 87 | + }, _cts.Token); |
| 88 | + } |
| 89 | + |
| 90 | + private async Task<string> ReadMarkedScriptText() |
| 91 | + { |
| 92 | + if (_markedScriptText is not null) return _markedScriptText; |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + await _markedScriptReadTextSemaphore.WaitAsync(_cts.Token); |
| 97 | + if (_markedScriptText is not null) return _markedScriptText; |
| 98 | + |
| 99 | + var scriptPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "_content", "Bit.BlazorUI.Extras", "marked", "marked-15.0.7.js"); |
| 100 | + |
| 101 | + if (File.Exists(scriptPath) is false) |
| 102 | + { |
| 103 | + scriptPath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "marked", "marked-15.0.7.js"); |
| 104 | + } |
| 105 | + |
| 106 | + if (File.Exists(scriptPath) is false) |
| 107 | + { |
| 108 | + return _markedScriptText = string.Empty; |
| 109 | + } |
| 110 | + |
| 111 | + return _markedScriptText = await File.ReadAllTextAsync(scriptPath); |
| 112 | + } |
| 113 | + finally |
| 114 | + { |
| 115 | + _markedScriptReadTextSemaphore.Release(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private async Task OnMarkdownSet() |
| 120 | + { |
| 121 | + if (IsRendered is false) return; |
| 122 | + |
| 123 | + await ParseAndRender(); |
| 124 | + } |
| 125 | + |
| 126 | + private async Task ParseAndRender() |
| 127 | + { |
| 128 | + if (Markdown.HasNoValue()) return; |
| 129 | + |
| 130 | + _html = await _js.BitMarkdownViewerParse(Markdown!); |
| 131 | + |
| 132 | + StateHasChanged(); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + protected override async ValueTask DisposeAsync(bool disposing) |
| 138 | + { |
| 139 | + if (IsDisposed || disposing is false) return; |
| 140 | + |
| 141 | + _cts.Cancel(); |
| 142 | + _cts.Dispose(); |
| 143 | + |
| 144 | + await base.DisposeAsync(disposing); |
| 145 | + } |
| 146 | +} |
0 commit comments