Convert HTML to Markdown – C# Examples and Online Converter

Markdown is a markup language with a plain-text-formatting syntax. Markdown is often used as a format for documentation and readme files since it allows writing in an easy-to-read and easy-to-write style. It is popular with technical writers for its simplicity of use, lightweight learning and broad support. Its design allows it to be easily converted to many output formats, but initially, it was created to convert only to HTML. Aspose.HTML class library provides a reversed conversion from HTML to Markdown. You can access and edit Markdown files or create new content from any device in any text editor.

In this article, you find information on how to convert HTML to MD using ConvertHTML() methods of the Converter class, and how to apply MarkdownSaveOptions. Our code examples help you to convert HTML to Markdown using the C# library.

Online HTML Converter

You can convert HTML to Markdown with Aspose.HTML for .NET API in real time. First, load an HTML file from your local drive and then run the example. In this example, the save options are set by default. You will immediately receive the result of the HTML to Markdown conversion as a separate Markdown file.

                
            

HTML to Markdown by a few lines of code

You can convert HTML to Markdown format using C# and other .NET programming languages. The static methods of the Converter class are primarily used as the easiest way to convert an HTML code into various formats. The following code snippet shows how to convert HTML to Markdown literally with a few lines of code!

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4...
 5    // Prepare HTML code and save it to a file
 6    var code = "<h1>Header 1</h1>" +
 7               "<h2>Header 2</h2>" +
 8               "<p>Hello, World!!</p>";
 9    File.WriteAllText("singl-line.html", code);
10
11    // Call ConvertHTML() method to convert HTML to Markdown
12    Converter.ConvertHTML("singl-line.html", new MarkdownSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.md"));

Save Options

The Markdown creation functionality can be enhanced with save options per your needs. The MarkdownSaveOptions has a number of properties that give you control over the conversion process. The most important option is MarkdownSaveOptions.Features. This option allows you to enable/disable the conversion of the particular element.

PropertyDescription
DefaultThis property returns a set of options that are compatible with default Markdown documentation.
FeaturesA flag set that controls which HTML elements are converted to Markdown.
FormatterThis property gets or sets the Markdown formatting style.
GitThis property returns a set of options that are compatible with GitLab Flavored Markdown.
ResourceHandlingOptionsGets a ResourceHandlingOptions object which is used for configuration of resources handling.

To learn more about MarkdownSaveOptions, please read the Fine-Tuning Converters article.

Convert HTML to Markdown using MarkdownSaveOptions

To convert HTML to Markdown with Markdown SaveOptions specifying, you should follow a few steps:

  1. Load an HTML file using one of the HTMLDocument() constructors of the HTMLDocument class.
  2. Create a new MarkdownSaveOptions object.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a Markdown file. You need to pass the HTMLDocument, MarkdownSaveOptions, and output file path to the ConvertHTML() method to convert HTML to Markdown.

The following example shows how to process only links and paragraphs, other HTML elements remain as is:

 1 
 2using System.IO;
 3using Aspose.Html.Converters;
 4using Aspose.Html.Saving;
 5...
 6    // Prepare a path for converted file saving 
 7    string savePath = Path.Combine(OutputDir, "options-output.md");
 8
 9    // Prepare HTML code and save it to a file
10    var code = "<h1>Header 1</h1>" +
11               "<h2>Header 2</h2>" +
12               "<p>Hello, World!!</p>" +
13               "<a href='aspose.com'>aspose</a>";
14    File.WriteAllText(Path.Combine(OutputDir, "options.html"), code);
15    
16    // Create an instance of SaveOptions and set up the rule: 
17    // – only <a> and <p> elements will be converted to Markdown
18    var options = new MarkdownSaveOptions();
19    options.Features = MarkdownFeatures.Link | MarkdownFeatures.AutomaticParagraph;
20
21    // Call the ConvertHTML() method to convert HTML to Markdown
22    Converter.ConvertHTML(Path.Combine(OutputDir, "options.html"), options, savePath);

To convert HTML to Markdown you can define your own set of rules or use the predefined templates. For instance, you can use the template based on GitLab Flavored Markdown syntax:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4...
 5    // Prepare a path for converted file saving 
 6    string savePath = Path.Combine(OutputDir, "output-git.md");
 7
 8    // Prepare HTML code and save it to the file
 9    var code = "<h1>Header 1</h1>" +
10               "<h2>Header 2</h2>" +
11               "<p>Hello World!!</p>";
12    File.WriteAllText(Path.Combine(OutputDir, "document.html"), code);
13
14    // Call ConvertHTML() method to convert HTML to Markdown
15    Converter.ConvertHTML(Path.Combine(OutputDir, "document.html"), MarkdownSaveOptions.Git, savePath);

GitLab Flavored Markdown is the GitHub.com version of the Markdown syntax that provides an additional set of helpful features that make it easier to work with content on GitHub.com.

Limitation

Markdown is a lightweight and easy-to-use syntax. Not all HTML elements are possible to convert to Markdown since there is no equivalent in Markdown syntax. The elements such as STYLE, SCRIPT, LINK, EMBED, etc. will be discarded during conversion.

You can download the complete examples and data files from GitHub.

Inline HTML

Markdown allows you to specify the pure HTML code, which will be rendered as is. The feature, which allows this behavior, is called “Inline HTML”. In order to use it, you should place one of the specific elements, supported by this feature, at the beginning of new line. Or you can mark one of such elements as “Inline HTML”, by adding the attribute markdown with the value inline to this element. Here is small example, which demonstrate, how to use this attribute:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4...
 5    // Prepare a path for converted file saving 
 6    string savePath = Path.Combine(OutputDir, "inline-html.md");
 7
 8    // Prepare HTML code and save it to the file
 9    var code = "text<div markdown='inline'><code>text</code></div>";
10    File.WriteAllText(Path.Combine(OutputDir, "inline.html"), code);
11
12    // Call ConvertHTML() method for HTML to Markdown conversion
13    Converter.ConvertHTML(Path.Combine(OutputDir, "inline.html"), new MarkdownSaveOptions(), savePath);
14    
15    // Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>

As you can see, content of the div element is not converted to Markdown and is treated by Markdown Processor as-is. The list of elements, which support this feature, is different for every Markdown processor.

The original Markdown specification supports these tags: BLOCKQUOTE,H1, H2, H3, H4, H5, H6, P, PRE, OL, UL, DL, DIV, INS, DEL, IFRAME, FIELDSET, NOSCRIPT, FORM, MATH.

The GitLab Flavored Markdown extends this list with the next tags: ARTICLE, FOOTER, NAV, ASIDE, HEADER, ADDRESS, HR, DD, FIGURE, FIGCAPTION, ABBR, VIDEO, AUDIO, OUTPUT, CANVAS, SECTION, DETAILS, HGROUP, SUMMARY.

Features nesting

Markdown supports a lot of features, but not all of them can be used together. As an example list elements inside of table elements would not be converted. The following table shows what features can be nested. Each feature is a member of the MarkdownFeatures enumeration.

Parent featureFeatures which can be processed inside
HeaderLink, Emphasis, Strong, InlineCode, Image, Strikethrough, Video
BlockquoteAny
ListAutomaticParagraph, Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough, Video, TaskList, List
LinkEmphasis, Strong, InlineCode, Image, LineBreak, Strikethrough
AutomaticParagraphLink, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough
StrikethroughLink, Emphasis, Strong, InlineCode, Image, LineBreak
TableVideo, Strikethrough, Image, InlineCode, Emphasis, Strong, Link
EmphasisLink, InlineCode, Image, LineBreak, Strikethrough, Video
StrongLink, InlineCode, Image, LineBreak, Strikethrough, Video

You can download the complete examples and data files from GitHub.

Download our Aspose.HTMLfor .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.

Aspose.HTML offers a free online HTML to MD Converter that converts HTML to Markdown with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

Text “Banner HTML to MD Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.