1
- using System . Globalization ;
2
- using Jint ;
3
-
4
- namespace Bit . BlazorUI ;
1
+ namespace Bit . BlazorUI ;
5
2
6
3
/// <summary>
7
4
/// BitMarkdownViewer is a SEO friendly Blazor wrapper around the famous markedjs library.
@@ -10,9 +7,7 @@ namespace Bit.BlazorUI;
10
7
public partial class BitMarkdownViewer : BitComponentBase
11
8
{
12
9
private string ? _html ;
13
- private static string ? _markedScriptText ;
14
10
private readonly CancellationTokenSource _cts = new ( ) ;
15
- private static readonly SemaphoreSlim _markedScriptReadTextSemaphore = new ( 1 , 1 ) ;
16
11
17
12
18
13
@@ -36,7 +31,11 @@ protected override async Task OnInitializedAsync()
36
31
{
37
32
try
38
33
{
39
- await RunJint ( ) ;
34
+ await Task . Run ( async ( ) =>
35
+ {
36
+ _html = await BitMarked . Parse ( Markdown , _cts . Token ) ;
37
+ await InvokeAsync ( StateHasChanged ) ;
38
+ } , _cts . Token ) ;
40
39
}
41
40
catch ( FileNotFoundException ex ) when ( ex . FileName ? . StartsWith ( "Jint" ) is true )
42
41
{
@@ -63,59 +62,6 @@ protected override async Task OnInitializedAsync()
63
62
64
63
65
64
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
65
private async Task OnMarkdownSet ( )
120
66
{
121
67
if ( IsRendered is false ) return ;
0 commit comments