Skip to content

jiangtj-lab/commonmark-spring-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Commonmark Spring Starter

A spring boot starter wapper for commonmark

Install

1/2: Add this to pom.xml:

<dependency>
  <groupId>com.jiangtj.common</groupId>
  <artifactId>commonmark-spring-starter</artifactId>
  <version>${last-version}</version>
</dependency>

2/2: Configuring github packages

configuring-apache-maven-for-use-with-github-packages

Usage

We provide Parser and HtmlRenderer instance, you can easily inject

@Resource
private Parser parser;
@Resource
private HtmlRenderer renderer;

Node document = parser.parse("This is *Sparta*");
renderer.render(document);

Normally, we don't care about the middle process, so you can use CommonMark simplified

@Resource
private CommonMark commonMark;

commonMark.render(document); // == parser.parse() and renderer.render()

Custom

Here are some default configurations for the HtmlRenderer, which you can override in the configuration file if needed.

commonmark.softbreak=\\n
commonmark.escapeHtml=false
commonmark.sanitizeUrls=false
commonmark.percentEncodeUrls=false

If there is an instance of AttributeProviderFactory or HtmlNodeRendererFactory, it will be automatically configured

@Configuration
class AttributeBean {
    @Bean
    public AttributeProviderFactory imageAttributeProvider() {
        return context -> new AttributeProviderTests.ImageAttributeProvider();
    }
}

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

Extension is the same, only need to provide an extended instance.

<dependency>
    <groupId>org.commonmark</groupId>
    <artifactId>commonmark-ext-task-list-items</artifactId>
    <version>0.18.2</version>
</dependency>
@Configuration
public static class ExtensionBeans {
    @Bean
    public Extension taskListItemsExtension() {
        return TaskListItemsExtension.create();
    }
}

You can see how to use it in the test case