-
Notifications
You must be signed in to change notification settings - Fork 329
Add GFM alerts extension #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
057f13d
Add GFM alerts extension
ia3andy 5815c3c
Add spec generator script and match GitHub softbreak rendering
ia3andy f3be489
Address review feedback: simplify PostProcessor and clean up code
ia3andy 61200f9
Update commonmark-ext-gfm-alerts/src/main/java/org/commonmark/ext/gfm…
robinst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # commonmark-ext-gfm-alerts | ||
|
|
||
| Extension for [commonmark-java](https://github.com/commonmark/commonmark-java) that adds support for [GitHub Flavored Markdown alerts](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts). | ||
|
|
||
| Enables highlighting important information using blockquote syntax with five standard alert types: NOTE, TIP, IMPORTANT, WARNING, and CAUTION. | ||
|
|
||
| ## Usage | ||
|
|
||
| #### Markdown Syntax | ||
|
|
||
| ```markdown | ||
| > [!NOTE] | ||
| > Useful information | ||
|
|
||
| > [!WARNING] | ||
| > Critical information | ||
| ``` | ||
|
|
||
| #### Standard GFM Types | ||
|
|
||
| ```java | ||
| Extension extension = AlertsExtension.create(); | ||
| Parser parser = Parser.builder().extensions(List.of(extension)).build(); | ||
| HtmlRenderer renderer = HtmlRenderer.builder().extensions(List.of(extension)).build(); | ||
| ``` | ||
|
|
||
| #### Custom Alert Types | ||
|
|
||
| Add custom types beyond the five standard GFM types: | ||
|
|
||
| ```java | ||
| Extension extension = AlertsExtension.builder() | ||
| .addCustomType("INFO", "Information") | ||
| .build(); | ||
| ``` | ||
|
|
||
| Custom types must be UPPERCASE and cannot override standard types. | ||
|
|
||
| #### Styling | ||
|
|
||
| Alerts render as `<div>` elements with CSS classes: | ||
|
|
||
| ```html | ||
| <div class="markdown-alert markdown-alert-note" data-alert-type="note"> | ||
| <p class="markdown-alert-title">Note</p> | ||
| <p>Content</p> | ||
| </div> | ||
| ``` | ||
|
|
||
| Basic CSS example: | ||
|
|
||
| ```css | ||
| .markdown-alert { | ||
| padding: 0.5rem 1rem; | ||
| margin-bottom: 1rem; | ||
| border-left: 4px solid; | ||
| } | ||
|
|
||
| .markdown-alert-note { border-color: #0969da; background-color: #ddf4ff; } | ||
| .markdown-alert-tip { border-color: #1a7f37; background-color: #dcffe4; } | ||
| .markdown-alert-important { border-color: #8250df; background-color: #f6f0ff; } | ||
| .markdown-alert-warning { border-color: #9a6700; background-color: #fff8c5; } | ||
| .markdown-alert-caution { border-color: #cf222e; background-color: #ffebe9; } | ||
| ``` | ||
|
|
||
| Icons can be added using CSS `::before` pseudo-elements with GitHub's [Octicons](https://primer.style/octicons/) (info, light-bulb, report, alert, stop icons). | ||
|
|
||
| ## License | ||
|
|
||
| See the main commonmark-java project for license information. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.commonmark</groupId> | ||
| <artifactId>commonmark-parent</artifactId> | ||
| <version>0.27.2-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>commonmark-ext-gfm-alerts</artifactId> | ||
| <name>commonmark-java extension for alerts</name> | ||
| <description>commonmark-java extension for GFM alerts (admonition blocks) using [!TYPE] syntax (GitHub Flavored Markdown)</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.commonmark</groupId> | ||
| <artifactId>commonmark</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.commonmark</groupId> | ||
| <artifactId>commonmark-test-util</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module org.commonmark.ext.gfm.alerts { | ||
| exports org.commonmark.ext.gfm.alerts; | ||
|
|
||
| requires transitive org.commonmark; | ||
| } |
19 changes: 19 additions & 0 deletions
19
commonmark-ext-gfm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/Alert.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package org.commonmark.ext.gfm.alerts; | ||
|
|
||
| import org.commonmark.node.CustomBlock; | ||
|
|
||
| /** | ||
| * Alert block for highlighting important information using {@code [!TYPE]} syntax. | ||
| */ | ||
| public class Alert extends CustomBlock { | ||
|
|
||
| private final String type; | ||
|
|
||
| public Alert(String type) { | ||
| this.type = type; | ||
| } | ||
|
|
||
| public String getType() { | ||
| return type; | ||
| } | ||
| } |
117 changes: 117 additions & 0 deletions
117
commonmark-ext-gfm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/AlertsExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package org.commonmark.ext.gfm.alerts; | ||
|
|
||
| import org.commonmark.Extension; | ||
| import org.commonmark.ext.gfm.alerts.internal.AlertPostProcessor; | ||
| import org.commonmark.ext.gfm.alerts.internal.AlertHtmlNodeRenderer; | ||
| import org.commonmark.ext.gfm.alerts.internal.AlertMarkdownNodeRenderer; | ||
| import org.commonmark.parser.Parser; | ||
| import org.commonmark.renderer.NodeRenderer; | ||
| import org.commonmark.renderer.html.HtmlNodeRendererContext; | ||
| import org.commonmark.renderer.html.HtmlNodeRendererFactory; | ||
| import org.commonmark.renderer.html.HtmlRenderer; | ||
| import org.commonmark.renderer.markdown.MarkdownNodeRendererContext; | ||
| import org.commonmark.renderer.markdown.MarkdownNodeRendererFactory; | ||
| import org.commonmark.renderer.markdown.MarkdownRenderer; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Extension for GFM alerts using {@code [!TYPE]} syntax (GitHub Flavored Markdown). | ||
| * <p> | ||
| * Create with {@link #create()} or {@link #builder()} and configure on builders | ||
| * ({@link org.commonmark.parser.Parser.Builder#extensions(Iterable)}, | ||
| * {@link HtmlRenderer.Builder#extensions(Iterable)}). | ||
| * Parsed alerts become {@link Alert} blocks. | ||
| */ | ||
| public class AlertsExtension implements Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension, | ||
| MarkdownRenderer.MarkdownRendererExtension { | ||
|
|
||
| static final Set<String> STANDARD_TYPES = Set.of("NOTE", "TIP", "IMPORTANT", "WARNING", "CAUTION"); | ||
|
|
||
| private final Map<String, String> customTypes; | ||
|
|
||
| private AlertsExtension(Builder builder) { | ||
| this.customTypes = new LinkedHashMap<>(builder.customTypes); | ||
|
ia3andy marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public static Extension create() { | ||
| return builder().build(); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| @Override | ||
| public void extend(Parser.Builder parserBuilder) { | ||
| Set<String> allowedTypes = new HashSet<>(STANDARD_TYPES); | ||
| allowedTypes.addAll(customTypes.keySet()); | ||
| parserBuilder.postProcessor(new AlertPostProcessor(allowedTypes)); | ||
| } | ||
|
|
||
| @Override | ||
| public void extend(HtmlRenderer.Builder rendererBuilder) { | ||
| rendererBuilder.nodeRendererFactory(new HtmlNodeRendererFactory() { | ||
| @Override | ||
| public NodeRenderer create(HtmlNodeRendererContext context) { | ||
| return new AlertHtmlNodeRenderer(context, customTypes); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void extend(MarkdownRenderer.Builder rendererBuilder) { | ||
| rendererBuilder.nodeRendererFactory(new MarkdownNodeRendererFactory() { | ||
| @Override | ||
| public NodeRenderer create(MarkdownNodeRendererContext context) { | ||
| return new AlertMarkdownNodeRenderer(context); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Character> getSpecialCharacters() { | ||
| return Set.of(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Builder for configuring the alerts extension. | ||
| */ | ||
| public static class Builder { | ||
| private final Map<String, String> customTypes = new LinkedHashMap<>(); | ||
|
ia3andy marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Adds a custom alert type with a display title. | ||
| * <p> | ||
| * This can also be used to override the display title of standard GFM types | ||
| * (e.g., for localization). | ||
| * | ||
| * @param type the alert type (must be uppercase) | ||
| * @param title the display title for this alert type | ||
| * @return {@code this} | ||
| */ | ||
| public Builder addCustomType(String type, String title) { | ||
| if (type == null || type.isEmpty()) { | ||
| throw new IllegalArgumentException("Type must not be null or empty"); | ||
| } | ||
| if (title == null || title.isEmpty()) { | ||
| throw new IllegalArgumentException("Title must not be null or empty"); | ||
| } | ||
| if (!type.equals(type.toUpperCase())) { | ||
|
ia3andy marked this conversation as resolved.
Outdated
|
||
| throw new IllegalArgumentException("Type must be uppercase: " + type); | ||
| } | ||
| customTypes.put(type, title); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @return a configured {@link Extension} | ||
| */ | ||
| public Extension build() { | ||
| return new AlertsExtension(this); | ||
| } | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
...fm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/internal/AlertHtmlNodeRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package org.commonmark.ext.gfm.alerts.internal; | ||
|
|
||
| import org.commonmark.ext.gfm.alerts.Alert; | ||
| import org.commonmark.node.Node; | ||
| import org.commonmark.renderer.html.HtmlNodeRendererContext; | ||
| import org.commonmark.renderer.html.HtmlWriter; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class AlertHtmlNodeRenderer extends AlertNodeRenderer { | ||
|
|
||
| private final HtmlWriter htmlWriter; | ||
| private final HtmlNodeRendererContext context; | ||
| private final Map<String, String> customTypeTitles; | ||
|
|
||
| public AlertHtmlNodeRenderer(HtmlNodeRendererContext context, Map<String, String> customTypeTitles) { | ||
| this.htmlWriter = context.getWriter(); | ||
| this.context = context; | ||
| this.customTypeTitles = customTypeTitles; | ||
| } | ||
|
|
||
| @Override | ||
| protected void renderAlert(Alert alert) { | ||
| String type = alert.getType(); | ||
| String cssClass = type.toLowerCase(); | ||
|
|
||
| htmlWriter.line(); | ||
| Map<String, String> attributes = new LinkedHashMap<>(); | ||
| attributes.put("class", "markdown-alert markdown-alert-" + cssClass); | ||
| attributes.put("data-alert-type", cssClass); | ||
|
|
||
| htmlWriter.tag("div", context.extendAttributes(alert, "div", attributes)); | ||
| htmlWriter.line(); | ||
|
|
||
| // Render alert title | ||
| htmlWriter.tag("p", Map.of("class", "markdown-alert-title")); | ||
| htmlWriter.text(getAlertTitle(type)); | ||
| htmlWriter.tag("/p"); | ||
| htmlWriter.line(); | ||
|
|
||
| // Render children (the alert content) | ||
| renderChildren(alert); | ||
|
|
||
| htmlWriter.tag("/div"); | ||
| htmlWriter.line(); | ||
| } | ||
|
|
||
| private String getAlertTitle(String type) { | ||
| if (customTypeTitles.containsKey(type)) { | ||
| return customTypeTitles.get(type); | ||
|
ia3andy marked this conversation as resolved.
Outdated
|
||
| } | ||
| switch (type) { | ||
| case "NOTE": | ||
| return "Note"; | ||
| case "TIP": | ||
| return "Tip"; | ||
| case "IMPORTANT": | ||
| return "Important"; | ||
| case "WARNING": | ||
| return "Warning"; | ||
| case "CAUTION": | ||
| return "Caution"; | ||
| default: | ||
| throw new IllegalStateException("Unknown alert type: " + type); | ||
| } | ||
| } | ||
|
|
||
| private void renderChildren(Node parent) { | ||
| Node node = parent.getFirstChild(); | ||
| while (node != null) { | ||
| Node next = node.getNext(); | ||
| context.render(node); | ||
| node = next; | ||
| } | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
...lerts/src/main/java/org/commonmark/ext/gfm/alerts/internal/AlertMarkdownNodeRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.commonmark.ext.gfm.alerts.internal; | ||
|
|
||
| import org.commonmark.ext.gfm.alerts.Alert; | ||
| import org.commonmark.node.Node; | ||
| import org.commonmark.renderer.markdown.MarkdownNodeRendererContext; | ||
| import org.commonmark.renderer.markdown.MarkdownWriter; | ||
|
|
||
| public class AlertMarkdownNodeRenderer extends AlertNodeRenderer { | ||
|
|
||
| private final MarkdownWriter writer; | ||
| private final MarkdownNodeRendererContext context; | ||
|
|
||
| public AlertMarkdownNodeRenderer(MarkdownNodeRendererContext context) { | ||
| this.writer = context.getWriter(); | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| protected void renderAlert(Alert alert) { | ||
| // First line: > [!TYPE] | ||
| writer.writePrefix("> "); | ||
| writer.pushPrefix("> "); | ||
| writer.raw("[!" + alert.getType() + "]"); | ||
| writer.line(); | ||
| renderChildren(alert); | ||
| writer.popPrefix(); | ||
| writer.block(); | ||
| } | ||
|
|
||
| private void renderChildren(Node parent) { | ||
| Node node = parent.getFirstChild(); | ||
| while (node != null) { | ||
| Node next = node.getNext(); | ||
| context.render(node); | ||
| node = next; | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...xt-gfm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/internal/AlertNodeRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.commonmark.ext.gfm.alerts.internal; | ||
|
|
||
| import org.commonmark.ext.gfm.alerts.Alert; | ||
| import org.commonmark.renderer.NodeRenderer; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| public abstract class AlertNodeRenderer implements NodeRenderer { | ||
|
|
||
| @Override | ||
| public Set<Class<? extends org.commonmark.node.Node>> getNodeTypes() { | ||
|
ia3andy marked this conversation as resolved.
Outdated
|
||
| return Set.of(Alert.class); | ||
| } | ||
|
|
||
| @Override | ||
| public void render(org.commonmark.node.Node node) { | ||
| Alert alert = (Alert) node; | ||
| renderAlert(alert); | ||
| } | ||
|
|
||
| protected abstract void renderAlert(Alert alert); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.