Skip to main content


Simple Markdown Documentation For Your AEM Components

Documentation Documentation Documentation. I believe documentation is one of the most important aspects of delivering a product, or in this case, an AEM component.
If you aren’t already, you should use the Granite UI widget fieldDescription property to add helpful tooltips to your dialog fields. You can also use the Granite Alert Widget for quick in-line authoring tips.
Here is an example:

Markdown Servlet

In some cases, you might need to add more documentation about the component, whether it’s technical or otherwise. For that, we will piggyback off Dan’s post and create a simple servlet using flexmark. That servlet will accept the file path from the request suffix and render that file as HTML for us to view.

Update: I found out that Andrei Shilov had a very similar idea and wrote about it here.

The Steps:

First, add the following two dependencies to your maven project:


Then embed both of them as bundles in your content-package-maven-plugin like so:

                <target>/apps/aem-markdown/install</target> <!-- Change this to your project install path -->
                <target>/apps/aem-markdown/install</target> <!-- Change this to your project install path -->

You can also embed them using the maven-bundle-plugin <Embed-Dependency> as an alternative method.
Then add the following servlet code:

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.options.MutableDataSet;
import java.nio.charset.Charset;
import java.util.Optional;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.commons.JcrUtils;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
    service = Servlet.class,
    immediate = true,
    property = {
      "service.description=Markdown Servlet",
      "service.vendor=Ahmed Musallam",
public class MarkdownServlet extends SlingSafeMethodsServlet {
  private static final Logger log = LoggerFactory.getLogger(MarkdownServlet.class);
  private static final MutableDataSet options = new MutableDataSet();
   * both Parser and Renderer are thread-safe according to:
  private static final Parser parser = Parser.builder(options).build();
  private static final HtmlRenderer renderer = HtmlRenderer.builder(options).build();
  protected void doGet(SlingHttpServletRequest slingRequest, SlingHttpServletResponse slingResponse)
      throws ServletException, IOException {
    // The markdown file path is the suffix of the request
    Resource mdFileResource =
            .filter(resource -> JcrConstants.NT_FILE.equals(resource.getResourceType()))
    if (mdFileResource == null) { // does not exist
      String path = Optional.ofNullable(slingRequest)
      slingResponse.sendError(404, "Markdown File does not exits at: "+ path);
    } else { // does exist
      try {
        slingResponse.setContentType("text/html; charset=utf-8");
        InputStream in = JcrUtils.readFile(mdFileResource.adaptTo(Node.class));
        String markdownString = IOUtils.toString(in, Charset.defaultCharset());
        String renderedHtml =  renderer.render(parser.parse(markdownString));
      } catch (RepositoryException e) {
        throw new ServletException("I've wrapped the real error in a Servlet Exception. Keep scrolling down...", e);

This servlet binds to the path /bin/markdown.html and uses the suffix as the path to the markdown file. For example, if my markdown file for the blockquote component was at path:/apps/my-com/components/general/blockquote/README.mdthen I could get the HTML version of it by requesting:/bin/markdown.html/apps/my-com/components/general/blockquote/

Example Component with hepPath

You can use the servlet above with the component dialog helpPath property.
Here is an example:

The sample file will look like this:

# Blockquote
A component that displays blockquote.
## Features
- Feature One
- Feature Two
## Edit Dialog Properties
1. `./quote` (richtext) The quote text
2. `./qouteTreatment` (select) The treatment class.
    Options: `quote`, `quote blue` and `quote single-quote`
3. `./citation` (textfield) the citation text for the quote.
## Special Authoring Documentation
There are no special authoring documentation for this component

After the helpPath update, the dialog help icon will now point to the path:

And when you click on it, the following page opens:

This is more than enough for general documentation. And since Markdown is parsed to semantic HTML, it is easy and clear to read.

Things I want to explore further:

While this is a good and simple way to render markdown with minimal effort, we can clean it up further. Let’s turn it into a project, and put it on Github to integrate with your existing project. To do this, add the following features:

  1. Add a Granite Widget to display documentation markdown in a new tab on component dialogs. You can even pull the default README.mdautomatically.
  2. Create a special page to display the markdown and add a CSS library for aesthetics.

That’s it! Simple markdown in AEM! Subscribe to get more helpful tips and tricks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ahmed Musallam, Adobe Technical Lead

Ahmed is an Adobe Technical Lead and expert in the Adobe Experience Cloud.

More from this Author

Follow Us