-
Notifications
You must be signed in to change notification settings - Fork 322
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| 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> |
| 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; | ||
| } |
| 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; | ||
| } | ||
| } |
| 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); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT, no need for a |
||||||
| } | ||||||
|
|
||||||
| 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<>(); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, no need for a |
||||||
|
|
||||||
| /** | ||||||
| * 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())) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(Otherwise, e.g. in a Turkish locale |
||||||
| 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); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| 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); | ||||||||||||||
|
Comment on lines
+50
to
+51
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| 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; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| 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; | ||
| } | ||
| } | ||
| } |
| 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() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an import please. |
||
| 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); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outdated now.