Skip to content

Commit 74f4a8a

Browse files
committed
Address Styling Comments
1 parent 8968d7b commit 74f4a8a

6 files changed

Lines changed: 26 additions & 45 deletions

File tree

commonmark-ext-gfm-alerts/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Allow authors to provide custom titles per alert by adding text after the alert
4242
marker on the same line:
4343

4444
```java
45-
var extension = AlertsExtension.builder().allowCustomTitles().build();
45+
var extension = AlertsExtension.builder().allowCustomTitles(true).build();
4646
```
4747

4848
```markdown
@@ -71,7 +71,7 @@ blocks are parsed as regular block quotes.
7171
This behavior can be changed to allow nested alerts:
7272

7373
```java
74-
var extension = AlertsExtension.builder().allowNestedAlerts().build();
74+
var extension = AlertsExtension.builder().allowNestedAlerts(true).build();
7575
```
7676

7777
### Styling

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

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,43 +120,28 @@ public Builder addCustomType(String type, String title) {
120120
}
121121

122122
/**
123-
* Allows custom titles on alerts. See {@link AlertTitle} for more information.
123+
* Allows or disallows custom titles on alerts. See {@link AlertTitle} for more information.
124+
* @param allow Whether to allow or disallow custom titles on alerts.
124125
* @return {@code this}
125126
*/
126-
public Builder allowCustomTitles() {
127-
customTitlesAllowed = true;
127+
public Builder allowCustomTitles(boolean allow) {
128+
customTitlesAllowed = allow;
128129
return this;
129130
}
130131

131132
/**
132-
* Disallows custom titles on alerts. See {@link AlertTitle} for more information.
133-
* @return {@code this}
134-
*/
135-
public Builder disallowCustomTitles() {
136-
customTitlesAllowed = false;
137-
return this;
138-
}
139-
140-
/**
141-
* Allows alerts to be parsed within blocks other than {@code Document} (the root).
133+
* Allows or disallows parsing alerts within non-root blocks ({@code Document}).
142134
* <p>
143-
* Note that even with this enabled, {@link Parser.Builder#maxOpenBlockParsers(int)}
144-
* will be respected.
145-
* @return {@code this}
146-
*/
147-
public Builder allowNestedAlerts() {
148-
nestedAlertsAllowed = true;
149-
return this;
150-
}
151-
152-
/**
153-
* Prevents alerts from being parsed within blocks other than {@code Document}
154-
* (the root). If an alert appears within another block, it will be parsed as
135+
* When disallowed, if an alert appears within another block, it will be parsed as
155136
* a regular {@code BlockQuote}.
137+
* <p>
138+
* Note that even when this is allowed, {@link Parser.Builder#maxOpenBlockParsers(int)}
139+
* will be respected.
140+
* @param allow Whether to allow or disallow parsing alerts within non-root blocks.
156141
* @return {@code this}
157142
*/
158-
public Builder disallowNestedAlerts() {
159-
nestedAlertsAllowed = false;
143+
public Builder allowNestedAlerts(boolean allow) {
144+
nestedAlertsAllowed = allow;
160145
return this;
161146
}
162147

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public BlockContinue tryContinue(ParserState state) {
5959
* (with up to 3 leading spaces, optional space after '>')
6060
*/
6161
var line = state.getLine().getContent();
62-
int nextNonSpace = state.getNextNonSpaceIndex();
62+
var nextNonSpace = state.getNextNonSpaceIndex();
6363
if (state.getIndent() >= 4 // Parsing.CODE_BLOCK_INDENT
6464
|| nextNonSpace >= line.length()
6565
|| line.charAt(nextNonSpace) != '>') {
6666
return BlockContinue.none();
6767
}
6868

69-
int newColumn = state.getColumn() + state.getIndent() + 1;
69+
var newColumn = state.getColumn() + state.getIndent() + 1;
7070
if (Characters.isSpaceOrTab(line, nextNonSpace + 1)) {
7171
newColumn++;
7272
}
@@ -150,7 +150,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
150150
}
151151

152152
var line = state.getLine().getContent();
153-
int nextNonSpace = state.getNextNonSpaceIndex();
153+
var nextNonSpace = state.getNextNonSpaceIndex();
154154

155155
// Case A: Fresh start. Line begins with '>'.
156156
if (nextNonSpace < line.length() && line.charAt(nextNonSpace) == '>') {
@@ -205,24 +205,20 @@ private BlockStart tryStartFresh(CharSequence line, int nextNonSpace, ParserStat
205205
afterGt = state.getIndex();
206206
}
207207

208-
Matcher matcher;
209-
if (customTitlesAllowed) {
210-
matcher = ALERT_PATTERN_CUSTOM_TITLE.matcher(line.subSequence(afterGt, line.length()));
211-
} else {
212-
matcher = ALERT_PATTERN_NO_CUSTOM_TITLE.matcher(line.subSequence(afterGt, line.length()));
213-
}
208+
var pattern = customTitlesAllowed ? ALERT_PATTERN_CUSTOM_TITLE : ALERT_PATTERN_NO_CUSTOM_TITLE;
209+
var matcher = pattern.matcher(line.subSequence(afterGt, line.length()));
214210

215211
if (!matcher.matches()) {
216212
return BlockStart.none();
217213
}
218214

219-
String typeOriginalCase = matcher.group(1);
220-
String type = typeOriginalCase.toUpperCase(Locale.ROOT);
215+
var typeOriginalCase = matcher.group(1);
216+
var type = typeOriginalCase.toUpperCase(Locale.ROOT);
221217
if (!allowedTypes.contains(type)) {
222218
return BlockStart.none();
223219
}
224220

225-
String titleContent = "";
221+
var titleContent = "";
226222
if (customTitlesAllowed) {
227223
titleContent = matcher.group(2).replaceFirst("^[ \\t]+", "").stripTrailing();
228224
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private boolean isCommentOnlyTitle(Node title) {
113113
}
114114

115115
private boolean isHtmlComment(HtmlInline htmlInline) {
116-
String literal = htmlInline.getLiteral();
116+
var literal = htmlInline.getLiteral();
117117
if (literal == null || !literal.startsWith("<!--")) {
118118
return false;
119119
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class AlertsMarkdownRendererTest {
1515
private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build();
1616
private static final MarkdownRenderer RENDERER = MarkdownRenderer.builder().extensions(EXTENSIONS).build();
1717

18-
private static final Set<Extension> EXTENSIONS_CUSTOM_TITLES = Set.of(AlertsExtension.builder().allowCustomTitles().build());
18+
private static final Set<Extension> EXTENSIONS_CUSTOM_TITLES = Set.of(AlertsExtension.builder().allowCustomTitles(true).build());
1919
private static final Parser PARSER_CUSTOM_TITLES = Parser.builder()
2020
.extensions(EXTENSIONS_CUSTOM_TITLES)
2121
.build();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class AlertsTest extends RenderingTestCase {
1818
private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build();
1919
private static final HtmlRenderer HTML_RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS).build();
2020

21-
private static final Set<Extension> EXTENSIONS_CUSTOM_TITLES = Set.of(AlertsExtension.builder().allowCustomTitles().build());
21+
private static final Set<Extension> EXTENSIONS_CUSTOM_TITLES = Set.of(AlertsExtension.builder().allowCustomTitles(true).build());
2222
private static final Parser PARSER_CUSTOM_TITLES = Parser.builder()
2323
.extensions(EXTENSIONS_CUSTOM_TITLES)
2424
.build();
@@ -335,7 +335,7 @@ public void noNestedAlertsByDefaultLeadingEmptyLines() {
335335

336336
@Test
337337
public void nestedAlerts() {
338-
Extension extension = AlertsExtension.builder().allowNestedAlerts().build();
338+
Extension extension = AlertsExtension.builder().allowNestedAlerts(true).build();
339339
Parser parser = Parser.builder().extensions(Set.of(extension)).build();
340340
HtmlRenderer renderer = HtmlRenderer.builder().extensions(Set.of(extension)).build();
341341

0 commit comments

Comments
 (0)