Markdown in AEM with Flexmark

Published on by Dan KlcoPicture of Me Dan Klco

Markdown is a light markup language with text-based syntax which can be converted to HTML, PDFs or any number of different styled formats. Markdown is a popular format for text-heavy content such as documentation, wiki content and comments as it is easy to maintain and read the content without the added complexity of including formatting.

Unfortunately, AEM did not provide a mechanism to interact with Markdown content, nor were any of the Java markdown libraries compatible with OSGi. Recently, I worked with the Flexmark team to produce an OSGi bundle version of the Flexmark markdown library.

Now that this library is released, integrating markdown content into AEM is an easy process.

Using Flexmark in AEM

To use the Flexmark library in AEM, first you need to get the bundle installed. It can either be downloaded directly from Maven Central (not recommended) or integrated into your Content Package POM with the following:

<!-- within /project/dependencies -->

<!-- within /project/build/plugins/plugin (content-package-maven-plugin) -->

Once the bundle is installed you can then use the Flexmark API to retrieve and leverage the Markdown content from the AEM repository, for example using a Sling Model like this one which retrieves Markdown content from the property markdownfield and converts the Markdown content to HTML:



import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;

@Model(adaptables = Resource.class)
public class DiscussionMarkdownModel {

	private Resource resource;

	public String getHtml() {
		MutableDataSet options = new MutableDataSet();

		Parser parser = Parser.builder(options).build();
		HtmlRenderer renderer = HtmlRenderer.builder(options).build();
		Node node = parser.parse(resource.getValueMap().get("markdownfield", String.class));

		return renderer.render(node);

With the new Flexmark OSGi bundle, integrating Markdown in AEM is painless, as we can see in this simple example.

Robert Munteanu is also working on a Sling Resource Provider backed by Markdown files, which is an interesting concept if someone wanted to do something like publish documentation in Markdown, but display it on a site with the dynamic, marketing features AEM. So who knows, maybe Adobe could convert their documentation site into Markdown + AEM?


comments powered by Disqus