MyNihongo / MudBlazor.Markdown

Markdown component based on the MudBlazor environment
https://mudblazor.com/
MIT License
140 stars 13 forks source link
blazor blazor-client blazor-components blazor-server blazor-webassembly markdown material mudblazor netcore wasm

Version Nuget downloads

Markdown component for MudBlazor

This README covers configuration steps for Blazor Server and Blazor WebAssembly. For images of how the markup component looks like in the browser go to the README of samples.

Update guide

For guidance with update errors please visit the wiki page.

Getting started

NB! MudBLazor does not work well with the static SSR format because some code is executed in OnAfterRender or OnAfterRenderAsync that is not invoked by default.
Specify @rendermode="InteractiveServer" on the markdown component to make it work (e.g. <MudMarkdown @rendermode="InteractiveServer" Value="some markdown here" />)

Install the NuGet package.

dotnet add package MudBlazor.Markdown

Add the following using statement in _Imports.razor.

@using MudBlazor

Add the following nodes in either App.razor or MainLayout.razor.

<MudThemeProvider />
<MudPopoverProvider />
<MudDialogProvider />
<MudSnackbarProvider />

Add the following nodes in Pages/_Host.cstml (Server) or wwwroot/index.html (WebAssembly).
In the <head> node add these CSS stylesheets.

<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="https://github.com/MyNihongo/MudBlazor.Markdown/blob/main/_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<link href="https://github.com/MyNihongo/MudBlazor.Markdown/blob/main/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.css" rel="stylesheet" />

At the bottom of the <body> node add this JS source.

<script src="https://github.com/MyNihongo/MudBlazor.Markdown/raw/main/_content/MudBlazor/MudBlazor.min.js"></script>
<script src="https://github.com/MyNihongo/MudBlazor.Markdown/raw/main/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.js"></script>

Register MudBlazor services in the DI container.
For the Blazor Server in the Startup.cs add this method.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMudServices();
    services.AddMudMarkdownServices();
    // Optionally if the default clipboard functionality fails it is possible to add a custom service
    // NB! MauiClipboardService is just an example
    services.AddMudMarkdownClipboardService<MauiClipboardService>();
}

For the Blazor WebAssembly in the Program.cs add this method.

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddMudServices();
builder.Services.AddMudMarkdownServices();
// Optionally if the default clipboard functionality fails it is possible to add a custom service
// NB! MauiClipboardService is just an example
builder.Services.AddMudMarkdownClipboardService<MauiClipboardService>();

Using the component

<MudText Typo="Typo.h3">My markdown</MudText>
<MudMarkdown Value="@Value" />

@code
{
    private string Value { get; } = "text *italics* and **bold**";
}

Available properties

The <MudMarkdown> supports the palette of the MudTheme which makes styling easy (we hope). These are the colors which are used in the <MudMarkdown>: