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