Markdig

Programming
Published on January 9, 2025 Last updated on October 7, 2025
Article Image

Introduction

Markdig is an open-source Markdown library for .NET.
It can convert Markdown-formatted text to HTML and is provided as a NuGet package.
Its appealing features include rich extensions, fast conversion, and flexible customization.

Official xoofx / markdig - GitHub

Minimal Code

1Install Markdig

dotnet add package Markdig

or

Install-Package Markdig

2Code (Program.cs)

Convert with Markdown.ToHtml().

// Program.cs
using Markdig;

// Markdown Text
var markdown = "# Hello, I'm Korochin!\nThis is a *Markdig* test.";

// Convert Markdown to HTML
var html = Markdown.ToHtml(markdown);

// Display Result
Console.WriteLine(html);

Output Result

<h1>こんにちは、コロちんだよ!</h1>
<p>これは <em>Markdig</em> のテストです。</p>

Customize Markdig Output

Markdig has a mechanism called a pipeline (MarkdownPipeline), which allows you to customize Markdown parsing and conversion. Let's add the AutoIdentifiers extension to the pipeline.
Generate an instance of MarkdownPipeline and specify it as the second argument of Markdown.ToHtml().

using Markdig;
using Markdig.Extensions.AutoIdentifiers;

// Markdown Text
var markdown = "# Hello, I'm Korochin!\nThis is a *Markdig* test.";

// Build the pipeline
var pipeline = new MarkdownPipelineBuilder()
    .UseAutoIdentifiers(AutoIdentifierOptions.GitHub) // Use GitHub-style auto identifiers
    .Build();

// Convert Markdown to HTML
// Specify a MarkdownPipeline instance as the second argument
var html = Markdown.ToHtml(markdown, pipeline);

// Display Result
Console.WriteLine(html);

Output Result

<h1 id="こんにちはコロちんだよ">こんにちは、コロちんだよ!</h1>
<p>これは <em>Markdig</em> のテストです。</p>

An id has been added to the heading tags (H tags).
AutoIdentifiers is an extension that automatically adds an id to heading tags.

Conversion Examples

Let's try converting each notation from Markdown - Wikipedia with Markdig.

Blockquote

> "This text will be enclosed in an HTML blockquote element.
A blockquote element is reflowable. You can break the text
as you like. Even if you break lines, it will be treated as a single
blockquote element after conversion."

"This text will be enclosed in an HTML blockquote element. A blockquote element is reflowable. You can break the text as you like. Even if you break lines, it will be treated as a single blockquote element after conversion."

List

* Unordered list item
    * Sub-item is indented with a tab or 4 spaces
* Another unordered list item
+ Unordered list item
    + Sub-item is indented with a tab or 4 spaces
+ Another unordered list item
- Unordered list item
    - Sub-item is indented with a tab or 4 spaces
- Another unordered list item
1. Ordered list item
    1. Sub-item is indented with a tab or 4 spaces
2. Another ordered list item
  • Unordered list item
    • Sub-item is indented with a tab or 4 spaces
  • Another unordered list item
  • Unordered list item
    • Sub-item is indented with a tab or 4 spaces
  • Another unordered list item
  • Unordered list item
    • Sub-item is indented with a tab or 4 spaces
  • Another unordered list item
  1. Ordered list item
    1. Sub-item is indented with a tab or 4 spaces
  2. Another ordered list item

Code

This is a paragraph. It includes `code text` within the sentence.

This is a paragraph. It includes code text within the sentence.

```javascript
(() => {
  'use strict';
  console.log('Hello world');
})();
```
(() => {
  'use strict';
  console.log('Hello world');
})();

Horizontal Rule

* * *
***
*****
- - -
---------------------------------------





[Link text](Link address "Link title")

[Link text][linkref]

[linkref]: https://ja.wikipedia.org/wiki/Markdown

[Link text](Link address "Link title")

Link text

Emphasis

*Emphasis* or _Emphasis_ (often rendered as italic)
**Strong emphasis** or __Strong emphasis__ (often rendered as bold)

Emphasis or Emphasis (often rendered as italic) Strong emphasis or Strong emphasis (often rendered as bold)

Image

![Alt text](https://korochin0911.github.io/images202501/xzxkrv.webp)
![Alt text](https://korochin0911.github.io/images202501/xzxkrv.webp "Title")

Alt textAlt text TitleAlt text

Inline HTML

<font color="red">Red</font>

Red

References

xoofx / markdig - GitHub
CommonMark Spec