Skip to content

Commit 253f5d7

Browse files
committed
Allow Alerts With No Body
1 parent 74f4a8a commit 253f5d7

5 files changed

Lines changed: 19 additions & 114 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
* the {@code AlertTitle} contains a {@code Text} node ({@code "Custom "}) followed
1919
* by an {@code Emphasis} node wrapping {@code "title"}.
2020
*
21-
* @see AlertsExtension.Builder#allowCustomTitles()
22-
* @see AlertsExtension.Builder#disallowCustomTitles()
21+
* @see AlertsExtension.Builder#allowCustomTitles(boolean)
2322
*/
2423
public class AlertTitle extends CustomNode {
2524
}

commonmark-ext-gfm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/internal/AlertBlockParser.java

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import java.util.Locale;
44
import java.util.Set;
5-
import java.util.regex.Matcher;
65
import java.util.regex.Pattern;
76

87
import org.commonmark.ext.gfm.alerts.Alert;
98
import org.commonmark.ext.gfm.alerts.AlertTitle;
109
import org.commonmark.node.Block;
1110
import org.commonmark.node.BlockQuote;
1211
import org.commonmark.node.Document;
13-
import org.commonmark.node.Paragraph;
1412
import org.commonmark.parser.InlineParser;
1513
import org.commonmark.parser.SourceLine;
1614
import 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();

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,19 @@ public void customTitleIgnoresBlockContent() {
214214

215215
@Test
216216
public void customTitleNoBody() {
217-
// Inlines should be parsed as usual after falling back to a block quote.
217+
// Alerts with no body are allowed.
218218
assertRenderingCustomTitles("> [!NOTE] Custom _title_\n> \n>\n>",
219-
"<blockquote>\n" +
220-
"<p>[!NOTE] Custom <em>title</em></p>\n" +
221-
"</blockquote>\n");
219+
"<div class=\"markdown-alert markdown-alert-note\" data-alert-type=\"note\">\n" +
220+
"<p class=\"markdown-alert-title\">Custom <em>title</em></p>\n" +
221+
"</div>\n");
222222
}
223223

224224
@Test
225225
public void customTitleNoBodyNoSpace() {
226-
assertRenderingCustomTitles("> [!NOTE] Custom _title_\n> \n>\n>",
227-
"<blockquote>\n" +
228-
"<p>[!NOTE] Custom <em>title</em></p>\n" +
229-
"</blockquote>\n");
226+
assertRenderingCustomTitles("> [!NOTE] Custom _title_",
227+
"<div class=\"markdown-alert markdown-alert-note\" data-alert-type=\"note\">\n" +
228+
"<p class=\"markdown-alert-title\">Custom <em>title</em></p>\n" +
229+
"</div>\n");
230230
}
231231

232232
@Test
@@ -252,18 +252,18 @@ public void onlyHtmlCommentsInTitleUseDefaultTitle() {
252252
@Test
253253
public void noLazyContinuationAfterMarker() {
254254
assertRendering("> [!NOTE]\nBody text",
255-
"<blockquote>\n" +
256-
"<p>[!NOTE]</p>\n" +
257-
"</blockquote>\n" +
255+
"<div class=\"markdown-alert markdown-alert-note\" data-alert-type=\"note\">\n" +
256+
"<p class=\"markdown-alert-title\">Note</p>\n" +
257+
"</div>\n" +
258258
"<p>Body text</p>\n");
259259
}
260260

261261
@Test
262262
public void noLazyContinuationAfterTitle() {
263263
assertRenderingCustomTitles("> [!NOTE] Custom title\nBody text",
264-
"<blockquote>\n" +
265-
"<p>[!NOTE] Custom title</p>\n" +
266-
"</blockquote>\n" +
264+
"<div class=\"markdown-alert markdown-alert-note\" data-alert-type=\"note\">\n" +
265+
"<p class=\"markdown-alert-title\">Custom title</p>\n" +
266+
"</div>\n" +
267267
"<p>Body text</p>\n");
268268
}
269269

commonmark-ext-gfm-alerts/src/test/resources/alerts-spec-template.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,6 @@ Unconfigured custom type is not an alert:
134134
> Should be blockquote
135135
````````````````````````````````
136136

137-
Marker with no content:
138-
139-
```````````````````````````````` example alert
140-
> [!NOTE]
141-
````````````````````````````````
142-
143-
Whitespace-only content after marker:
144-
145-
```````````````````````````````` example alert
146-
> [!TIP]
147-
>
148-
>
149-
````````````````````````````````
150-
151137
Extra space inside marker:
152138

153139
```````````````````````````````` example alert
@@ -198,14 +184,6 @@ Leading spaces before blockquote marker:
198184
> Content
199185
````````````````````````````````
200186

201-
Blank line after marker ends the blockquote (not an alert):
202-
203-
```````````````````````````````` example alert
204-
> [!NOTE]
205-
206-
Some text
207-
````````````````````````````````
208-
209187
Alert followed by blockquote:
210188

211189
```````````````````````````````` example alert

commonmark-ext-gfm-alerts/src/test/resources/alerts-spec.txt

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -236,28 +236,6 @@ Should be blockquote</p>
236236
</blockquote>
237237
````````````````````````````````
238238

239-
Marker with no content:
240-
241-
```````````````````````````````` example alert
242-
> [!NOTE]
243-
.
244-
<blockquote>
245-
<p>[!NOTE]</p>
246-
</blockquote>
247-
````````````````````````````````
248-
249-
Whitespace-only content after marker:
250-
251-
```````````````````````````````` example alert
252-
> [!TIP]
253-
>
254-
>
255-
.
256-
<blockquote>
257-
<p>[!TIP]</p>
258-
</blockquote>
259-
````````````````````````````````
260-
261239
Extra space inside marker:
262240

263241
```````````````````````````````` example alert
@@ -309,7 +287,7 @@ Regular blockquote is not affected:
309287
Trailing spaces after marker:
310288

311289
```````````````````````````````` example alert
312-
> [!NOTE]
290+
> [!NOTE]
313291
> This is a note
314292
.
315293
<div class="markdown-alert markdown-alert-note" data-alert-type="note">
@@ -342,19 +320,6 @@ Leading spaces before blockquote marker:
342320
</div>
343321
````````````````````````````````
344322

345-
Blank line after marker ends the blockquote (not an alert):
346-
347-
```````````````````````````````` example alert
348-
> [!NOTE]
349-
350-
Some text
351-
.
352-
<blockquote>
353-
<p>[!NOTE]</p>
354-
</blockquote>
355-
<p>Some text</p>
356-
````````````````````````````````
357-
358323
Alert followed by blockquote:
359324

360325
```````````````````````````````` example alert

0 commit comments

Comments
 (0)