Skip to content

Commit 8677173

Browse files
knsh14clayreimann
authored andcommitted
feature(markdown): add markdown api
Closes #325
1 parent b4995e0 commit 8677173

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

lib/GitHub.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Search from './Search';
1212
import RateLimit from './RateLimit';
1313
import Repository from './Repository';
1414
import Organization from './Organization';
15+
import Markdown from './Markdown';
1516

1617
/**
1718
* GitHub encapsulates the functionality to create various API wrapper objects.
@@ -93,6 +94,14 @@ class GitHub {
9394
return new RateLimit(this.__auth, this.__apiBase);
9495
}
9596

97+
/**
98+
* Create a new Markdown wrapper
99+
* @return {Markdown}
100+
*/
101+
getMarkdown() {
102+
return new Markdown(this.__auth, this.__apiBase);
103+
}
104+
96105
_getFullName(user, repo) {
97106
let fullname = user;
98107

lib/Markdown.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @file
3+
* @copyright 2013 Michael Aufreiter (Development Seed) and 2016 Yahoo Inc.
4+
* @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}.
5+
* Github.js is freely distributable.
6+
*/
7+
8+
import Requestable from './Requestable';
9+
10+
/**
11+
* RateLimit allows users to query their rate-limit status
12+
*/
13+
class Markdown extends Requestable {
14+
/**
15+
* construct a RateLimit
16+
* @param {Requestable.auth} auth - the credentials to authenticate to GitHub
17+
* @param {string} [apiBase] - the base Github API URL
18+
* @return {Promise} - the promise for the http request
19+
*/
20+
constructor(auth, apiBase) {
21+
super(auth, apiBase);
22+
}
23+
24+
/**
25+
* Render html from Markdown text.
26+
* @see https://developer.github.com/v3/markdown/#render-an-arbitrary-markdown-document
27+
* @param {Object} options
28+
* @param {string} [options.text] - the markdown text to convert
29+
* @param {string} [options.mode=markdown] - can be either `markdown` or `gfm`
30+
* @param {string} [options.context] - repository name if mode is gfm
31+
* @param {Requestable.callback} [cb] - will receive the converted html
32+
* @return {Promise} - the promise for the http request
33+
*/
34+
render(options, cb) {
35+
return this._request('POST', '/markdown', options, cb);
36+
}
37+
}
38+
39+
module.exports = Markdown;

test/markdown.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import expect from 'must';
2+
3+
import Github from '../lib/GitHub';
4+
import testUser from './fixtures/user.json';
5+
6+
describe('Markdown', function() {
7+
let github;
8+
let markdown;
9+
10+
before(function() {
11+
github = new Github({
12+
username: testUser.USERNAME,
13+
password: testUser.PASSWORD,
14+
auth: 'basic'
15+
});
16+
17+
markdown = github.getMarkdown();
18+
});
19+
20+
it('should convert markdown to html as plain Markdown', function(done) {
21+
const options = {
22+
text: 'Hello world github/linguist#1 **cool**, and #1!'
23+
};
24+
25+
markdown.render(options)
26+
.then(function({data: html}) {
27+
expect(html).to.be('<p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>\n');
28+
done();
29+
}).catch(done);
30+
});
31+
32+
it('should convert markdown to html as GFM', function(done) {
33+
const options = {
34+
text: 'Hello world github/linguist#1 **cool**, and #1!',
35+
mode: 'gfm',
36+
context: 'github/gollum'
37+
};
38+
markdown.render(options)
39+
.then(function({data: html}) {
40+
expect(html).to.be('<p>Hello world <a href="https://github.com/github/linguist/issues/1" class="issue-link js-issue-link" data-url="https://github.com/github/linguist/issues/1" data-id="1012654" data-error-text="Failed to load issue title" data-permission-text="Issue title is private">github/linguist#1</a> <strong>cool</strong>, and <a href="https://github.com/gollum/gollum/issues/1" class="issue-link js-issue-link" data-url="https://github.com/gollum/gollum/issues/1" data-id="183433" data-error-text="Failed to load issue title" data-permission-text="Issue title is private">#1</a>!</p>'); // jscs:ignore
41+
done();
42+
}).catch(done);
43+
});
44+
});

0 commit comments

Comments
 (0)