1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.metaschema.core.datatype.markup.flexmark;
28
29 import com.vladsch.flexmark.ext.escaped.character.EscapedCharacterExtension;
30 import com.vladsch.flexmark.ext.gfm.strikethrough.SubscriptExtension;
31 import com.vladsch.flexmark.ext.superscript.SuperscriptExtension;
32 import com.vladsch.flexmark.ext.tables.TablesExtension;
33 import com.vladsch.flexmark.ext.typographic.TypographicExtension;
34 import com.vladsch.flexmark.formatter.Formatter;
35 import com.vladsch.flexmark.html.HtmlRenderer;
36 import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
37 import com.vladsch.flexmark.parser.Parser;
38 import com.vladsch.flexmark.parser.ParserEmulationProfile;
39 import com.vladsch.flexmark.util.data.DataHolder;
40 import com.vladsch.flexmark.util.data.DataSet;
41 import com.vladsch.flexmark.util.data.MutableDataSet;
42 import com.vladsch.flexmark.util.format.options.ListBulletMarker;
43 import com.vladsch.flexmark.util.misc.Extension;
44
45 import java.util.List;
46 import java.util.Map;
47 import java.util.concurrent.ConcurrentHashMap;
48
49 import edu.umd.cs.findbugs.annotations.NonNull;
50 import edu.umd.cs.findbugs.annotations.Nullable;
51
52 public final class FlexmarkConfiguration {
53
54 @NonNull
55 private static final ParserEmulationProfile PARSER_PROFILE = ParserEmulationProfile.COMMONMARK_0_29;
56
57 @NonNull
58 public static final DataSet FLEXMARK_CONFIG = initFlexmarkConfig();
59
60 @SuppressWarnings("null")
61 @NonNull
62 private static DataSet initFlexmarkConfig() {
63 MutableDataSet options = new MutableDataSet();
64 options.setFrom(PARSER_PROFILE);
65
66 List<Extension> extensions = List.of(
67
68 InsertAnchorExtension.create(),
69
70 HtmlQuoteTagExtension.create(),
71 TypographicExtension.create(),
72 TablesExtension.create(),
73
74 HtmlCodeRenderExtension.create(),
75
76 EscapedCharacterExtension.create(),
77 SuperscriptExtension.create(),
78 SubscriptExtension.create()
79
80 );
81 Parser.EXTENSIONS.set(options, extensions);
82
83
84 Parser.FENCED_CODE_CONTENT_BLOCK.set(options, true);
85
86
87
88
89
90
91 Parser.ASTERISK_DELIMITER_PROCESSOR.set(options, false);
92
93
94 TablesExtension.COLUMN_SPANS.set(options, false);
95 TablesExtension.APPEND_MISSING_COLUMNS.set(options, true);
96 TablesExtension.DISCARD_EXTRA_COLUMNS.set(options, true);
97 TablesExtension.HEADER_SEPARATOR_COLUMN_MATCH.set(options, true);
98
99
100 TypographicExtension.ENABLE_SMARTS.set(options, false);
101 TypographicExtension.SINGLE_QUOTE_UNMATCHED.set(options, "'");
102 TypographicExtension.DOUBLE_QUOTE_OPEN.set(options, "\"");
103 TypographicExtension.DOUBLE_QUOTE_CLOSE.set(options, "\"");
104
105 Map<String, String> typographicReplacementMap = new ConcurrentHashMap<>();
106 typographicReplacementMap.put("“", "\"");
107 typographicReplacementMap.put("”", "\"");
108 typographicReplacementMap.put("“", "“");
109 typographicReplacementMap.put("”", "”");
110
111
112 typographicReplacementMap.put("‘", "‘");
113 typographicReplacementMap.put("’", "’");
114 typographicReplacementMap.put("'", "’");
115
116 typographicReplacementMap.put("«", "«");
117
118 typographicReplacementMap.put("»", "»");
119
120 typographicReplacementMap.put("…", "…");
121
122 typographicReplacementMap.put("&endash;", "–");
123
124 typographicReplacementMap.put("&emdash;", "—");
125
126 FlexmarkHtmlConverter.TYPOGRAPHIC_REPLACEMENT_MAP.set(options, typographicReplacementMap);
127 FlexmarkHtmlConverter.OUTPUT_UNKNOWN_TAGS.set(options, true);
128 FlexmarkHtmlConverter.SETEXT_HEADINGS.set(options, false);
129
130 FlexmarkHtmlConverter.BR_AS_EXTRA_BLANK_LINES.set(options, false);
131
132
133
134
135 Formatter.MAX_TRAILING_BLANK_LINES.set(options, -1);
136 Formatter.LIST_BULLET_MARKER.set(options, ListBulletMarker.DASH);
137
138 HtmlRenderer.MAX_TRAILING_BLANK_LINES.set(options, -1);
139 HtmlRenderer.UNESCAPE_HTML_ENTITIES.set(options, true);
140 HtmlRenderer.PERCENT_ENCODE_URLS.set(options, true);
141
142
143
144
145
146 return options.toImmutable();
147 }
148
149 public static DataSet newFlexmarkConfig(@Nullable DataHolder options) {
150 return options == null ? FLEXMARK_CONFIG : DataSet.merge(FLEXMARK_CONFIG, options);
151 }
152
153 private FlexmarkConfiguration() {
154
155 }
156
157 }