commonmark / commonmark-java

Java library for parsing and rendering CommonMark (Markdown)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to avoid 'Unexpected close tag' issue

abhikt48 opened this issue · comments

We have a requirement to convert MD to HTML with table extension. One of the column contains array<object> and generated HTML is not valid because there is no closing tag of <object>

Can you please suggest any way to replace array<object> with array[object], or any other good representation which does not break HTML structure? I have oneway which is String manipulation after MD to HTML conversion, but I want to avoid this approach. I am expecting a better approach from the library side. Much appreciated for any suggestions.

Input

# Test Quantity Service

##### Payload

| Name | Type |
|---|---|
| constituentLocationStock | array<object> |

Generated Output

<h1>Test Quantity Service</h1>
<h5>Payload</h5>
<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>constituentLocationStock</td>
<td>array<object></td>
</tr>
</tbody>
</table>

Java Code

String content = FileUtils.readFileToString(new File("C:\\temp.md"), StandardCharsets.UTF_8);

        Extension tableExtension = TablesExtension.create();

        List<Extension> extensions = Arrays.asList(tableExtension);
        Parser parser = Parser.builder().extensions(extensions).build();
        HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();

        Node document = parser.parse((String) content);
        String mdTohtml = renderer.render(document);
        System.out.println("mdTohtml -" + mdTohtml);

Used Library

<dependency>
			<groupId>org.commonmark</groupId>
			<artifactId>commonmark</artifactId>
			<version>0.18.1</version>
		</dependency>
		<dependency>
			<groupId>org.commonmark</groupId>
			<artifactId>commonmark-ext-gfm-tables</artifactId>
			<version>0.18.1</version>
		</dependency>

Expectation
Replace array<object> to array[object], or any other good representation which does not break HTML structure.

Please let me know for any information.

You need to create create your renderer with escapeHtml(true) if you want to escape HTML:

HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).extensions(extensions).build();