Skip to content

MyNihongo/MudBlazor.Markdown

Repository files navigation

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.

📘 Documentation

🚀 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="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<link href="_content/MudBlazor.Markdown/MudBlazor.Markdown.min.css" rel="stylesheet" />

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

<script src="_content/MudBlazor/MudBlazor.min.js"></script>
<script src="_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>();

💻 Example Usage

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

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