1515import java .util .Locale ;
1616import java .util .HashSet ;
1717import java .util .Map ;
18+ import java .util .Objects ;
1819import java .util .Set ;
1920
2021/**
@@ -51,8 +52,7 @@ public class AlertsExtension implements Parser.ParserExtension, HtmlRenderer.Htm
5152
5253 /**
5354 * The standard GitHub Flavored Markdown (GFM) types that the extension
54- * enables by default. They can be removed individually with
55- * {@link Builder#removeTypes(String...)}.
55+ * enables by default. These can be overwritten with {@link Builder#setAllowedTypes(Map)}.
5656 */
5757 public static final Map <String , String > STANDARD_TYPES = Map .ofEntries (
5858 Map .entry ("NOTE" , "Note" ),
@@ -114,10 +114,46 @@ public Set<Character> getSpecialCharacters() {
114114 * Builder for configuring the alerts extension.
115115 */
116116 public static class Builder {
117- private final Map <String , String > allowedTypes = new HashMap <>(STANDARD_TYPES );
117+ private Map <String , String > allowedTypes = new HashMap <>(STANDARD_TYPES );
118118 private boolean customTitlesAllowed = false ;
119119 private boolean nestedAlertsAllowed = false ;
120120
121+ /**
122+ * Sets which alert types will be recognized and parsed into {@link Alert} blocks,
123+ * completely overwriting any previous configuration.
124+ * <p>
125+ * By default, {@link AlertsExtension#STANDARD_TYPES} are used.
126+ *
127+ * @param allowedTypes A map of alert type to the default title for that type.
128+ * Must not be null/empty or contain any null/empty keys or
129+ * values. Additionally, all alert types must be uppercase.
130+ * @return {@code this}
131+ */
132+ public Builder setAllowedTypes (Map <String , String > allowedTypes ) {
133+ Objects .requireNonNull (allowedTypes , "allowedTypes must not be null" );
134+ if (allowedTypes .isEmpty ()) {
135+ throw new IllegalArgumentException ("allowedTypes must not be empty" );
136+ }
137+
138+ for (Map .Entry <String , String > entry : allowedTypes .entrySet ()) {
139+ var type = Objects .requireNonNull (entry .getKey (), "Types must not be null" );
140+ if (type .isEmpty ()) {
141+ throw new IllegalArgumentException ("Types must not be empty" );
142+ }
143+ if (!type .equals (type .toUpperCase (Locale .ROOT ))) {
144+ throw new IllegalArgumentException ("Types must be uppercase: " + type );
145+ }
146+
147+ var defaultTitle = Objects .requireNonNull (entry .getValue (), "Default titles must not be null: " + type );
148+ if (defaultTitle .isEmpty ()) {
149+ throw new IllegalArgumentException ("Default titles must not be empty: " + type );
150+ }
151+ }
152+
153+ this .allowedTypes = Map .copyOf (allowedTypes );
154+ return this ;
155+ }
156+
121157 /**
122158 * Adds a custom alert type with a default title.
123159 * <p>
@@ -142,36 +178,10 @@ public Builder addCustomType(String type, String title) {
142178 return this ;
143179 }
144180
145- /**
146- * Removes alert types from the allowed list.
147- *
148- * @param types the alert types to remove (must be uppercase)
149- * @return {@code this}
150- * @see AlertsExtension#STANDARD_TYPES
151- */
152- public Builder removeTypes (String ... types ) {
153- if (types == null ) {
154- throw new IllegalArgumentException ("Types must not be null" );
155- }
156-
157- for (String type : types ) {
158- if (type == null || type .isEmpty ()) {
159- throw new IllegalArgumentException ("Each type must not be null or empty" );
160- }
161-
162- if (!type .equals (type .toUpperCase (Locale .ROOT ))) {
163- throw new IllegalArgumentException ("Type must be uppercase: " + type );
164- }
165-
166- allowedTypes .remove (type );
167- }
168-
169- return this ;
170- }
171-
172181 /**
173182 * Allows or disallows custom titles on alerts. Inline formatting is supported
174183 * within these titles.
184+ *
175185 * @param allow Whether to allow or disallow custom titles on alerts.
176186 * @return {@code this}
177187 * @see AlertTitle
@@ -189,6 +199,7 @@ public Builder allowCustomTitles(boolean allow) {
189199 * <p>
190200 * Note that even when this is allowed, {@link Parser.Builder#maxOpenBlockParsers(int)}
191201 * will be respected.
202+ *
192203 * @param allow Whether to allow or disallow parsing alerts within non-root blocks.
193204 * @return {@code this}
194205 */
0 commit comments