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,13 +7,12 @@ 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
19
14
[ Inject ] private IJSRuntime _js { get ; set ; } = default ! ;
15
+ [ Inject ] private BitMarkdownService _markdownService { get ; set ; } = default ! ;
20
16
21
17
22
18
@@ -32,90 +28,15 @@ public partial class BitMarkdownViewer : BitComponentBase
32
28
33
29
protected override async Task OnInitializedAsync ( )
34
30
{
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
- }
31
+ _html = await _markdownService . Parse ( Markdown , _cts . Token ) ;
32
+
33
+ StateHasChanged ( ) ;
60
34
61
35
await base . OnInitializedAsync ( ) ;
62
36
}
63
37
64
38
65
39
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
40
private async Task OnMarkdownSet ( )
120
41
{
121
42
if ( IsRendered is false ) return ;
0 commit comments