22
33import java .util .Locale ;
44import java .util .Set ;
5- import java .util .regex .Matcher ;
65import java .util .regex .Pattern ;
76
87import org .commonmark .ext .gfm .alerts .Alert ;
98import org .commonmark .ext .gfm .alerts .AlertTitle ;
109import org .commonmark .node .Block ;
1110import org .commonmark .node .BlockQuote ;
1211import org .commonmark .node .Document ;
13- import org .commonmark .node .Paragraph ;
1412import org .commonmark .parser .InlineParser ;
1513import org .commonmark .parser .SourceLine ;
1614import org .commonmark .parser .SourceLines ;
@@ -28,12 +26,10 @@ public class AlertBlockParser extends AbstractBlockParser {
2826 private static final Pattern ALERT_PATTERN_CUSTOM_TITLE = Pattern .compile ("^\\ [!([a-zA-Z]+)](.*)$" );
2927
3028 private final Alert block ;
31- private final String typeOriginalCase ;
3229 private final String titleContent ;
3330
34- private AlertBlockParser (String type , String typeOriginalCase , String titleContent ) {
31+ private AlertBlockParser (String type , String titleContent ) {
3532 this .block = new Alert (type );
36- this .typeOriginalCase = typeOriginalCase ;
3733 this .titleContent = titleContent ;
3834 }
3935
@@ -76,16 +72,6 @@ public BlockContinue tryContinue(ParserState state) {
7672
7773 @ Override
7874 public void parseInlines (InlineParser inlineParser ) {
79- // Determine if there is any non-title body content.
80- if (block .getFirstChild () == null ) {
81- /*
82- * Replace the Alert with a BlockQuote whose only paragraph contains
83- * the original first line text.
84- */
85- demoteToBlockQuote (inlineParser );
86- return ;
87- }
88-
8975 if (titleContent .isEmpty ()) {
9076 return ;
9177 }
@@ -104,28 +90,6 @@ public void parseInlines(InlineParser inlineParser) {
10490 block .prependChild (titleNode );
10591 }
10692
107- private void demoteToBlockQuote (InlineParser inlineParser ) {
108- var bq = new BlockQuote ();
109- bq .setSourceSpans (block .getSourceSpans ());
110- var p = new Paragraph ();
111-
112- // Build the literal text including the alert marker and title.
113- var literal = "[!" + typeOriginalCase + "]" ;
114- if (!titleContent .isEmpty ()) {
115- /*
116- * This may not preserve the original number of spaces between the
117- * alert marker and title (e.g., if there were 0 or 2+ spaces).
118- */
119- literal += " " + titleContent ;
120- }
121-
122- // Parse the inlines of the full content (alert marker + title)
123- inlineParser .parse (SourceLines .of (SourceLine .of (literal , null )), p );
124- bq .appendChild (p );
125- block .insertAfter (bq );
126- block .unlink ();
127- }
128-
12993 public static class Factory extends AbstractBlockParserFactory {
13094
13195 private final Set <String > allowedTypes ;
@@ -212,8 +176,7 @@ private BlockStart tryStartFresh(CharSequence line, int nextNonSpace, ParserStat
212176 return BlockStart .none ();
213177 }
214178
215- var typeOriginalCase = matcher .group (1 );
216- var type = typeOriginalCase .toUpperCase (Locale .ROOT );
179+ var type = matcher .group (1 ).toUpperCase (Locale .ROOT );
217180 if (!allowedTypes .contains (type )) {
218181 return BlockStart .none ();
219182 }
@@ -224,7 +187,7 @@ private BlockStart tryStartFresh(CharSequence line, int nextNonSpace, ParserStat
224187 }
225188
226189 // Consume the rest of the first line.
227- var start = BlockStart .of (new AlertBlockParser (type , typeOriginalCase , titleContent )).atIndex (line .length ());
190+ var start = BlockStart .of (new AlertBlockParser (type , titleContent )).atIndex (line .length ());
228191
229192 // If we got here via the promotion path, replace the empty BlockQuote.
230193 var matched = state .getActiveBlockParser ().getBlock ();
0 commit comments