Skip to content

Commit

Permalink
render math expressions with MathJax (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailsKuzmins authored Aug 28, 2022
1 parent 93f29ec commit 356a366
Show file tree
Hide file tree
Showing 66 changed files with 435 additions and 330 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bld/
**/wwwroot/**/*.min.*
**/wwwroot/**/*.png
**/wwwroot/**/*.jpg
**/wwwroot/**/*.woff

# Visual Studio 2015/2017 cache/options directory
.vs/
Expand Down
8 changes: 8 additions & 0 deletions samples/MudBlazor.Markdown.Core/Enums/MarkdownResourceType.cs
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
}
5 changes: 5 additions & 0 deletions samples/MudBlazor.Markdown.Core/MarkdownNavMenu.razor
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>
27 changes: 27 additions & 0 deletions samples/MudBlazor.Markdown.Core/MarkdownPage.razor
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;
}
17 changes: 10 additions & 7 deletions samples/MudBlazor.Markdown.Core/MudBlazor.Markdown.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<None Remove="*.md" />
<EmbeddedResource Include="*.md" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.1.5" />
<PackageReference Include="Blazored.LocalStorage" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>
Expand All @@ -19,4 +16,10 @@
<ProjectReference Include="..\..\src\MudBlazor.Markdown\MudBlazor.Markdown.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="*.md" />
<EmbeddedResource Include="*.md" />
<None Remove="*.csproj.DotSettings" />
</ItemGroup>

</Project>
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>
3 changes: 3 additions & 0 deletions samples/MudBlazor.Markdown.Core/Pages/Enderal.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@page "/enderal"

<MarkdownPage MarkdownResource="MarkdownResourceType.Enderal" />
3 changes: 3 additions & 0 deletions samples/MudBlazor.Markdown.Core/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@page "/"

<MarkdownPage />
3 changes: 3 additions & 0 deletions samples/MudBlazor.Markdown.Core/Pages/Math.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@page "/math"

<MarkdownPage MarkdownResource="MarkdownResourceType.Math" />
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);
}
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 samples/MudBlazor.Markdown.Core/Services/MarkdownService.cs
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);
}
}
76 changes: 36 additions & 40 deletions samples/MudBlazor.Markdown.Core/Services/ThemeService.cs
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);
}
}
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();
}
9 changes: 9 additions & 0 deletions samples/MudBlazor.Markdown.Core/sample-math.md
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}$$
4 changes: 2 additions & 2 deletions samples/MudBlazor.Markdown.Samples.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31229.75
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MudBlazor.Markdown.Wasm", "WebAssembly\MudBlazor.Markdown.Wasm.csproj", "{938200EC-072C-4FD7-B699-AEEFF03546BB}"
EndProject
Expand Down
Loading

0 comments on commit 356a366

Please sign in to comment.