Blazor: Live Preview Markdown Editor’s Content Using Markdig Library | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Blazor: Live Preview Markdown Editor’s Content Using Markdig Library

Blazor: Live Preview Markdown Editor’s Content Using Markdig Library

Our Syncfusion Blazor Rich Text Editor component can be used as a Blazor WYSIWYG Markdown editor. It allows the content to be in the Markdown format. The typed Markdown syntax content can be previewed using any third-party plugin.

In this blog post, we are going to integrate the Markdig third-party library into our Blazor Rich Text Editor’s Markdown editor (MD editor) to preview the Markdown.

The following topics are covered in this blog:

Let’s get started!

Prerequisites

We are going to demonstrate the application in a Blazor server-side application. So, the following prerequisites are needed:

What is Markdig?

Markdig is one of the fastest third-party libraries for parsing and converting Markdown plain text to HTML string.

For more information, please go through this Markdig GitHub link.

Create a Blazor server-side application

First, create a Blazor server-side application and configure the Syncfusion Blazor services.

Configure the Blazor Rich Text Editor’s Markdown editor

Enable the Syncfusion Blazor Rich Text Editor to function as a Markdown editor:

  1. Set the EditorMode as EditorMode.Markdown.
    <SfRichTextEditor EditorMode="EditorMode.Markdown">
    </SfRichTextEditor>
  2. Then, set the SaveInterval to 1 to convert and trigger the ValueChange event every second.
    <SfRichTextEditor SaveInterval="1" EditorMode="EditorMode.Markdown" Height="100%" @bind-Value="@mdValue">
          ……
          …….
    </SfRichTextEditor>
  3. Configure the most-used toolbar items like Bold and Italic for the SfRichTextEditor Markdown editor.
    private List<ToolbarItemModel> tools = new List<ToolbarItemModel>()
    {
         new ToolbarItemModel() { Command = ToolbarCommand.Bold },
         new ToolbarItemModel() { Command = ToolbarCommand.Italic },
         new ToolbarItemModel() { Command = ToolbarCommand.StrikeThrough },
         new ToolbarItemModel() { Command = ToolbarCommand.SubScript },
         new ToolbarItemModel() { Command = ToolbarCommand.SuperScript },
         new ToolbarItemModel() { Command = ToolbarCommand.Separator },
         new ToolbarItemModel() { Command = ToolbarCommand.Formats },
         new ToolbarItemModel() { Command = ToolbarCommand.OrderedList },
         new ToolbarItemModel() { Command = ToolbarCommand.UnorderedList },
         new ToolbarItemModel() { Command = ToolbarCommand.Separator },
         new ToolbarItemModel() { Command = ToolbarCommand.CreateLink },
         new ToolbarItemModel() { Command = ToolbarCommand.Image },
         new ToolbarItemModel() { Command = ToolbarCommand.CreateTable },
         new ToolbarItemModel() { Command = ToolbarCommand.Separator },
         new ToolbarItemModel() { Command = ToolbarCommand.Undo },
         new ToolbarItemModel() { Command = ToolbarCommand.Redo }
     };
  4. Then, render the Markdown editor.
    <SfRichTextEditor SaveInterval="1" EditorMode="EditorMode.Markdown" Height="100%" @bind-Value="@mdValue">
       <RichTextEditorEvents ValueChange="onValueChange"></RichTextEditorEvents>
       <RichTextEditorToolbarSettings Items="@tools" Type="ToolbarType.MultiRow"></RichTextEditorToolbarSettings>
    </SfRichTextEditor>

    Render another Rich Text Editor in the right side to preview the Markdown editor without a toolbar.

    <SfRichTextEditor Readonly="true" Height="100%" @bind-Value="@htmlValue">
       <RichTextEditorToolbarSettings Enable="false"></RichTextEditorToolbarSettings>
    </SfRichTextEditor>

Add the Markdig library

To import the Markdig namespace, install the Markdig NuGet package in your application as shown.

dotnet add package Markdig --version 0.22.0

Show live preview

Convert the Rich Text Editor’s Markdown plain text to HTML string using the Markdig library. Then, bind the resultant HTML string to the Rich Text Editor Markdown preview on the right side to preview the HTML content.

Refer to the following code.

private MarkdownPipeline pipeline { get; set; }
 private string htmlValue { get; set; }
 protected override void OnInitialized()
 {
    pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
    this.htmlValue = Markdig.Markdown.ToHtml(mdValue, pipeline);
    base.OnInitialized();
 }

Convert the Markdown plain text to HTML string in the ValueChange event of the left-side Rich Text Editor.
Refer to the following code example.

private void onValueChange(Syncfusion.Blazor.RichTextEditor.ChangeEventArgs args)
    {
        if (args.Value == null)
        {
            this.htmlValue = null;
        }
        else
        {
            this.htmlValue = Markdig.Markdown.ToHtml(args.Value, pipeline);
        }
    }

Output

Live preview of markdown in Blazor
Live preview of markdown in Blazor

Resources

To learn more, refer to the demo sample live preview of markdown content in Blazor.

Conclusion

I hope you now have a clear idea on how easy it is to integrate the Markdig library with Syncfusion Blazor Rich Text Editor’s Markdown editor and preview Markdown text.

If you are new to Syncfusion, try our control’s features by downloading a free trial. You can also explore our online demo and our documentation.

You can contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

If you like this post, we think you will also like the following:

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed