Skip to main content

Markdown in an ASP.NET Core rich text editor

The editor with a formatted document

Developers often prefer Markdown, while everyone else wants a toolbar. RichTextBox can serve both, and convert a document in either direction.

Turn it on

<richtextbox id="StructuredEditor" name="Body"
             toolbar="default"
             enable-structured-content="true"
             height="420px"></richtextbox>

Convert in JavaScript

var editor = window.richTextBoxEditors.StructuredEditor;

var md = editor.getMarkdown();      // the document as Markdown
editor.fromMarkdown("# Title\n\nSome *text*.");   // Markdown into the editor
editor.downloadMarkdown();          // save a .md file
editor.copyAsMarkdown();            // to the clipboard
editor.toggleMarkdownMode();        // edit the Markdown directly
editor.isMarkdownMode();

Check the demo for what each one returns.

Which to store

You can store either. Most sites should keep HTML, because that is what the editor loads and what the cleaner handles, and generate Markdown when someone asks for it. If your system is built around Markdown, such as a documentation site in Git, store the Markdown, and convert it to HTML to show it.

What Markdown cannot hold

Colors, fonts, merged table cells and comments have no standard form in Markdown. A round trip through it loses them, so do not switch a document with that kind of formatting without warning people.

Clean it either way

Markdown can contain HTML, and it can contain javascript: links, so it is not automatically safe. Render it to HTML, then pass it through IRichTextBoxHtmlSanitizer, as you would any posted HTML.

Read next