1. Project Clover database mer. févr. 4 2026 12:48:28 CET
  2. Package org.apache.maven.doxia.module.markdown

File FlexmarkParserTest.java

 

Code metrics

0
18
3
1
101
60
4
0,22
6
3
1,33

Classes

Class Line # Actions
FlexmarkParserTest 35 18 0% 4 1
0.9523809695,2%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * Copyright 2012-2025 Christophe Friederich
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package org.apache.maven.doxia.module.markdown;
17   
18    import com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension;
19    import com.vladsch.flexmark.ext.autolink.AutolinkExtension;
20    import com.vladsch.flexmark.ext.definition.DefinitionExtension;
21    import com.vladsch.flexmark.ext.escaped.character.EscapedCharacterExtension;
22    import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
23    import com.vladsch.flexmark.ext.tables.TablesExtension;
24    import com.vladsch.flexmark.ext.typographic.TypographicExtension;
25    import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension;
26    import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterExtension;
27    import com.vladsch.flexmark.html.HtmlRenderer;
28    import com.vladsch.flexmark.util.ast.Node;
29    import com.vladsch.flexmark.util.data.MutableDataSet;
30    import java.io.IOException;
31    import java.util.Arrays;
32    import org.devacfr.testing.jupiter.TestCase;
33    import org.junit.jupiter.api.Test;
34   
 
35    public class FlexmarkParserTest extends TestCase {
36   
37    /**
38    * Flexmark's Markdown parser (one static instance fits all)
39    */
40    private static final com.vladsch.flexmark.parser.Parser FLEXMARK_PARSER;
41   
42    /**
43    * Flexmark's HTML renderer (its output will be re-parsed and converted to Sink events)
44    */
45    private static final HtmlRenderer FLEXMARK_HTML_RENDERER;
46   
47    // Initialize the Flexmark parser and renderer, once and for all
 
48  2 toggle static {
49  2 MutableDataSet flexmarkOptions = new MutableDataSet();
50   
51    // Enable the extensions that we used to have in Pegdown
52  2 flexmarkOptions.set(com.vladsch.flexmark.parser.Parser.EXTENSIONS,
53    Arrays.asList(EscapedCharacterExtension.create(),
54    AbbreviationExtension.create(),
55    AutolinkExtension.create(),
56    DefinitionExtension.create(),
57    TypographicExtension.create(),
58    TablesExtension.create(),
59    WikiLinkExtension.create(),
60    StrikethroughExtension.create()));
61   
62    // Disable wrong apostrophe replacement
63  2 flexmarkOptions.set(TypographicExtension.SINGLE_QUOTE_UNMATCHED, "'");
64   
65    // Additional options on the HTML rendering
66  2 flexmarkOptions.set(HtmlRenderer.HTML_BLOCK_OPEN_TAG_EOL, false);
67  2 flexmarkOptions.set(HtmlRenderer.HTML_BLOCK_CLOSE_TAG_EOL, false);
68  2 flexmarkOptions.set(HtmlRenderer.MAX_TRAILING_BLANK_LINES, -1);
69   
70    // Build the Markdown parser
71  2 FLEXMARK_PARSER = com.vladsch.flexmark.parser.Parser.builder(flexmarkOptions).build();
72   
73  2 MutableDataSet flexmarkMetadataOptions = new MutableDataSet();
74  2 flexmarkMetadataOptions.set(com.vladsch.flexmark.parser.Parser.EXTENSIONS,
75    Arrays.asList(YamlFrontMatterExtension.create()));
76   
77    // Build the HTML renderer
78  2 FLEXMARK_HTML_RENDERER = HtmlRenderer.builder(flexmarkOptions)
79    .linkResolverFactory(new FlexmarkDoxiaLinkResolver.Factory())
80    .build();
81    }
82   
 
83  2 toggle @Test
84    public void shouldConvertMarkdown() {
85  2 verify((content) -> {
86  2 StringBuilder html = new StringBuilder();
87  2 try {
88  2 return render(content, html);
89    } catch (IOException e) {
90  0 throw new RuntimeException(e);
91    }
92    }, "md", "html");
93    }
94   
 
95  2 toggle private String render(String content, Appendable html) throws IOException {
96  2 Node documentRoot = FLEXMARK_PARSER.parse(content);
97  2 FLEXMARK_HTML_RENDERER.render(documentRoot, html);
98  2 return html.toString();
99   
100    }
101    }