Skip to content

Commit 361958b

Browse files
committed
add BitMarked service class to BlazorUI bitfoundation#10221
1 parent 10e1826 commit 361958b

File tree

2 files changed

+70
-60
lines changed

2 files changed

+70
-60
lines changed

src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewer.razor.cs

+6-60
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System.Globalization;
2-
using Jint;
3-
4-
namespace Bit.BlazorUI;
1+
namespace Bit.BlazorUI;
52

63
/// <summary>
74
/// BitMarkdownViewer is a SEO friendly Blazor wrapper around the famous markedjs library.
@@ -10,9 +7,7 @@ namespace Bit.BlazorUI;
107
public partial class BitMarkdownViewer : BitComponentBase
118
{
129
private string? _html;
13-
private static string? _markedScriptText;
1410
private readonly CancellationTokenSource _cts = new();
15-
private static readonly SemaphoreSlim _markedScriptReadTextSemaphore = new(1, 1);
1611

1712

1813

@@ -36,7 +31,11 @@ protected override async Task OnInitializedAsync()
3631
{
3732
try
3833
{
39-
await RunJint();
34+
await Task.Run(async () =>
35+
{
36+
_html = await BitMarked.Parse(Markdown, _cts.Token);
37+
await InvokeAsync(StateHasChanged);
38+
}, _cts.Token);
4039
}
4140
catch (FileNotFoundException ex) when (ex.FileName?.StartsWith("Jint") is true)
4241
{
@@ -63,59 +62,6 @@ protected override async Task OnInitializedAsync()
6362

6463

6564

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-
11965
private async Task OnMarkdownSet()
12066
{
12167
if (IsRendered is false) return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Globalization;
2+
using Jint;
3+
4+
namespace Bit.BlazorUI;
5+
6+
public class BitMarked
7+
{
8+
private static string? _markedScriptText;
9+
private static readonly SemaphoreSlim _markedScriptReadTextSemaphore = new(1, 1);
10+
11+
12+
13+
public static async Task<string> Parse(string? markdown, CancellationToken cancellationToken)
14+
{
15+
if (markdown.HasNoValue()) return string.Empty;
16+
17+
await ReadMarkedScriptText(cancellationToken);
18+
if (_markedScriptText.HasNoValue()) return string.Empty;
19+
20+
using var engine = new Engine(options =>
21+
{
22+
options.Strict();
23+
options.CancellationToken(cancellationToken);
24+
options.Culture(CultureInfo.CurrentUICulture);
25+
}).Execute(_markedScriptText!);
26+
27+
var fn = engine.Evaluate("marked.parse").AsFunctionInstance();
28+
29+
return fn.Call(markdown).AsString();
30+
}
31+
32+
33+
34+
private static async Task<string> ReadMarkedScriptText(CancellationToken cancellationToken)
35+
{
36+
if (_markedScriptText is not null) return _markedScriptText;
37+
38+
try
39+
{
40+
await _markedScriptReadTextSemaphore.WaitAsync(cancellationToken);
41+
if (_markedScriptText is not null) return _markedScriptText;
42+
43+
//TODO: this script path discovery needs improvement!
44+
var scriptPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "_content", "Bit.BlazorUI.Extras", "marked", "marked-15.0.7.js");
45+
46+
if (File.Exists(scriptPath) is false)
47+
{
48+
scriptPath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "marked", "marked-15.0.7.js");
49+
}
50+
51+
if (File.Exists(scriptPath) is false)
52+
{
53+
Console.Error.WriteLine("Could not find the marked js script file!");
54+
return _markedScriptText = string.Empty;
55+
}
56+
57+
return _markedScriptText = await File.ReadAllTextAsync(scriptPath, cancellationToken);
58+
}
59+
finally
60+
{
61+
_markedScriptReadTextSemaphore.Release();
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)