Skip to content

Commit fcecf94

Browse files
committed
Add a GitHub extension preset
1 parent 3e1f83b commit fcecf94

10 files changed

Lines changed: 215 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,20 @@ HtmlRenderer renderer = HtmlRenderer.builder()
313313

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

316+
To enable tables, strikethrough, autolinks, task list items, alerts, and footnotes
317+
together, use the `org.commonmark:commonmark-ext-github` dependency. Pass its preset
318+
to both builders:
319+
320+
```java
321+
import org.commonmark.ext.github.GitHubExtensions;
322+
323+
List<Extension> extensions = GitHubExtensions.create();
324+
Parser parser = Parser.builder().extensions(extensions).build();
325+
HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
326+
```
327+
328+
Other extensions can still be enabled individually.
329+
316330
The following extensions are developed with this library, each in their
317331
own artifact.
318332

‎commonmark-android-test/app/build.gradle‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ android {
3636
[
3737
'../../commonmark',
3838
'../../commonmark-ext-autolink',
39+
'../../commonmark-ext-footnotes',
40+
'../../commonmark-ext-gfm-alerts',
3941
'../../commonmark-ext-gfm-strikethrough',
4042
'../../commonmark-ext-gfm-tables',
43+
'../../commonmark-ext-github',
4144
'../../commonmark-ext-heading-anchor',
4245
'../../commonmark-ext-ins',
46+
'../../commonmark-ext-task-list-items',
4347
'../../commonmark-ext-yaml-front-matter'
4448
].forEach { d ->
4549
// don't include module-info files, otherwise we get

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ public class AlertsExtension
6363
* The standard GitHub Flavored Markdown (GFM) types that the extension enables by default.
6464
* These can be overwritten with {@link Builder#setAllowedTypes(Map)}.
6565
*/
66-
public static final Map<String, String> STANDARD_TYPES =
67-
Map.ofEntries(
68-
Map.entry("NOTE", "Note"),
69-
Map.entry("TIP", "Tip"),
70-
Map.entry("IMPORTANT", "Important"),
71-
Map.entry("WARNING", "Warning"),
72-
Map.entry("CAUTION", "Caution"));
66+
public static final Map<String, String> STANDARD_TYPES;
67+
68+
static {
69+
Map<String, String> types = new HashMap<>();
70+
types.put("NOTE", "Note");
71+
types.put("TIP", "Tip");
72+
types.put("IMPORTANT", "Important");
73+
types.put("WARNING", "Warning");
74+
types.put("CAUTION", "Caution");
75+
STANDARD_TYPES = Collections.unmodifiableMap(types);
76+
}
7377

7478
/** A map of alert marker ({@code [!TYPE]}) to the default title for that marker. */
7579
private final Map<String, String> allowedTypes;

‎commonmark-ext-github/pom.xml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.commonmark</groupId>
6+
<artifactId>commonmark-parent</artifactId>
7+
<version>0.32.1-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>commonmark-ext-github</artifactId>
11+
<name>commonmark-java GitHub extensions</name>
12+
<description>A bundle of extensions for GitHub Markdown features</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.commonmark</groupId>
17+
<artifactId>commonmark</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.commonmark</groupId>
21+
<artifactId>commonmark-ext-autolink</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.commonmark</groupId>
25+
<artifactId>commonmark-ext-footnotes</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.commonmark</groupId>
29+
<artifactId>commonmark-ext-gfm-alerts</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.commonmark</groupId>
33+
<artifactId>commonmark-ext-gfm-strikethrough</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.commonmark</groupId>
37+
<artifactId>commonmark-ext-gfm-tables</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.commonmark</groupId>
41+
<artifactId>commonmark-ext-task-list-items</artifactId>
42+
</dependency>
43+
</dependencies>
44+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module org.commonmark.ext.github {
2+
exports org.commonmark.ext.github;
3+
4+
requires transitive org.commonmark;
5+
requires org.commonmark.ext.autolink;
6+
requires org.commonmark.ext.footnotes;
7+
requires org.commonmark.ext.gfm.alerts;
8+
requires org.commonmark.ext.gfm.strikethrough;
9+
requires org.commonmark.ext.gfm.tables;
10+
requires org.commonmark.ext.task.list.items;
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.commonmark.ext.github;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import org.commonmark.Extension;
7+
import org.commonmark.ext.autolink.AutolinkExtension;
8+
import org.commonmark.ext.footnotes.FootnotesExtension;
9+
import org.commonmark.ext.gfm.alerts.AlertsExtension;
10+
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
11+
import org.commonmark.ext.gfm.tables.TablesExtension;
12+
import org.commonmark.ext.task.list.items.TaskListItemsExtension;
13+
14+
/** A preset for tables, strikethrough, autolinks, task lists, alerts and footnotes. */
15+
public final class GitHubExtensions {
16+
private GitHubExtensions() {}
17+
18+
/**
19+
* Creates the extensions with their default settings. Use the same list when building the
20+
* parser and renderer.
21+
*
22+
* @return an unmodifiable list of extensions
23+
*/
24+
public static List<Extension> create() {
25+
return Collections.unmodifiableList(
26+
Arrays.asList(
27+
AutolinkExtension.create(),
28+
FootnotesExtension.create(),
29+
AlertsExtension.create(),
30+
StrikethroughExtension.create(),
31+
TablesExtension.create(),
32+
TaskListItemsExtension.create()));
33+
}
34+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** A preset for Markdown features supported by GitHub. */
2+
package org.commonmark.ext.github;

‎commonmark-integration-test/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<groupId>org.commonmark</groupId>
1717
<artifactId>commonmark</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>org.commonmark</groupId>
21+
<artifactId>commonmark-ext-github</artifactId>
22+
</dependency>
1923
<dependency>
2024
<groupId>org.commonmark</groupId>
2125
<artifactId>commonmark-ext-autolink</artifactId>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.commonmark.integration;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.List;
6+
import org.commonmark.Extension;
7+
import org.commonmark.ext.autolink.AutolinkExtension;
8+
import org.commonmark.ext.footnotes.FootnotesExtension;
9+
import org.commonmark.ext.gfm.alerts.AlertsExtension;
10+
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
11+
import org.commonmark.ext.gfm.tables.TablesExtension;
12+
import org.commonmark.ext.github.GitHubExtensions;
13+
import org.commonmark.ext.task.list.items.TaskListItemsExtension;
14+
import org.commonmark.parser.Parser;
15+
import org.commonmark.renderer.html.HtmlRenderer;
16+
import org.junit.jupiter.api.Test;
17+
18+
class GitHubExtensionsTest {
19+
@Test
20+
void rendersTheSixBundledFeaturesTogether() {
21+
String markdown =
22+
"""
23+
| column |
24+
| ------ |
25+
| ~~old~~ |
26+
27+
- [x] See https://example.com
28+
29+
> [!NOTE]
30+
> See the note[^one].
31+
32+
[^one]: A footnote.
33+
""";
34+
List<Extension> manual =
35+
List.of(
36+
AutolinkExtension.create(),
37+
FootnotesExtension.create(),
38+
AlertsExtension.create(),
39+
StrikethroughExtension.create(),
40+
TablesExtension.create(),
41+
TaskListItemsExtension.create());
42+
List<Extension> preset = GitHubExtensions.create();
43+
assertEquals(
44+
manual.stream().map(Object::getClass).toList(),
45+
preset.stream().map(Object::getClass).toList());
46+
String html = render(markdown, preset);
47+
assertEquals(render(markdown, manual), html);
48+
for (String feature :
49+
List.of(
50+
"<th>column</th>",
51+
"<del>old</del>",
52+
"type=\"checkbox\"",
53+
"<a href=\"https://example.com\">",
54+
"markdown-alert-note",
55+
"class=\"footnote-ref\"")) {
56+
assertTrue(html.contains(feature), feature);
57+
}
58+
}
59+
60+
@Test
61+
void preservesCoreRenderingForOtherExtensions() {
62+
String markdown =
63+
"""
64+
---
65+
title: example
66+
---
67+
68+
# Heading
69+
70+
++insert++ ![text](/img.png){width=6}
71+
""";
72+
assertEquals(render(markdown, List.of()), render(markdown, GitHubExtensions.create()));
73+
}
74+
75+
@Test
76+
void callersCannotChangeThePreset() {
77+
assertThrows(UnsupportedOperationException.class, () -> GitHubExtensions.create().clear());
78+
assertEquals(6, GitHubExtensions.create().size());
79+
}
80+
81+
private static String render(String markdown, List<Extension> extensions) {
82+
Parser parser = Parser.builder().extensions(extensions).build();
83+
return HtmlRenderer.builder().extensions(extensions).build().render(parser.parse(markdown));
84+
}
85+
}

‎pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<module>commonmark-ext-gfm-alerts</module>
2121
<module>commonmark-ext-gfm-strikethrough</module>
2222
<module>commonmark-ext-gfm-tables</module>
23+
<module>commonmark-ext-github</module>
2324
<module>commonmark-ext-heading-anchor</module>
2425
<module>commonmark-ext-image-attributes</module>
2526
<module>commonmark-ext-ins</module>
@@ -183,6 +184,11 @@
183184
<artifactId>commonmark-ext-gfm-tables</artifactId>
184185
<version>0.32.1-SNAPSHOT</version>
185186
</dependency>
187+
<dependency>
188+
<groupId>org.commonmark</groupId>
189+
<artifactId>commonmark-ext-github</artifactId>
190+
<version>0.32.1-SNAPSHOT</version>
191+
</dependency>
186192
<dependency>
187193
<groupId>org.commonmark</groupId>
188194
<artifactId>commonmark-ext-heading-anchor</artifactId>

0 commit comments

Comments
 (0)