Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 5bf252e

Browse files
committed
Added markdown support
Fixes issue hub4j#165
1 parent 75512ff commit 5bf252e

5 files changed

Lines changed: 87 additions & 3 deletions

File tree

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import javax.xml.bind.DatatypeConverter;
3131
import java.io.FileNotFoundException;
3232
import java.io.IOException;
33+
import java.io.InputStreamReader;
3334
import java.io.InterruptedIOException;
35+
import java.io.Reader;
3436
import java.net.URL;
3537
import java.util.*;
3638

@@ -1149,7 +1151,25 @@ public int getContributions() {
11491151
return contributions;
11501152
}
11511153
}
1152-
1154+
1155+
/**
1156+
* Render a Markdown document.
1157+
*
1158+
* In {@linkplain MarkdownMode#GFM GFM mode}, issue numbers and user mentions
1159+
* are linked accordingly.
1160+
*
1161+
* @see GitHub#renderMarkdown(String)
1162+
*/
1163+
public Reader renderMarkdown(String text, MarkdownMode mode) throws IOException {
1164+
return new InputStreamReader(
1165+
new Requester(root)
1166+
.with("text", text)
1167+
.with("mode",mode==null?null:mode.toString())
1168+
.with("context", getFullName())
1169+
.read("/markdown"),
1170+
"UTF-8");
1171+
}
1172+
11531173

11541174

11551175
@Override

src/main/java/org/kohsuke/github/GitHub.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
2727
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
2828

29+
import java.io.ByteArrayInputStream;
2930
import java.io.FileNotFoundException;
3031
import java.io.IOException;
32+
import java.io.InputStreamReader;
3133
import java.io.Reader;
3234
import java.net.MalformedURLException;
3335
import java.net.URL;
@@ -470,6 +472,25 @@ protected void wrapUp(GHRepository[] page) {
470472
};
471473
}
472474

475+
/**
476+
* Render a Markdown document in raw mode.
477+
*
478+
* <p>
479+
* It takes a Markdown document as plaintext and renders it as plain Markdown
480+
* without a repository context (just like a README.md file is rendered – this
481+
* is the simplest way to preview a readme online).
482+
*
483+
* @see GHRepository#renderMarkdown(String, MarkdownMode)
484+
*/
485+
public Reader renderMarkdown(String text) throws IOException {
486+
return new InputStreamReader(
487+
new Requester(this)
488+
.with(new ByteArrayInputStream(text.getBytes("UTF-8")))
489+
.contentType("text/plain;charset=UTF-8")
490+
.read("/markdown/raw"),
491+
"UTF-8");
492+
}
493+
473494
/*package*/ static URL parseURL(String s) {
474495
try {
475496
return s==null ? null : new URL(s);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.kohsuke.github;
2+
3+
import java.util.Locale;
4+
5+
/**
6+
* Rendering mode of markdown.
7+
*
8+
* @author Kohsuke Kawaguchi
9+
* @see GitHub#renderMarkdown(String)
10+
* @see GHRepository#renderMarkdown(String, MarkdownMode)
11+
*/
12+
public enum MarkdownMode {
13+
/**
14+
* Render a document as plain Markdown, just like README files are rendered.
15+
*/
16+
MARKDOWN,
17+
/**
18+
* Render a document as user-content, e.g. like user comments or issues are rendered.
19+
* In GFM mode, hard line breaks are always taken into account, and issue and user
20+
* mentions are linked accordingly.
21+
*
22+
* @see GHRepository#renderMarkdown(String, MarkdownMode)
23+
*/
24+
GFM;
25+
26+
public String toString() {
27+
return name().toLowerCase(Locale.ENGLISH);
28+
}
29+
}

src/main/java/org/kohsuke/github/Requester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public InputStream read(String tailApiUrl) throws IOException {
251251
buildRequest(uc);
252252

253253
try {
254-
return uc.getInputStream();
254+
return wrapStream(uc,uc.getInputStream());
255255
} catch (IOException e) {
256256
handleApiError(e,uc);
257257
}

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.common.collect.Iterables;
55
import com.google.common.collect.Lists;
66

7+
import org.apache.commons.io.IOUtils;
78
import org.junit.Assume;
89
import org.junit.Test;
910
import org.kohsuke.github.GHCommit.File;
@@ -758,11 +759,24 @@ public void testIssue162() throws Exception {
758759
String content1 = content.getContent();
759760
String content2 = r.getFileContent(content.getPath(), "gh-pages").getContent();
760761
System.out.println(content.getPath());
761-
assertEquals(content1,content2);
762+
assertEquals(content1, content2);
762763
}
763764
}
764765
}
765766

767+
@Test
768+
public void markDown() throws Exception {
769+
assertEquals("<p><strong>Test日本語</strong></p>", IOUtils.toString(gitHub.renderMarkdown("**Test日本語**")).trim());
770+
771+
String actual = IOUtils.toString(gitHub.getRepository("kohsuke/github-api").renderMarkdown("@kohsuke to fix issue #1", MarkdownMode.GFM));
772+
System.out.println(actual);
773+
assertTrue(actual.contains("href=\"https://github.com/kohsuke\""));
774+
assertTrue(actual.contains("href=\"https://github.com/kohsuke/github-api/pull/1\""));
775+
assertTrue(actual.contains("class=\"user-mention\""));
776+
assertTrue(actual.contains("class=\"issue-link\""));
777+
assertTrue(actual.contains("to fix issue"));
778+
}
779+
766780
private void kohsuke() {
767781
String login = getUser().getLogin();
768782
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

0 commit comments

Comments
 (0)