This repository was archived by the owner on Jul 31, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
main/java/org/kohsuke/github
test/java/org/kohsuke/github Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3030import javax .xml .bind .DatatypeConverter ;
3131import java .io .FileNotFoundException ;
3232import java .io .IOException ;
33+ import java .io .InputStreamReader ;
3334import java .io .InterruptedIOException ;
35+ import java .io .Reader ;
3436import java .net .URL ;
3537import 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
Original file line number Diff line number Diff line change 2626import static com .fasterxml .jackson .annotation .JsonAutoDetect .Visibility .ANY ;
2727import static com .fasterxml .jackson .annotation .JsonAutoDetect .Visibility .NONE ;
2828
29+ import java .io .ByteArrayInputStream ;
2930import java .io .FileNotFoundException ;
3031import java .io .IOException ;
32+ import java .io .InputStreamReader ;
3133import java .io .Reader ;
3234import java .net .MalformedURLException ;
3335import 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 );
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 44import com .google .common .collect .Iterables ;
55import com .google .common .collect .Lists ;
66
7+ import org .apache .commons .io .IOUtils ;
78import org .junit .Assume ;
89import org .junit .Test ;
910import 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" ));
You can’t perform that action at this time.
0 commit comments