A Swing EditorKit for rendering Markdown inside JEditorPane. Uses commonmark-java to parse Markdown into HTML, which is then displayed through Swing's built-in HTML rendering engine.
- Drop-in
EditorKitfor anyJEditorPanewith content typetext/markdown - GFM extensions: tables, strikethrough (
~~text~~), and autolinks - GitHub-inspired default stylesheet (headings, code blocks, blockquotes, tables, etc.)
- Round-trip support: original Markdown source is preserved and written back via
write() - Java 11+
Maven:
<dependency>
<groupId>ca.weblite</groupId>
<artifactId>swing-markdown-renderer</artifactId>
<version>0.1.5</version>
</dependency>Gradle:
implementation 'ca.weblite:swing-markdown-renderer:0.1.5'JEditorPane editor = new JEditorPane();
editor.setEditorKit(new MarkdownEditorKit());
editor.setText("# Hello World\nThis is **Markdown**.");MarkdownEditorKit kit = new MarkdownEditorKit();
editor.setEditorKit(kit);
kit.setMarkdownText(editor, "# Title\nSome *content*.");String markdown = MarkdownEditorKit.getMarkdownText(editor);MarkdownEditorKit kit = new MarkdownEditorKit();
String html = kit.renderToHtml("**bold** and *italic*");The project includes a live editor demo (MarkdownEditorDemo) with a split pane: type Markdown on the left, see rendered output on the right in real time.
mvn compile exec:java -Dexec.mainClass=ca.weblite.markdown.MarkdownEditorDemoOr with javac directly:
# Compile
javac -cp "lib/*" -d target/classes src/main/java/ca/weblite/markdown/*.java
# Run
java -cp "lib/*:target/classes" ca.weblite.markdown.MarkdownEditorDemo- Java 11 or later
- Maven 3.6+
mvn clean testmvn clean packageThe JAR will be in target/swing-markdown-renderer-1.0.0-SNAPSHOT.jar.
src/
main/java/ca/weblite/markdown/
MarkdownEditorKit.java # The EditorKit implementation
MarkdownEditorDemo.java # Live preview demo application
test/java/ca/weblite/markdown/
MarkdownEditorKitTest.java # Unit tests (20 tests)
| Dependency | Version | Purpose |
|---|---|---|
| commonmark | 0.21.0 | Markdown parser and HTML renderer |
| commonmark-ext-gfm-tables | 0.21.0 | GFM table support |
| commonmark-ext-gfm-strikethrough | 0.21.0 | GFM strikethrough support |
| commonmark-ext-autolink | 0.21.0 | Automatic URL linking |
| junit | 4.13.2 | Unit testing (test scope) |
See LICENSE for details.