How to Convert Markdown to HTML using Java

Cloudmersive
2 min readSep 7, 2022

--

Markdown is an excellent lightweight solution for creating web-facing content without having to write out line after line of HTML code. However, since the entire point of Markdown is to make writing HTML easier, we’re going to need to convert Markdown syntax to HTML eventually — and that’s where our Markdown to HTML API can save the day. This API will take an input .md file and output your HTML string, as well as a boolean to verify that the operation was successful (incorrectly formatted markdown syntax will likely result in a “false” result).

To use this API for free, you just need to register a free-tier account on our website (free accounts yield a limit of 800 API calls per month; perfect for small projects) and use your secure Cloudmersive API key to authenticate the service. You can easily structure your API call using the instructions provided below, which include ready-to-run Java code examples for your convenience.

Before we jump into the fun stuff, we need to first install the API client with Maven (you can also use Gradle, which I’ve left out for the sake of brevity in this article; gradle examples can be found here under the /convert/web/md/to/html dropdown). Let’s add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, let’s add a reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now we can shift over to our controller and add our imports:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertWebApi;

Lastly, we can copy in the remaining code to call the function. We just need to include our API key where indicated in the comments, and include our file path where shown in the snippet below that:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertWebApi apiInstance = new ConvertWebApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
HtmlMdResult result = apiInstance.convertWebMdToHtml(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertWebApi#convertWebMdToHtml");
e.printStackTrace();
}

Now you’re all done — simple as that.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet