Convert Markdown to HTML in Java
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. Its design allows it to be easily converted to many output formats, but initially it was created to convert the only to HTML.
This article provides information on how to convert Markdown to HTML using the Aspose.HTML for Java library. In addition, you will consider Java examples to illustrate conversion scenarios.
Why Convert Markdown to HTML?
- Markdown is a lightweight markup language originally designed to be easily converted to HTML. The idea was to allow writers to use a simple and easy-to-learn syntax to format their text without worrying about the complexities of HTML. HTML is the standard markup language for creating web pages, so converting Markdown to HTML makes publishing content on the web easy.
- Aspose.HTML uses Markdown to HTML conversion as an intermediate step to convert Markdown documents to other popular formats such as PDF, XPS, and images.
Convert Markdown to HTML
Using the Aspose.HTML class library in your Java application, you can easily convert Markdown to HTML with just a few lines of code! An easy way to perform the conversion is to call the convertMarkdown(sourcePath, outputPath)
method, which only takes a path to the source Markdown file and output file path.
1 // Prepare a path to a source Markdown file
2 String sourcePath = Path.combine(getDataDir(), "nature.md");
3
4 // Prepare a path for converted file saving
5 String outputPath = Path.combine(getOutputDir(), "nature-output.html");
6
7 // Convert Markdown to HTML
8 com.aspose.html.converters.Converter.convertMarkdown(sourcePath, outputPath);
Let’s look at another Java example. You can prepare Markdown source code from scratch and convert it to HTML:
1 // Prepare a simple Markdown example
2 String code = StringExtensions.concat("### Hello, World!",
3 "\r\n",
4 "[visit applications](https://products.aspose.app/html/applications)");
5
6 // Create a Markdown file
7 com.aspose.html.internal.ms.System.IO.File.writeAllText(sourcePath, code);
8
9 // Prepare a path for converted file saving
10 String outputPath = Path.combine(getOutputDir(), "document-output.html");
11
12 // Convert Markdown to HTML
13 com.aspose.html.converters.Converter.convertMarkdown(sourcePath, outputPath);
Aspose.HTML offers a free online Markdown to HTML Converter that converts Markdown to HTML with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!
Markdown Syntax
This article demonstrates the syntax declared in the core Markdown specification and GitLab Flavored Markdown variation. All these features are supported by Aspose.HTML out-of-the-box.
Headers
Markdown supports two styles of headers, Setext and atx:
1 This is an H1
2
3\=============
4
5This is an H2
6
7\-------------
1 # This is an H1
2
3\## This is an H2
4
5\###### This is an H6
Lists
Markdown supports ordered (numbered) and unordered (bulleted) lists. Unordered lists use asterisks, pluses, and hyphens — interchangably – as list markers:
1 * Red
2
3\* Green
4
5\* Blue
Ordered lists use numbers followed by periods:
1 1. Bird
2
3\2. McHale
4
5\3. Parish
Images
Inline image syntax looks like this:
1 ![Alt text](/path/to/img.jpg)
Links
Links syntax looks like this:
1 [an example](http://example.com/ "Title")
Emphasis
Markdown treats asterisks * and underscores (_) as indicators of emphasis:
1 *single asterisks*
2
3_single underscores_
4
5**double asterisks**
6
7__double underscores__
Blockquotes
Markdown uses email-style > characters for blockquoting:
1 > This is the first level of quoting.
2
3\>
4
5\> > This is nested blockquote.
Code Block
To indicate a span of code, wrap it with backtick quotes (`):
1 javascript
2
3function myFunc() {
4
5 alert('Hello World!!!');
6
7}
8
9\
Tables
Tables aren’t part of the core Markdown spec, but they are part of GFM:
1 | header 1 | header 2 | header 3 |
2
3\| --- | ------ |--------- |
4
5| cell 1 | cell 2 | cell 3 |
You can download the complete examples and data files from GitHub.