eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

Manipulating Markdown content is a common programming task. CommonMark is a Java library that simplifies working with Markdown documents.

In this tutorial, we’ll learn how to manipulate Markdown content using the library. We’ll see how to parse Markdown into HTML and convert HTML back into Markdown. Finally, we’ll explore how to customize nodes for advanced processing.

2. commonmark-java Library

The commonmark-java library provides classes and interfaces for working with Markdown content based on the CommonMark specification. It allows us to parse Markdown into HTML and convert HTML back into Markdown. Additionally, it provides access to the Abstract Syntax Tree (AST), enabling further customization and processing.

To use the common-java library, let’s add the commonmark dependency to our pom.xml:

<dependency>
    <groupId>org.commonmark</groupId>
    <artifactId>commonmark</artifactId>
    <version>0.28.0</version>
</dependency>

The commark dependency provides classes such as Parser, HtmlRenderer, and MarkdownRenderer for Markdown processing and rendering.

Additionally, CommonMark provides extension dependencies for more advanced processing features. Examples include commonmark-ext-gfm-tables for GitHub Flavored Markdown tables and commonmark-ext-gfm-alerts for alert blocks.

3. Parsing and Rendering Markdown to HTML

Moving on, let’s see one of the most common uses of the library: parsing Markdown and rendering it as HTML.

First, let’s define a method named markDownToHtml():

public static String markDownToHtml(String markdown) {
    Parser parser = Parser.builder().build();
    Node document = parser.parse(markdown);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    return renderer.render(document);
}

Here, we create an instance of Parser to parse Markdown input into a document node. Next, we create an HtmlRenderer instance to render the parsed node as HTML.

A Node represents an element in the parsed Markdown document tree.

Then, let’s write a unit test to verify the result:

@Test
void givenMarkdownInput_whenConvertingToHtml_thenReturnRenderedHtml() {
    String html = markDownToHtml("Welcome to *Baeldung*");

    assertEquals("<p>Welcome to <em>Baeldung</em></p>\n", html);
}

In the code above, we pass a string containing Markdown syntax to markDownToHtml() method. Since Baeldung is wrapped in asterisks (*), the Markdown parser interprets it as emphasized text. Consequently, the renderer converts it to an HTML <em> element.

4. Processing Parsed Nodes

Furthermore, we can use a visitor to further process nodes in the parsed document tree. The library allows us to extend the AbstractVisitor class to process a node.

Let’s create a visitor class that counts every word in a sentence:

class WordCountVisitor extends AbstractVisitor {

    int wordCount = 0;

    @Override
    public void visit(Text text) {
        wordCount += text.getLiteral().split("\\w+").length;
        visitChildren(text);
    }
}

Next, let’s write a method that uses the visitor:

public static int processParsedNode(String markdown) {
    Parser parser = Parser.builder().build();
    Node node = parser.parse(markdown);
    WordCountVisitor visitor = new WordCountVisitor();
    node.accept(visitor);
    return visitor.wordCount;
}

In the method above, we create a WordCountVisitor object and pass it to the parsed node for processing.

Let’s write a unit test to confirm the method:

@Test
void givenMarkdownInput_whenProcessingParsedNode_thenReturnWordCount() {
    int wordCount = processParsedNode("Welcome to *Baeldung*");

    assertEquals(3, wordCount);
}

Here, we verify that the expected word count is equal to the actual word count.

5. Rendering HTML to Markdown

Furthermore, CommonMark also provides classes for rendering HTML-like document structures into Markdown format, making it a library for both HTML and Markdown processing.

Let’s see this in action by writing code that converts an HTML heading into a Markdown format:

public static String htmlToMarkDown(String htmlHeading) {
    Heading heading = new Heading();
    heading.setLevel(2);

    heading.appendChild(new Text(htmlHeading));
    Document document = new Document();
    document.appendChild(heading);

    MarkdownRenderer renderer = MarkdownRenderer.builder()
      .build();
    return renderer.render(document);
}

In the code above, we create a Heading object and set the level to 2, which represents an H2 heading. Next, we append a  Text object containing the heading content. Finally, we use the MarkdownRenderer builder to render the document as Markdown.

Next, let’s write a unit test to confirm the output:

@Test
void givenHeadingText_whenConvertingToMarkdown_thenReturnMarkdownHeading() {
    String markdown = htmlToMarkDown("Java Tutorial");

    assertEquals("## Java Tutorial\n", markdown);
}

In the code above, we verify that the renderer correctly converts the document structure into a valid Markdown output.

6. Customizing HTML Rendering

Furthermore, CommonMark allows us to customize rendered HTML attributes using the AttributeProvider interface.

Let’s see this in action by implementing a class custom image attribute provider:

public class ImageAttributeProvider implements AttributeProvider {

    @Override
    public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
        if (node instanceof Image) {
            attributes.put("class", "border");
        }
    }
}

In the code above, we create a class named ImageAttributeProvider that implements the AttributeProvider interface. Inside the setAttributes() method, we check whether the current node is an instance of Image. If it is, we add a class attribute with the value “border“.

Next, let’s write a method that applies the custom attribute provider during HTML rendering:

public static String changingHtmlAttribute(String source) {
    Parser parser = Parser.builder()
      .build();
    Node node = parser.parse(source);
    HtmlRenderer renderer = HtmlRenderer.builder()
      .attributeProviderFactory(context -> new ImageAttributeProvider())
      .build();

    return renderer.render(node);
}

In the code above, we customize the HtmlRenderer with a custom attribute provider using the attributeProviderFactory() method. Using lambda expressions, we invoke our provider for each node, allowing us to customize the generated HTML attributes. Every rendered image element receives a class=”border” attribute.

Finally, let’s write a unit test to ascertain the customized output:

@Test
void givenImageMarkdown_whenRenderingHtml_thenAddCustomClassAttribute() {
    String html = changingHtmlAttribute("![text](/url.png)");

    assertEquals("<p><img src=\"/url.png\" alt=\"text\" class=\"border\" /></p>\n", html);
}

In the test above, we verify that the custom attribute provider successfully adds the border CSS class to rendered image elements.

7. Customizing Render Node

Moreover, the library allows us to customize how specific nodes are rendered by implementing the NodeRenderer interface.

Let’s create a custom renderer for IndentedCodeBlock nodes:

public class IndentedCodeBlockNodeRenderer implements NodeRenderer {

    private final HtmlWriter html;

    public IndentedCodeBlockNodeRenderer(HtmlNodeRendererContext context) {
        this.html = context.getWriter();
    }

    @Override
    public Set<Class<? extends Node>> getNodeTypes() {
        return Set.of(IndentedCodeBlock.class);
    }

    @Override
    public void render(Node node) {
        IndentedCodeBlock codeBlock = (IndentedCodeBlock) node;
        html.line();
        html.tag("pre");
        html.text(codeBlock.getLiteral());
        html.tag("/pre");
        html.line();

    }
}

In the code above, we implement the NodeRenderer interface and specify that the renderer handles IndentedCodeBlock nodes through the getNodeTypes() method.

Then, in the render() method, we manually generate the HTML output for the code block using HtmlWriter.

Next, let’s register the custom renderer with HtmlRenderer:

public static String customizingHtmlRendering(String source) {
    Parser parser = Parser.builder()
      .build();
    Node node = parser.parse(source);
    HtmlRenderer renderer = HtmlRenderer.builder()
      .nodeRendererFactory(IndentedCodeBlockNodeRenderer::new)
      .build();

    return renderer.render(node);
}

Here, we customize the renderer using the nodeRendererFactory() method. We invoke the custom node using a method reference. This allows the renderer to delegate matching nodes to our custom implementation during HTML generation

Let’s write a unit test to verify the customizingHtmlRendering() method:

@Test
void givenIndentedCodeBlock_whenRenderingHtml_thenUseCustomNodeRenderer() {
    String html = customizingHtmlRendering("Example:\n\n    code");

    assertEquals("<p>Example:</p>\n<pre>code\n</pre>\n", html);
}

Here, we verify that indented code blocks are rendered using our custom renderer implementation.

8. Conclusion

In this article, we learned how to use the CommonMark library to parse Markdown into HTML and convert HTML back into Markdown. Additionally, we saw how to customize HTML attributes and node rendering for more advanced processing scenarios.

As always, the source code for the sample code is available over on GitHub.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
guest
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments