Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions libraries-formatting/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-formatting</artifactId>

<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>${commonmark.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<commonmark.version>0.28.0</commonmark.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.baeldung.commonmark;

import org.commonmark.node.Document;
import org.commonmark.node.Heading;
import org.commonmark.node.Node;
import org.commonmark.node.Text;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.renderer.markdown.MarkdownRenderer;

public class CommonMarkUsage {

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

}

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;

}

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);
}

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);
}

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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.commonmark;

import java.util.Map;

import org.commonmark.node.Image;
import org.commonmark.node.Node;
import org.commonmark.renderer.html.AttributeProvider;

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");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.commonmark;

import java.util.Set;

import org.commonmark.node.IndentedCodeBlock;
import org.commonmark.node.Node;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.HtmlNodeRendererContext;
import org.commonmark.renderer.html.HtmlWriter;

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();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.commonmark;

import org.commonmark.node.AbstractVisitor;
import org.commonmark.node.Text;

public class WordCountVisitor extends AbstractVisitor {

int wordCount = 0;

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



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.commonmark;

import static com.baeldung.commonmark.CommonMarkUsage.changingHtmlAttribute;
import static com.baeldung.commonmark.CommonMarkUsage.customizingHtmlRendering;
import static com.baeldung.commonmark.CommonMarkUsage.htmlToMarkDown;
import static com.baeldung.commonmark.CommonMarkUsage.markDownToHtml;
import static com.baeldung.commonmark.CommonMarkUsage.processParsedNode;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class CommonMarkUsageUnitTest {

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

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

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

assertEquals(3, wordCount);
}

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

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

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

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

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

assertEquals("<p>Example:</p>\n<pre>code\n</pre>\n", html);
}
}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@
<module>libraries-data-io-2</module>
<module>libraries-data-mariadb4j</module>
<module>libraries-files</module>
<module>libraries-formatting</module>
<module>libraries-http</module>
<module>libraries-http-2</module>
<module>libraries-http-3</module>
Expand Down Expand Up @@ -1197,6 +1198,7 @@
<module>libraries-data-io-2</module>
<module>libraries-data-mariadb4j</module>
<module>libraries-files</module>
<module>libraries-formatting</module>
<module>libraries-http</module>
<module>libraries-http-2</module>
<module>libraries-http-3</module>
Expand Down