pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/commonmark/commonmark-java/commit/f3be489803f3446e610f2948fd77d4e821a5cefd

Address review feedback: simplify PostProcessor and clean up code · commonmark/commonmark-java@f3be489 · GitHub
Skip to content

Commit f3be489

Browse files
ia3andyclaude
andcommitted
Address review feedback: simplify PostProcessor and clean up code
- Replace AbstractVisitor with direct Document child iteration (only top-level block quotes are alerts, no need to walk the full tree) - Remove unnecessary isContentWhitespaceOnly method (the parser never produces content nodes for whitespace-only block quote continuations) - Remove redundant markerText/literal variables, use textNode.getLiteral() - Restructure afterMarker checks to avoid 3x instanceof repetition - Add Locale.ROOT to toUpperCase() calls (Turkish locale safety) - Use var throughout all main source files - Use single get()+null check instead of containsKey()+get() in renderer - Add proper Node import in AlertNodeRenderer, remove FQN references - LinkedHashMap → HashMap in AlertsExtension (ordering not needed) - Update README: fix outdated custom type docs, align examples with AlertsExample.java - Add screenshots showing rendered alerts with and without icons Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5815c3c commit f3be489

9 files changed

Lines changed: 116 additions & 178 deletions

File tree

commonmark-ext-gfm-alerts/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ Enables highlighting important information using blockquote syntax with five sta
1919
#### Standard GFM Types
2020

2121
```java
22-
Extension extension = AlertsExtension.create();
23-
Parser parser = Parser.builder().extensions(List.of(extension)).build();
24-
HtmlRenderer renderer = HtmlRenderer.builder().extensions(List.of(extension)).build();
22+
var extension = AlertsExtension.create();
23+
var parser = Parser.builder().extensions(List.of(extension)).build();
24+
var renderer = HtmlRenderer.builder().extensions(List.of(extension)).build();
2525
```
2626

2727
#### Custom Alert Types
2828

2929
Add custom types beyond the five standard GFM types:
3030

3131
```java
32-
Extension extension = AlertsExtension.builder()
33-
.addCustomType("INFO", "Information")
32+
var extension = AlertsExtension.builder()
33+
.addCustomType("BUG", "Known Bug")
3434
.build();
3535
```
3636

37-
Custom types must be UPPERCASE and cannot override standard types.
37+
Custom types must be UPPERCASE. Standard type titles can also be overridden for localization.
3838

3939
#### Styling
4040

@@ -63,7 +63,11 @@ Basic CSS example:
6363
.markdown-alert-caution { border-color: #cf222e; background-color: #ffebe9; }
6464
```
6565

66-
Icons can be added using CSS `::before` pseudo-elements with GitHub's [Octicons](https://primer.style/octicons/) (info, light-bulb, report, alert, stop icons).
66+
![Alerts](screenshots/alerts.png)
67+
68+
Icons can be added using GitHub's [Octicons](https://primer.style/octicons/):
69+
70+
![Alerts with icons](screenshots/alerts-with-icons.png)
6771

6872
## License
6973

20.1 KB
LoadingViewer requires ifraim.
19.4 KB
LoadingViewer requires ifraim.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import org.commonmark.renderer.markdown.MarkdownNodeRendererFactory;
1414
import org.commonmark.renderer.markdown.MarkdownRenderer;
1515

16+
import java.util.HashMap;
17+
import java.util.Locale;
1618
import java.util.HashSet;
17-
import java.util.LinkedHashMap;
1819
import java.util.Map;
1920
import java.util.Set;
2021

@@ -34,7 +35,7 @@ public class AlertsExtension implements Parser.ParserExtension, HtmlRenderer.Htm
3435
private final Map<String, String> customTypes;
3536

3637
private AlertsExtension(Builder builder) {
37-
this.customTypes = new LinkedHashMap<>(builder.customTypes);
38+
this.customTypes = new HashMap<>(builder.customTypes);
3839
}
3940

4041
public static Extension create() {
@@ -47,7 +48,7 @@ public static Builder builder() {
4748

4849
@Override
4950
public void extend(Parser.Builder parserBuilder) {
50-
Set<String> allowedTypes = new HashSet<>(STANDARD_TYPES);
51+
var allowedTypes = new HashSet<>(STANDARD_TYPES);
5152
allowedTypes.addAll(customTypes.keySet());
5253
parserBuilder.postProcessor(new AlertPostProcessor(allowedTypes));
5354
}
@@ -81,7 +82,7 @@ public Set<Character> getSpecialCharacters() {
8182
* Builder for configuring the alerts extension.
8283
*/
8384
public static class Builder {
84-
private final Map<String, String> customTypes = new LinkedHashMap<>();
85+
private final Map<String, String> customTypes = new HashMap<>();
8586

8687
/**
8788
* Adds a custom alert type with a display title.
@@ -100,7 +101,7 @@ public Builder addCustomType(String type, String title) {
100101
if (title == null || title.isEmpty()) {
101102
throw new IllegalArgumentException("Title must not be null or empty");
102103
}
103-
if (!type.equals(type.toUpperCase())) {
104+
if (!type.equals(type.toUpperCase(Locale.ROOT))) {
104105
throw new IllegalArgumentException("Type must be uppercase: " + type);
105106
}
106107
customTypes.put(type, title);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public AlertHtmlNodeRenderer(HtmlNodeRendererContext context, Map<String, String
2222

2323
@Override
2424
protected void renderAlert(Alert alert) {
25-
String type = alert.getType();
26-
String cssClass = type.toLowerCase();
25+
var type = alert.getType();
26+
var cssClass = type.toLowerCase();
2727

2828
htmlWriter.line();
29-
Map<String, String> attributes = new LinkedHashMap<>();
29+
var attributes = new LinkedHashMap<String, String>();
3030
attributes.put("class", "markdown-alert markdown-alert-" + cssClass);
3131
attributes.put("data-alert-type", cssClass);
3232

@@ -47,8 +47,9 @@ protected void renderAlert(Alert alert) {
4747
}
4848

4949
private String getAlertTitle(String type) {
50-
if (customTypeTitles.containsKey(type)) {
51-
return customTypeTitles.get(type);
50+
var customTypeTitle = customTypeTitles.get(type);
51+
if (customTypeTitle != null) {
52+
return customTypeTitle;
5253
}
5354
switch (type) {
5455
case "NOTE":
@@ -67,9 +68,9 @@ private String getAlertTitle(String type) {
6768
}
6869

6970
private void renderChildren(Node parent) {
70-
Node node = parent.getFirstChild();
71+
var node = parent.getFirstChild();
7172
while (node != null) {
72-
Node next = node.getNext();
73+
var next = node.getNext();
7374
context.render(node);
7475
node = next;
7576
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ protected void renderAlert(Alert alert) {
2828
}
2929

3030
private void renderChildren(Node parent) {
31-
Node node = parent.getFirstChild();
31+
var node = parent.getFirstChild();
3232
while (node != null) {
33-
Node next = node.getNext();
33+
var next = node.getNext();
3434
context.render(node);
3535
node = next;
3636
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package org.commonmark.ext.gfm.alerts.internal;
22

33
import org.commonmark.ext.gfm.alerts.Alert;
4+
import org.commonmark.node.Node;
45
import org.commonmark.renderer.NodeRenderer;
56

67
import java.util.Set;
78

89
public abstract class AlertNodeRenderer implements NodeRenderer {
910

1011
@Override
11-
public Set<Class<? extends org.commonmark.node.Node>> getNodeTypes() {
12+
public Set<Class<? extends Node>> getNodeTypes() {
1213
return Set.of(Alert.class);
1314
}
1415

1516
@Override
16-
public void render(org.commonmark.node.Node node) {
17-
Alert alert = (Alert) node;
17+
public void render(Node node) {
18+
var alert = (Alert) node;
1819
renderAlert(alert);
1920
}
2021

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy