Skip to content

Commit d665735

Browse files
committed
Change removeTypes to setAllowedTypes
1 parent 4256dc7 commit d665735

4 files changed

Lines changed: 61 additions & 37 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ with the exception that 0.x versions can break between minor versions.
2020
other blocks (including other alerts). See
2121
[this section of the alerts README](./commonmark-ext-gfm-alerts/README.md#nesting-alerts)
2222
for more information.
23-
- New configuration for `AlertsExtension` to allow alert types (including standard
24-
GFM types) to be removed (disallowed).
23+
- New configuration for `AlertsExtension` to allow the set of alert types
24+
(including standard GFM types) to be completely overwritten.
2525
```java
26-
var extension = AlertsExtension.builder().removeTypes("NOTE", "TIP").build();
26+
var extension = AlertsExtension.builder()
27+
.setAllowedTypes(Map.ofEntries(
28+
Map.entry("IMPORTANT", "Important"),
29+
Map.entry("WARNING", "Warning")
30+
Map.entry("BUG", "Known Bug")
31+
))
32+
.build();
2733
```
2834

2935
## [0.28.0] - 2026-03-31

commonmark-ext-gfm-alerts/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ var extension = AlertsExtension.builder()
3636

3737
Custom types must be UPPERCASE. Standard type titles can also be overridden for localization.
3838

39-
If any types (including the five standard GFM types) aren't desired, they can be
40-
removed (disallowed):
39+
The allowed types (including the five standard GFM types) can also be completely overwritten:
4140

4241
```java
43-
var extension = AlertsExtension.builder().removeTypes("NOTE", "TIP").build();
42+
var extension = AlertsExtension.builder()
43+
.setAllowedTypes(Map.ofEntries(
44+
Map.entry("IMPORTANT", "Important"),
45+
Map.entry("WARNING", "Warning")
46+
Map.entry("BUG", "Known Bug")
47+
))
48+
.build();
4449
```
4550

4651
### Custom Alert Titles

commonmark-ext-gfm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/AlertsExtension.java

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Locale;
1616
import java.util.HashSet;
1717
import java.util.Map;
18+
import java.util.Objects;
1819
import 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
*/

commonmark-ext-gfm-alerts/src/test/java/org/commonmark/ext/gfm/alerts/AlertsTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.jupiter.api.Test;
1313

1414
import java.util.List;
15+
import java.util.Map;
1516
import java.util.Set;
1617

1718
import static org.assertj.core.api.Assertions.assertThat;
@@ -139,7 +140,8 @@ public void customTypeTitleMustNotBeEmpty() {
139140

140141
@Test
141142
public void removeStandardTypes() {
142-
var extension = AlertsExtension.builder().removeTypes("NOTE", "TIP").build();
143+
var allowedTypes = Map.ofEntries(Map.entry("IMPORTANT", "Important"));
144+
var extension = AlertsExtension.builder().setAllowedTypes(allowedTypes).build();
143145
var parser = Parser.builder().extensions(Set.of(extension)).build();
144146
var renderer = HtmlRenderer.builder().extensions(Set.of(extension)).build();
145147

0 commit comments

Comments
 (0)