-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
render math expressions with MathJax (#116)
- Loading branch information
1 parent
93f29ec
commit 356a366
Showing
66 changed files
with
435 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
samples/MudBlazor.Markdown.Core/Enums/MarkdownResourceType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace MudBlazor.Markdown.Core; | ||
|
||
public enum MarkdownResourceType : byte | ||
{ | ||
Main, | ||
Enderal, | ||
Math | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@using Microsoft.AspNetCore.Components.Routing | ||
|
||
<MudNavLink Match="NavLinkMatch.All" Href="/">Sample</MudNavLink> | ||
<MudNavLink Match="NavLinkMatch.All" Href="/enderal">Enderal sample</MudNavLink> | ||
<MudNavLink Match="NavLinkMatch.All" Href="/math">Math sample</MudNavLink> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@using Markdig.Syntax.Inlines | ||
@using MudBlazor.Markdown.Core.Services | ||
|
||
<MudMarkdown Value="@Value" | ||
TableCellMinWidth="100" | ||
OverrideLinkUrl="OverrideLink" | ||
CodeBlockTheme="CodeBlockTheme.DraculaBase16" /> | ||
|
||
@code | ||
{ | ||
[Parameter] | ||
public MarkdownResourceType MarkdownResource { get; set; } | ||
|
||
[Inject] | ||
private IMarkdownService MarkdownService { get; init; } = default!; | ||
|
||
private string Value { get; set; } = string.Empty; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
Value = await MarkdownService.GetSampleAsync(MarkdownResource) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
private static string? OverrideLink(LinkInline x) => | ||
x.Url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
samples/MudBlazor.Markdown.Core/MudBlazor.Markdown.Core.csproj.DotSettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=enums/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cinterfaces/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@page "/enderal" | ||
|
||
<MarkdownPage MarkdownResource="MarkdownResourceType.Enderal" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@page "/" | ||
|
||
<MarkdownPage /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@page "/math" | ||
|
||
<MarkdownPage MarkdownResource="MarkdownResourceType.Math" /> |
11 changes: 3 additions & 8 deletions
11
samples/MudBlazor.Markdown.Core/Services/Interfaces/IMarkdownService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
using System.Threading.Tasks; | ||
namespace MudBlazor.Markdown.Core.Services; | ||
|
||
namespace MudBlazor.Markdown.Core.Services.Interfaces | ||
public interface IMarkdownService | ||
{ | ||
public interface IMarkdownService | ||
{ | ||
Task<string> GetSampleAsync(); | ||
|
||
Task<string> GetEnderalSampleAsync(); | ||
} | ||
Task<string> GetSampleAsync(MarkdownResourceType resourceType); | ||
} |
12 changes: 4 additions & 8 deletions
12
samples/MudBlazor.Markdown.Core/Services/Interfaces/IThemeService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
namespace MudBlazor.Markdown.Core.Services; | ||
|
||
namespace MudBlazor.Markdown.Core.Services.Interfaces | ||
public interface IThemeService | ||
{ | ||
public interface IThemeService | ||
{ | ||
IObservable<bool> IsDarkTheme { get; } | ||
IObservable<bool> IsDarkTheme { get; } | ||
|
||
Task ToggleThemeAsync(); | ||
} | ||
Task ToggleThemeAsync(); | ||
} |
42 changes: 22 additions & 20 deletions
42
samples/MudBlazor.Markdown.Core/Services/MarkdownService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using MudBlazor.Markdown.Core.Services.Interfaces; | ||
using System.Reflection; | ||
|
||
namespace MudBlazor.Markdown.Core.Services | ||
namespace MudBlazor.Markdown.Core.Services; | ||
|
||
internal sealed class MarkdownService : IMarkdownService | ||
{ | ||
internal sealed class MarkdownService : IMarkdownService | ||
public Task<string> GetSampleAsync(MarkdownResourceType resourceType) | ||
{ | ||
public Task<string> GetSampleAsync() => | ||
GetMarkdownAsync("sample"); | ||
var resourceName = resourceType switch | ||
{ | ||
MarkdownResourceType.Main => "sample", | ||
MarkdownResourceType.Enderal => "sample-enderal", | ||
MarkdownResourceType.Math => "sample-math", | ||
_ => throw new ArgumentOutOfRangeException(nameof(resourceType), resourceType, $"Unknown {nameof(MarkdownResourceType)}: {resourceType}") | ||
}; | ||
|
||
public Task<string> GetEnderalSampleAsync() => | ||
GetMarkdownAsync("sample-enderal"); | ||
return GetMarkdownAsync(resourceName); | ||
} | ||
|
||
private static async Task<string> GetMarkdownAsync(string name) | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
var resourceName = $"{assembly.GetName().Name}.{name}.md"; | ||
private static async Task<string> GetMarkdownAsync(string name) | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
var resourceName = $"{assembly.GetName().Name}.{name}.md"; | ||
|
||
await using var stream = assembly.GetManifestResourceStream(resourceName) ?? throw new InvalidOperationException("Markdown resource not found"); | ||
using var reader = new StreamReader(stream); | ||
await using var stream = assembly.GetManifestResourceStream(resourceName) ?? throw new InvalidOperationException("Markdown resource not found"); | ||
using var reader = new StreamReader(stream); | ||
|
||
return await reader.ReadToEndAsync() | ||
.ConfigureAwait(false); | ||
} | ||
return await reader.ReadToEndAsync() | ||
.ConfigureAwait(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,52 @@ | ||
using System; | ||
using System.Reactive.Subjects; | ||
using System.Threading.Tasks; | ||
using System.Reactive.Subjects; | ||
using Blazored.LocalStorage; | ||
using MudBlazor.Markdown.Core.Services.Interfaces; | ||
|
||
namespace MudBlazor.Markdown.Core.Services | ||
namespace MudBlazor.Markdown.Core.Services; | ||
|
||
internal sealed class ThemeService : IThemeService | ||
{ | ||
internal sealed class ThemeService : IThemeService | ||
{ | ||
private const string IsDarkKey = "HnT"; | ||
private const string IsDarkKey = "HnT"; | ||
|
||
private readonly ILocalStorageService _localStorageService; | ||
private readonly IMudMarkdownThemeService _mudMarkdownThemeService; | ||
private readonly ILocalStorageService _localStorageService; | ||
private readonly IMudMarkdownThemeService _mudMarkdownThemeService; | ||
|
||
private readonly ReplaySubject<bool> _isDarkThemeSubject = new(1); | ||
private bool _isDark; | ||
private readonly ReplaySubject<bool> _isDarkThemeSubject = new(1); | ||
private bool _isDark; | ||
|
||
public ThemeService( | ||
ILocalStorageService localStorageService, | ||
IMudMarkdownThemeService mudMarkdownThemeService) | ||
{ | ||
_localStorageService = localStorageService; | ||
_mudMarkdownThemeService = mudMarkdownThemeService; | ||
public ThemeService( | ||
ILocalStorageService localStorageService, | ||
IMudMarkdownThemeService mudMarkdownThemeService) | ||
{ | ||
_localStorageService = localStorageService; | ||
_mudMarkdownThemeService = mudMarkdownThemeService; | ||
|
||
// Sync service is not available in Blazor.Server | ||
Task.Run(async () => | ||
{ | ||
_isDark = await localStorageService.GetItemAsync<bool>(IsDarkKey) | ||
.ConfigureAwait(false); | ||
// Sync service is not available in Blazor.Server | ||
Task.Run(async () => | ||
{ | ||
_isDark = await localStorageService.GetItemAsync<bool>(IsDarkKey) | ||
.ConfigureAwait(false); | ||
|
||
PublishTheme(_isDark); | ||
}); | ||
} | ||
PublishTheme(_isDark); | ||
}); | ||
} | ||
|
||
public IObservable<bool> IsDarkTheme => _isDarkThemeSubject; | ||
public IObservable<bool> IsDarkTheme => _isDarkThemeSubject; | ||
|
||
public async Task ToggleThemeAsync() | ||
{ | ||
_isDark = !_isDark; | ||
public async Task ToggleThemeAsync() | ||
{ | ||
_isDark = !_isDark; | ||
|
||
await _localStorageService.SetItemAsync(IsDarkKey, _isDark) | ||
.ConfigureAwait(false); | ||
await _localStorageService.SetItemAsync(IsDarkKey, _isDark) | ||
.ConfigureAwait(false); | ||
|
||
PublishTheme(_isDark); | ||
} | ||
PublishTheme(_isDark); | ||
} | ||
|
||
private void PublishTheme(bool isDark) | ||
{ | ||
var codeBlockTheme = isDark ? CodeBlockTheme.DraculaBase16 : CodeBlockTheme.Monokai; | ||
_mudMarkdownThemeService.SetCodeBlockTheme(codeBlockTheme); | ||
private void PublishTheme(bool isDark) | ||
{ | ||
var codeBlockTheme = isDark ? CodeBlockTheme.DraculaBase16 : CodeBlockTheme.Monokai; | ||
_mudMarkdownThemeService.SetCodeBlockTheme(codeBlockTheme); | ||
|
||
_isDarkThemeSubject.OnNext(isDark); | ||
} | ||
_isDarkThemeSubject.OnNext(isDark); | ||
} | ||
} |
18 changes: 8 additions & 10 deletions
18
samples/MudBlazor.Markdown.Core/Utils/ServiceRegistration/ServiceCollectionEx.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
using Blazored.LocalStorage; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MudBlazor.Markdown.Core.Services; | ||
using MudBlazor.Markdown.Core.Services.Interfaces; | ||
|
||
namespace MudBlazor.Markdown.Core.Utils.ServiceRegistration | ||
namespace MudBlazor.Markdown.Core.Utils.ServiceRegistration; | ||
|
||
public static class ServiceCollectionEx | ||
{ | ||
public static class ServiceCollectionEx | ||
{ | ||
public static IServiceCollection AddCoreServices(this IServiceCollection @this) => | ||
@this | ||
.AddSingleton<IMarkdownService>(new MarkdownService()) | ||
.AddScoped<IThemeService, ThemeService>() | ||
.AddBlazoredLocalStorage(); | ||
} | ||
public static IServiceCollection AddCoreServices(this IServiceCollection @this) => | ||
@this | ||
.AddSingleton<IMarkdownService>(new MarkdownService()) | ||
.AddScoped<IThemeService, ThemeService>() | ||
.AddBlazoredLocalStorage(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#### Inline | ||
$x = \frac{1}{2}$ | ||
$ax^2$ | ||
$\sqrt {2}$ | ||
|
||
#### Centered | ||
$$x = \frac{1}{2}$$ | ||
$$ax^2$$ | ||
$$\sqrt {2}$$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.