Convert HTML to Markdown in Java

To convert HTML to Markdown in Java, prepare an HTML file, create MarkdownSaveOptions, and call Converter.convertHTML(). Use setFeatures() to select which HTML elements are converted, or MarkdownSaveOptions.getGit() for GitLab Flavored Markdown-compatible options.

Markdown is a lightweight plain-text markup syntax widely used for documentation and README files. Aspose.HTML for Java supports the reverse of the usual Markdown-to-HTML workflow by converting supported HTML elements to Markdown with convertHTML() and MarkdownSaveOptions.

Convert HTML to Markdown in Java

The following example creates conversion.html and converts it to conversion.md with the default MarkdownSaveOptions:

  1. Prepare the HTML string and save it to an input file.
  2. Create MarkdownSaveOptions.
  3. Call Converter.convertHTML(sourcePath, options, outputPath).
 1// Convert HTML to Markdown in Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<h1>Convert HTML to Markdown Using Java</h1>" +
 5        "<h2>How to Convert HTML to MD in Java</h2>" +
 6        "<p>The Aspose.HTML for Java library allows you to convert HTML to Markdown.</p>";
 7FileHelper.writeAllText("conversion.html", code);
 8
 9// Call ConvertHTML() method to convert HTML to Markdown
10Converter.convertHTML("conversion.html", new MarkdownSaveOptions(), "conversion.md");

Save Options – MarkdownSaveOptions Class

The MarkdownSaveOptions class controls which HTML elements are converted, the Markdown formatting style, resource handling, and predefined compatibility settings.

MethodUse it to
getDefault()Get options compatible with default Markdown documentation.
setFeatures(value)Set flags that control which HTML elements are converted to Markdown.
setFormatter(value)Set the Markdown formatting style.
getGit()Get options compatible with GitLab Flavored Markdown.
getResourceHandlingOptions()Access settings for handling linked resources.

For additional save settings, see Fine-Tuning Converters. Download complete examples and data files from GitHub.

Select HTML Elements with MarkdownFeatures

The MarkdownFeatures flags control which types of HTML elements are converted to Markdown. Combine flags with the bitwise OR operator and pass the result to MarkdownSaveOptions.setFeatures().

In the following example:

  1. Create an HTML file containing headings, a paragraph, and a link.
  2. Create MarkdownSaveOptions.
  3. Enable MarkdownFeatures.Link and MarkdownFeatures.AutomaticParagraph.
  4. Call Converter.convertHTML(sourcePath, options, outputPath) to convert the HTML file to Markdown.

Only the enabled link and automatic paragraph features are selected for Markdown conversion.

In this example, the result is saved as options-output.md.

 1// Convert selective HTML tags to Markdown using Java
 2
 3// Prepare HTML code and save it to the file
 4String code = "<h1>Header 1</h1>" +
 5        "<h2>Header 2</h2>" +
 6        "<p>Hello, World!!</p>" +
 7        "<a href='aspose.com'>aspose</a>";
 8FileHelper.writeAllText("options.html", code);
 9
10// Create an instance of SaveOptions and set up the rule:
11// - only <a> and <p> elements will be converted to Markdown
12MarkdownSaveOptions options = new MarkdownSaveOptions();
13options.setFeatures(MarkdownFeatures.Link | MarkdownFeatures.AutomaticParagraph);
14
15// Call the convertHTML() method to convert HTML to Markdown
16Converter.convertHTML("options.html", options, "options-output.md");

Use GitLab Flavored Markdown Options

MarkdownSaveOptions.getGit() returns a predefined set of options compatible with GitLab Flavored Markdown. Use it when the output is intended for GitLab-compatible Markdown processing.

The following example creates an HTML file and converts it to output-git.md with the predefined GitLab-compatible options:

 1// Convert HTML to Markdown in Java using Git syntax
 2
 3// Prepare HTML code and save it to a file
 4String code = "<h1>Header 1</h1>" +
 5        "<h2>Header 2</h2>" +
 6        "<p>Hello, World!!</p>";
 7FileHelper.writeAllText("document.html", code);
 8
 9// Call convertHTML() method to convert HTML to Markdown
10Converter.convertHTML("document.html", MarkdownSaveOptions.getGit(), "output-git.md");

HTML to Markdown Conversion Limitations

Markdown cannot represent every HTML element or reproduce CSS-driven page layout. Elements without a Markdown equivalent, including <style>, <script>, <link>, and <embed>, are discarded during conversion.

Preserve Content as Inline HTML

Markdown processors can allow raw HTML to remain in Markdown. Aspose.HTML can preserve supported elements as inline HTML when an element is marked with the markdown="inline" attribute. Support for raw HTML elements also depends on the Markdown processor that reads the resulting file.

The following example converts inline.html to inline-html.md. The <div markdown="inline"> element and its <code> child remain as HTML in the output:

 1// Convert inline HTML elements to Markdown using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "text<div markdown='inline'><code>text</code></div>";
 5FileHelper.writeAllText("inline.html", code);
 6
 7// Call convertHTML() method to convert HTML to Markdown
 8Converter.convertHTML("inline.html", new MarkdownSaveOptions(), "inline-html.md");
 9
10// Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>

The original Markdown specification supports the following block-level raw HTML elements: BLOCKQUOTE, H1H6, P, PRE, OL, UL, DL, DIV, INS, DEL, IFRAME, FIELDSET, NOSCRIPT, FORM, and MATH.

GitLab Flavored Markdown extends the supported set with elements including ARTICLE, FOOTER, NAV, ASIDE, HEADER, ADDRESS, HR, DD, FIGURE, FIGCAPTION, ABBR, VIDEO, AUDIO, OUTPUT, CANVAS, SECTION, DETAILS, HGROUP, and SUMMARY.

Feature Nesting

Not every Markdown feature can be nested inside every other feature. For example, list elements inside table elements are not converted. The following table shows the supported nesting combinations for members of the MarkdownFeatures enumeration.

Parent featureFeatures that 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

Related Markdown Workflows

Other Platforms

FAQ

Can I convert HTML to Markdown for free?

Yes. Use the free online HTML to Markdown Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.

Does HTML-to-Markdown conversion preserve CSS and page layout?

No. Markdown represents document structure rather than CSS-based visual layout. Use MarkdownSaveOptions to control supported converted features, and use inline HTML where the target Markdown processor supports it.

Try Online HTML to Markdown Conversion

Use the free online HTML to Markdown Converter for quick manual conversion without writing Java code.