11package org .commonmark .ext .gfm .alerts ;
22
33import org .commonmark .Extension ;
4- import org .commonmark .ext .gfm .alerts .internal .AlertPostProcessor ;
4+ import org .commonmark .ext .gfm .alerts .internal .AlertBlockParser ;
55import org .commonmark .ext .gfm .alerts .internal .AlertHtmlNodeRenderer ;
66import org .commonmark .ext .gfm .alerts .internal .AlertMarkdownNodeRenderer ;
77import org .commonmark .parser .Parser ;
@@ -33,9 +33,13 @@ public class AlertsExtension implements Parser.ParserExtension, HtmlRenderer.Htm
3333 static final Set <String > STANDARD_TYPES = Set .of ("NOTE" , "TIP" , "IMPORTANT" , "WARNING" , "CAUTION" );
3434
3535 private final Map <String , String > customTypes ;
36+ private final boolean customTitlesAllowed ;
37+ private final boolean nestedAlertsAllowed ;
3638
3739 private AlertsExtension (Builder builder ) {
3840 this .customTypes = new HashMap <>(builder .customTypes );
41+ this .customTitlesAllowed = builder .customTitlesAllowed ;
42+ this .nestedAlertsAllowed = builder .nestedAlertsAllowed ;
3943 }
4044
4145 public static Extension create () {
@@ -50,7 +54,8 @@ public static Builder builder() {
5054 public void extend (Parser .Builder parserBuilder ) {
5155 var allowedTypes = new HashSet <>(STANDARD_TYPES );
5256 allowedTypes .addAll (customTypes .keySet ());
53- parserBuilder .postProcessor (new AlertPostProcessor (allowedTypes ));
57+ parserBuilder .customBlockParserFactory (
58+ new AlertBlockParser .Factory (allowedTypes , customTitlesAllowed , nestedAlertsAllowed ));
5459 }
5560
5661 @ Override
@@ -83,6 +88,8 @@ public Set<Character> getSpecialCharacters() {
8388 */
8489 public static class Builder {
8590 private final Map <String , String > customTypes = new HashMap <>();
91+ private boolean customTitlesAllowed = false ;
92+ private boolean nestedAlertsAllowed = false ;
8693
8794 /**
8895 * Adds a custom alert type with a display title.
@@ -108,6 +115,46 @@ public Builder addCustomType(String type, String title) {
108115 return this ;
109116 }
110117
118+ /**
119+ * Allows custom titles on alerts. See {@link AlertTitle} for more information.
120+ * @return {@code this}
121+ */
122+ public Builder allowCustomTitles () {
123+ customTitlesAllowed = true ;
124+ return this ;
125+ }
126+
127+ /**
128+ * Disallows custom titles on alerts. See {@link AlertTitle} for more information.
129+ * @return {@code this}
130+ */
131+ public Builder disallowCustomTitles () {
132+ customTitlesAllowed = false ;
133+ return this ;
134+ }
135+
136+ /**
137+ * Allows alerts to be parsed within blocks other than {@code Document} (the root).
138+ * <p>
139+ * Note that even with this enabled, {@link Parser.Builder#maxOpenBlockParsers(int)}
140+ * will be respected.
141+ * @return {@code this}
142+ */
143+ public Builder allowNestedAlerts () {
144+ nestedAlertsAllowed = true ;
145+ return this ;
146+ }
147+
148+ /**
149+ * Prevents alerts from being parsed within blocks other than {@code Document}
150+ * (the root). If they appear, they will be parsed as regular {@code BlockQuote}s.
151+ * @return {@code this}
152+ */
153+ public Builder disallowNestedAlerts () {
154+ nestedAlertsAllowed = false ;
155+ return this ;
156+ }
157+
111158 /**
112159 * @return a configured {@link Extension}
113160 */
0 commit comments