Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ HtmlRenderer renderer = HtmlRenderer.builder()

To configure another extension in the above example, just add it to the list.

To enable tables, strikethrough, autolinks, task list items, alerts, and footnotes
together, use the `org.commonmark:commonmark-ext-github` dependency. Pass its preset
to both builders:

```java
import org.commonmark.ext.github.GitHubExtensions;

List<Extension> extensions = GitHubExtensions.create();
Parser parser = Parser.builder().extensions(extensions).build();
HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
```

Other extensions can still be enabled individually.

The following extensions are developed with this library, each in their
own artifact.

Expand Down
4 changes: 4 additions & 0 deletions commonmark-android-test/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ android {
[
'../../commonmark',
'../../commonmark-ext-autolink',
'../../commonmark-ext-footnotes',
'../../commonmark-ext-gfm-alerts',
'../../commonmark-ext-gfm-strikethrough',
'../../commonmark-ext-gfm-tables',
'../../commonmark-ext-github',
'../../commonmark-ext-heading-anchor',
'../../commonmark-ext-ins',
'../../commonmark-ext-task-list-items',
'../../commonmark-ext-yaml-front-matter'
].forEach { d ->
// don't include module-info files, otherwise we get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ public class AlertsExtension
* The standard GitHub Flavored Markdown (GFM) types that the extension enables by default.
* These can be overwritten with {@link Builder#setAllowedTypes(Map)}.
*/
public static final Map<String, String> STANDARD_TYPES =
Map.ofEntries(
Map.entry("NOTE", "Note"),
Map.entry("TIP", "Tip"),
Map.entry("IMPORTANT", "Important"),
Map.entry("WARNING", "Warning"),
Map.entry("CAUTION", "Caution"));
public static final Map<String, String> STANDARD_TYPES;

static {
Map<String, String> types = new HashMap<>();
types.put("NOTE", "Note");
types.put("TIP", "Tip");
types.put("IMPORTANT", "Important");
types.put("WARNING", "Warning");
types.put("CAUTION", "Caution");
STANDARD_TYPES = Collections.unmodifiableMap(types);
}

/** A map of alert marker ({@code [!TYPE]}) to the default title for that marker. */
private final Map<String, String> allowedTypes;
Expand Down
44 changes: 44 additions & 0 deletions commonmark-ext-github/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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.32.1-SNAPSHOT</version>
</parent>

<artifactId>commonmark-ext-github</artifactId>
<name>commonmark-java GitHub extensions</name>
<description>A bundle of extensions for GitHub Markdown features</description>

<dependencies>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-autolink</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-footnotes</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-gfm-alerts</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-gfm-strikethrough</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-gfm-tables</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-task-list-items</artifactId>
</dependency>
</dependencies>
</project>
11 changes: 11 additions & 0 deletions commonmark-ext-github/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module org.commonmark.ext.github {
exports org.commonmark.ext.github;

requires transitive org.commonmark;
requires org.commonmark.ext.autolink;
requires org.commonmark.ext.footnotes;
requires org.commonmark.ext.gfm.alerts;
requires org.commonmark.ext.gfm.strikethrough;
requires org.commonmark.ext.gfm.tables;
requires org.commonmark.ext.task.list.items;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.commonmark.ext.github;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.commonmark.Extension;
import org.commonmark.ext.autolink.AutolinkExtension;
import org.commonmark.ext.footnotes.FootnotesExtension;
import org.commonmark.ext.gfm.alerts.AlertsExtension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.task.list.items.TaskListItemsExtension;

/** A preset for tables, strikethrough, autolinks, task lists, alerts and footnotes. */
public final class GitHubExtensions {
private GitHubExtensions() {}

/**
* Creates the extensions with their default settings. Use the same list when building the
* parser and renderer.
*
* @return an unmodifiable list of extensions
*/
public static List<Extension> create() {
return Collections.unmodifiableList(
Arrays.asList(
AutolinkExtension.create(),
FootnotesExtension.create(),
AlertsExtension.create(),
StrikethroughExtension.create(),
TablesExtension.create(),
TaskListItemsExtension.create()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** A preset for Markdown features supported by GitHub. */
package org.commonmark.ext.github;
4 changes: 4 additions & 0 deletions commonmark-integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-github</artifactId>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-autolink</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.commonmark.integration;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import org.commonmark.Extension;
import org.commonmark.ext.autolink.AutolinkExtension;
import org.commonmark.ext.footnotes.FootnotesExtension;
import org.commonmark.ext.gfm.alerts.AlertsExtension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.github.GitHubExtensions;
import org.commonmark.ext.task.list.items.TaskListItemsExtension;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.junit.jupiter.api.Test;

class GitHubExtensionsTest {
@Test
void rendersTheSixBundledFeaturesTogether() {
String markdown =
"""
| column |
| ------ |
| ~~old~~ |

- [x] See https://example.com

> [!NOTE]
> See the note[^one].

[^one]: A footnote.
""";
List<Extension> manual =
List.of(
AutolinkExtension.create(),
FootnotesExtension.create(),
AlertsExtension.create(),
StrikethroughExtension.create(),
TablesExtension.create(),
TaskListItemsExtension.create());
List<Extension> preset = GitHubExtensions.create();
assertEquals(
manual.stream().map(Object::getClass).toList(),
preset.stream().map(Object::getClass).toList());
String html = render(markdown, preset);
assertEquals(render(markdown, manual), html);
for (String feature :
List.of(
"<th>column</th>",
"<del>old</del>",
"type=\"checkbox\"",
"<a href=\"https://example.com\">",
"markdown-alert-note",
"class=\"footnote-ref\"")) {
assertTrue(html.contains(feature), feature);
}
}

@Test
void preservesCoreRenderingForOtherExtensions() {
String markdown =
"""
---
title: example
---

# Heading

++insert++ ![text](/img.png){width=6}
""";
assertEquals(render(markdown, List.of()), render(markdown, GitHubExtensions.create()));
}

@Test
void callersCannotChangeThePreset() {
assertThrows(UnsupportedOperationException.class, () -> GitHubExtensions.create().clear());
assertEquals(6, GitHubExtensions.create().size());
}

private static String render(String markdown, List<Extension> extensions) {
Parser parser = Parser.builder().extensions(extensions).build();
return HtmlRenderer.builder().extensions(extensions).build().render(parser.parse(markdown));
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>commonmark-ext-gfm-alerts</module>
<module>commonmark-ext-gfm-strikethrough</module>
<module>commonmark-ext-gfm-tables</module>
<module>commonmark-ext-github</module>
<module>commonmark-ext-heading-anchor</module>
<module>commonmark-ext-image-attributes</module>
<module>commonmark-ext-ins</module>
Expand Down Expand Up @@ -183,6 +184,11 @@
<artifactId>commonmark-ext-gfm-tables</artifactId>
<version>0.32.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-github</artifactId>
<version>0.32.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark-ext-heading-anchor</artifactId>
Expand Down