Skip to content

Commit 9c813f6

Browse files
committed
Remove addCustomType
1 parent d665735 commit 9c813f6

6 files changed

Lines changed: 60 additions & 66 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ 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 the set of alert types
24-
(including standard GFM types) to be completely overwritten.
23+
- **Breaking:** `AlertsExtension.Builder#addCustomType` has been changed to
24+
`setAllowedTypes` to allow the set of alert types (including standard GFM types)
25+
to be completely overwritten.
2526
```java
2627
var extension = AlertsExtension.builder()
2728
.setAllowedTypes(Map.ofEntries(
@@ -31,6 +32,15 @@ with the exception that 0.x versions can break between minor versions.
3132
))
3233
.build();
3334
```
35+
Example migration:
36+
```diff
37+
+ var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
38+
+ customTypes.put("BUG", "Known Bug");
39+
var extension = AlertsExtension.builder()
40+
- .addCustomType("BUG", "Known Bug")
41+
+ .setAllowedTypes(customTypes)
42+
.build();
43+
```
3444

3545
## [0.28.0] - 2026-03-31
3646
### Added

commonmark-ext-gfm-alerts/README.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,19 @@ var renderer = HtmlRenderer.builder().extensions(List.of(extension)).build();
2626

2727
### Custom Alert Types
2828

29-
Add custom types beyond the five standard GFM types:
29+
By default, the five standard GFM alert types are used. These can be added to or replaced
30+
with a new set of alert types:
3031

3132
```java
33+
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
34+
customTypes.put("BUG", "Known Bug");
3235
var extension = AlertsExtension.builder()
33-
.addCustomType("BUG", "Known Bug")
36+
.setAllowedTypes(customTypes)
3437
.build();
3538
```
3639

3740
Custom types must be UPPERCASE. Standard type titles can also be overridden for localization.
3841

39-
The allowed types (including the five standard GFM types) can also be completely overwritten:
40-
41-
```java
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();
49-
```
50-
5142
### Custom Alert Titles
5243

5344
Allow authors to provide custom titles per alert by adding text after the alert

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -154,30 +154,6 @@ public Builder setAllowedTypes(Map<String, String> allowedTypes) {
154154
return this;
155155
}
156156

157-
/**
158-
* Adds a custom alert type with a default title.
159-
* <p>
160-
* This can also be used to override the default title of standard GFM types
161-
* (e.g., for localization).
162-
*
163-
* @param type the alert type (must be uppercase)
164-
* @param title the default title for this alert type
165-
* @return {@code this}
166-
*/
167-
public Builder addCustomType(String type, String title) {
168-
if (type == null || type.isEmpty()) {
169-
throw new IllegalArgumentException("Type must not be null or empty");
170-
}
171-
if (title == null || title.isEmpty()) {
172-
throw new IllegalArgumentException("Title must not be null or empty");
173-
}
174-
if (!type.equals(type.toUpperCase(Locale.ROOT))) {
175-
throw new IllegalArgumentException("Type must be uppercase: " + type);
176-
}
177-
allowedTypes.put(type, title);
178-
return this;
179-
}
180-
181157
/**
182158
* Allows or disallows custom titles on alerts. Inline formatting is supported
183159
* within these titles.

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.commonmark.renderer.markdown.MarkdownRenderer;
66
import org.junit.jupiter.api.Test;
77

8+
import java.util.HashMap;
89
import java.util.Set;
910

1011
import static org.assertj.core.api.Assertions.assertThat;
@@ -40,41 +41,43 @@ public void allStandardTypesRoundTrip() {
4041
@Test
4142
public void lowercaseTypeRendersAsUppercase() {
4243
// Lowercase input gets normalized to uppercase type
43-
String rendered = RENDERER.render(PARSER.parse("> [!note]\n> Content\n"));
44+
var rendered = RENDERER.render(PARSER.parse("> [!note]\n> Content\n"));
4445
assertThat(rendered).isEqualTo("> [!NOTE]\n> Content\n");
4546
}
4647

4748
@Test
4849
public void leadingAndTrailingLinesAreRemoved() {
49-
String rendered = RENDERER.render(PARSER.parse(">\n> \n>[!NOTE]\n> Content\n>\n> \n"));
50+
var rendered = RENDERER.render(PARSER.parse(">\n> \n>[!NOTE]\n> Content\n>\n> \n"));
5051
assertThat(rendered).isEqualTo("> [!NOTE]\n> Content\n");
5152
}
5253

5354
@Test
5455
public void alertWithMultipleParagraphs() {
55-
String input = "> [!NOTE]\n> First paragraph\n>\n> Second paragraph\n";
56+
var input = "> [!NOTE]\n> First paragraph\n>\n> Second paragraph\n";
5657
// MarkdownWriter always writes the prefix including trailing space
57-
String expected = "> [!NOTE]\n> First paragraph\n> \n> Second paragraph\n";
58-
String rendered = RENDERER.render(PARSER.parse(input));
58+
var expected = "> [!NOTE]\n> First paragraph\n> \n> Second paragraph\n";
59+
var rendered = RENDERER.render(PARSER.parse(input));
5960
assertThat(rendered).isEqualTo(expected);
6061
}
6162

6263
@Test
6364
public void customTypeRoundTrip() {
64-
Extension extension = AlertsExtension.builder()
65-
.addCustomType("INFO", "Information")
65+
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
66+
customTypes.put("INFO", "Information");
67+
var extension = AlertsExtension.builder()
68+
.setAllowedTypes(customTypes)
6669
.build();
6770

68-
Parser parser = Parser.builder().extensions(Set.of(extension)).build();
69-
MarkdownRenderer renderer = MarkdownRenderer.builder().extensions(Set.of(extension)).build();
70-
String input = "> [!INFO]\n> Custom type\n";
71+
var parser = Parser.builder().extensions(Set.of(extension)).build();
72+
var renderer = MarkdownRenderer.builder().extensions(Set.of(extension)).build();
73+
var input = "> [!INFO]\n> Custom type\n";
7174

7275
assertRoundTrip(input, parser, renderer);
7376
}
7477

7578
@Test
7679
public void alertWithList() {
77-
String input = "> [!NOTE]\n> Items:\n> \n> - First\n> - Second\n";
80+
var input = "> [!NOTE]\n> Items:\n> \n> - First\n> - Second\n";
7881
assertRoundTrip(input);
7982
}
8083

@@ -92,17 +95,17 @@ public void customTitleWithFormattingRoundTrip() {
9295

9396
@Test
9497
public void customTitleWithMultipleBlocks() {
95-
String input = "> [!NOTE]Title\n> First paragraph\n>\n> Second paragraph\n>\n> - > Nested blocks\n";
98+
var input = "> [!NOTE]Title\n> First paragraph\n>\n> Second paragraph\n>\n> - > Nested blocks\n";
9699
// MarkdownWriter always writes the prefix including trailing space
97-
String expected = "> [!NOTE] Title\n> First paragraph\n> \n> Second paragraph\n> \n> - > Nested blocks\n";
98-
String rendered = RENDERER_CUSTOM_TITLES.render(PARSER_CUSTOM_TITLES.parse(input));
100+
var expected = "> [!NOTE] Title\n> First paragraph\n> \n> Second paragraph\n> \n> - > Nested blocks\n";
101+
var rendered = RENDERER_CUSTOM_TITLES.render(PARSER_CUSTOM_TITLES.parse(input));
99102
assertThat(rendered).isEqualTo(expected);
100103
}
101104

102105
// Helpers
103106

104107
private void assertRoundTrip(String input, Parser parser, MarkdownRenderer renderer) {
105-
String rendered = renderer.render(parser.parse(input));
108+
var rendered = renderer.render(parser.parse(input));
106109
assertThat(rendered).isEqualTo(input);
107110
}
108111

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.commonmark.testutil.RenderingTestCase;
1212
import org.junit.jupiter.api.Test;
1313

14+
import java.util.HashMap;
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.Set;
@@ -46,8 +47,10 @@ private void assertRenderingCustomTitles(String source, String expectedResult) {
4647

4748
@Test
4849
public void customType() {
50+
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
51+
customTypes.put("INFO", "Information");
4952
var extension = AlertsExtension.builder()
50-
.addCustomType("INFO", "Information")
53+
.setAllowedTypes(customTypes)
5154
.build();
5255

5356
var parser = Parser.builder().extensions(Set.of(extension)).build();
@@ -63,9 +66,11 @@ public void customType() {
6366
@Test
6467
public void multipleCustomTypes() {
6568
var extension = AlertsExtension.builder()
66-
.addCustomType("INFO", "Information")
67-
.addCustomType("SUCCESS", "Success!")
68-
.addCustomType("DANGER", "Danger!")
69+
.setAllowedTypes(Map.ofEntries(
70+
Map.entry("INFO", "Information"),
71+
Map.entry("SUCCESS", "Success!"),
72+
Map.entry("DANGER", "Danger!")
73+
))
6974
.build();
7075

7176
var parser = Parser.builder().extensions(Set.of(extension)).build();
@@ -88,8 +93,10 @@ public void multipleCustomTypes() {
8893

8994
@Test
9095
public void standardTypesWithCustomConfigured() {
96+
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
97+
customTypes.put("INFO", "Information");
9198
var extension = AlertsExtension.builder()
92-
.addCustomType("INFO", "Information")
99+
.setAllowedTypes(customTypes)
93100
.build();
94101

95102
var parser = Parser.builder().extensions(Set.of(extension)).build();
@@ -104,8 +111,10 @@ public void standardTypesWithCustomConfigured() {
104111

105112
@Test
106113
public void overrideStandardTypeTitle() {
114+
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
115+
customTypes.put("NOTE", "Nota");
107116
var extension = AlertsExtension.builder()
108-
.addCustomType("NOTE", "Nota")
117+
.setAllowedTypes(customTypes)
109118
.build();
110119

111120
var parser = Parser.builder().extensions(Set.of(extension)).build();
@@ -123,19 +132,19 @@ public void overrideStandardTypeTitle() {
123132
@Test
124133
public void customTypeMustBeUppercase() {
125134
assertThrows(IllegalArgumentException.class, () ->
126-
AlertsExtension.builder().addCustomType("info", "Information").build());
135+
AlertsExtension.builder().setAllowedTypes(Map.of("info", "Information")).build());
127136
}
128137

129138
@Test
130139
public void customTypeMustNotBeEmpty() {
131140
assertThrows(IllegalArgumentException.class, () ->
132-
AlertsExtension.builder().addCustomType("", "Title").build());
141+
AlertsExtension.builder().setAllowedTypes(Map.of("", "Title")).build());
133142
}
134143

135144
@Test
136145
public void customTypeTitleMustNotBeEmpty() {
137146
assertThrows(IllegalArgumentException.class, () ->
138-
AlertsExtension.builder().addCustomType("INFO", "").build());
147+
AlertsExtension.builder().setAllowedTypes(Map.of("INFO", "")).build());
139148
}
140149

141150
@Test
@@ -431,8 +440,10 @@ public void alertParsedAsAlertNode() {
431440

432441
@Test
433442
public void customTypeParsedAsAlertNode() {
443+
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
444+
customTypes.put("INFO", "Information");
434445
var extension = AlertsExtension.builder()
435-
.addCustomType("INFO", "Information")
446+
.setAllowedTypes(customTypes)
436447
.build();
437448

438449
var parser = Parser.builder().extensions(Set.of(extension)).build();

commonmark-ext-gfm-alerts/src/test/java/org/commonmark/ext/gfm/alerts/examples/AlertsExample.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.commonmark.parser.Parser;
55
import org.commonmark.renderer.html.HtmlRenderer;
66

7+
import java.util.HashMap;
78
import java.util.List;
89

910
/**
@@ -55,8 +56,10 @@ private static void customTypesExample() {
5556
System.out.println("CUSTOM ALERT TYPES");
5657
System.out.println("=".repeat(60));
5758

59+
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
60+
customTypes.put("BUG", "Known Bug");
5861
var extension = AlertsExtension.builder()
59-
.addCustomType("BUG", "Known Bug")
62+
.setAllowedTypes(customTypes)
6063
.build();
6164

6265
var parser = Parser.builder()

0 commit comments

Comments
 (0)