Skip to content

Commit 55004c8

Browse files
committed
Revert "Remove addCustomType"
This reverts commit 9c813f6.
1 parent 9c813f6 commit 55004c8

6 files changed

Lines changed: 66 additions & 60 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ 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-
- **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.
23+
- New configuration for `AlertsExtension` to allow the set of alert types
24+
(including standard GFM types) to be completely overwritten.
2625
```java
2726
var extension = AlertsExtension.builder()
2827
.setAllowedTypes(Map.ofEntries(
@@ -32,15 +31,6 @@ with the exception that 0.x versions can break between minor versions.
3231
))
3332
.build();
3433
```
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-
```
4434

4535
## [0.28.0] - 2026-03-31
4636
### Added

commonmark-ext-gfm-alerts/README.md

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

2727
### Custom Alert Types
2828

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:
29+
Add custom types beyond the five standard GFM types:
3130

3231
```java
33-
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
34-
customTypes.put("BUG", "Known Bug");
3532
var extension = AlertsExtension.builder()
36-
.setAllowedTypes(customTypes)
33+
.addCustomType("BUG", "Known Bug")
3734
.build();
3835
```
3936

4037
Custom types must be UPPERCASE. Standard type titles can also be overridden for localization.
4138

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+
4251
### Custom Alert Titles
4352

4453
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ 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+
157181
/**
158182
* Allows or disallows custom titles on alerts. Inline formatting is supported
159183
* within these titles.

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

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

8-
import java.util.HashMap;
98
import java.util.Set;
109

1110
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,43 +40,41 @@ public void allStandardTypesRoundTrip() {
4140
@Test
4241
public void lowercaseTypeRendersAsUppercase() {
4342
// Lowercase input gets normalized to uppercase type
44-
var rendered = RENDERER.render(PARSER.parse("> [!note]\n> Content\n"));
43+
String rendered = RENDERER.render(PARSER.parse("> [!note]\n> Content\n"));
4544
assertThat(rendered).isEqualTo("> [!NOTE]\n> Content\n");
4645
}
4746

4847
@Test
4948
public void leadingAndTrailingLinesAreRemoved() {
50-
var rendered = RENDERER.render(PARSER.parse(">\n> \n>[!NOTE]\n> Content\n>\n> \n"));
49+
String rendered = RENDERER.render(PARSER.parse(">\n> \n>[!NOTE]\n> Content\n>\n> \n"));
5150
assertThat(rendered).isEqualTo("> [!NOTE]\n> Content\n");
5251
}
5352

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

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

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";
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";
7471

7572
assertRoundTrip(input, parser, renderer);
7673
}
7774

7875
@Test
7976
public void alertWithList() {
80-
var input = "> [!NOTE]\n> Items:\n> \n> - First\n> - Second\n";
77+
String input = "> [!NOTE]\n> Items:\n> \n> - First\n> - Second\n";
8178
assertRoundTrip(input);
8279
}
8380

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

9693
@Test
9794
public void customTitleWithMultipleBlocks() {
98-
var input = "> [!NOTE]Title\n> First paragraph\n>\n> Second paragraph\n>\n> - > Nested blocks\n";
95+
String input = "> [!NOTE]Title\n> First paragraph\n>\n> Second paragraph\n>\n> - > Nested blocks\n";
9996
// MarkdownWriter always writes the prefix including trailing space
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));
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));
10299
assertThat(rendered).isEqualTo(expected);
103100
}
104101

105102
// Helpers
106103

107104
private void assertRoundTrip(String input, Parser parser, MarkdownRenderer renderer) {
108-
var rendered = renderer.render(parser.parse(input));
105+
String rendered = renderer.render(parser.parse(input));
109106
assertThat(rendered).isEqualTo(input);
110107
}
111108

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

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

14-
import java.util.HashMap;
1514
import java.util.List;
1615
import java.util.Map;
1716
import java.util.Set;
@@ -47,10 +46,8 @@ private void assertRenderingCustomTitles(String source, String expectedResult) {
4746

4847
@Test
4948
public void customType() {
50-
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
51-
customTypes.put("INFO", "Information");
5249
var extension = AlertsExtension.builder()
53-
.setAllowedTypes(customTypes)
50+
.addCustomType("INFO", "Information")
5451
.build();
5552

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

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

9489
@Test
9590
public void standardTypesWithCustomConfigured() {
96-
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
97-
customTypes.put("INFO", "Information");
9891
var extension = AlertsExtension.builder()
99-
.setAllowedTypes(customTypes)
92+
.addCustomType("INFO", "Information")
10093
.build();
10194

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

112105
@Test
113106
public void overrideStandardTypeTitle() {
114-
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
115-
customTypes.put("NOTE", "Nota");
116107
var extension = AlertsExtension.builder()
117-
.setAllowedTypes(customTypes)
108+
.addCustomType("NOTE", "Nota")
118109
.build();
119110

120111
var parser = Parser.builder().extensions(Set.of(extension)).build();
@@ -132,19 +123,19 @@ public void overrideStandardTypeTitle() {
132123
@Test
133124
public void customTypeMustBeUppercase() {
134125
assertThrows(IllegalArgumentException.class, () ->
135-
AlertsExtension.builder().setAllowedTypes(Map.of("info", "Information")).build());
126+
AlertsExtension.builder().addCustomType("info", "Information").build());
136127
}
137128

138129
@Test
139130
public void customTypeMustNotBeEmpty() {
140131
assertThrows(IllegalArgumentException.class, () ->
141-
AlertsExtension.builder().setAllowedTypes(Map.of("", "Title")).build());
132+
AlertsExtension.builder().addCustomType("", "Title").build());
142133
}
143134

144135
@Test
145136
public void customTypeTitleMustNotBeEmpty() {
146137
assertThrows(IllegalArgumentException.class, () ->
147-
AlertsExtension.builder().setAllowedTypes(Map.of("INFO", "")).build());
138+
AlertsExtension.builder().addCustomType("INFO", "").build());
148139
}
149140

150141
@Test
@@ -440,10 +431,8 @@ public void alertParsedAsAlertNode() {
440431

441432
@Test
442433
public void customTypeParsedAsAlertNode() {
443-
var customTypes = new HashMap<>(AlertsExtension.STANDARD_TYPES);
444-
customTypes.put("INFO", "Information");
445434
var extension = AlertsExtension.builder()
446-
.setAllowedTypes(customTypes)
435+
.addCustomType("INFO", "Information")
447436
.build();
448437

449438
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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.commonmark.parser.Parser;
55
import org.commonmark.renderer.html.HtmlRenderer;
66

7-
import java.util.HashMap;
87
import java.util.List;
98

109
/**
@@ -56,10 +55,8 @@ private static void customTypesExample() {
5655
System.out.println("CUSTOM ALERT TYPES");
5756
System.out.println("=".repeat(60));
5857

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

6562
var parser = Parser.builder()

0 commit comments

Comments
 (0)