Skip to content

Commit 4b1fd96

Browse files
authored
Merge pull request #5 from yaeda/support-private
feat(plugin): support private repository
2 parents af719a3 + 1777837 commit 4b1fd96

File tree

5 files changed

+89
-10
lines changed

5 files changed

+89
-10
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ You should set `repo` in your `book.json`.
3333
}
3434
```
3535

36+
- `private`: `boolean` (optional, default is `false`)
37+
38+
For private repositories, set this flag.
39+
40+
```json
41+
{
42+
"gitbook": ">=3.0.0",
43+
"title": "Example",
44+
"plugins": [
45+
"github-issue-feedback"
46+
],
47+
"pluginsConfig": {
48+
"github-issue-feedback": {
49+
"repo": "your/private_repo",
50+
"private": true
51+
}
52+
}
53+
}
54+
```
55+
3656
## Changelog
3757

3858
See [Releases page](https://github.com/azu/gitbook-plugin-github-issue-feedback/releases).

index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
// MIT © 2017 azu
22
"use strict";
3+
4+
const path = require("path")
5+
const fse = require("fs-extra")
6+
37
module.exports = {
48
website: {
59
assets: "./assets",
610
js: [
711
"plugin.js"
812
]
13+
},
14+
hooks: {
15+
finish: function() {
16+
// check configuration
17+
const pluginsConfig = this.config.get("pluginsConfig")
18+
const isPrivateRepo =
19+
pluginsConfig !== undefined &&
20+
pluginsConfig["github-issue-feedback"] !== undefined &&
21+
pluginsConfig["github-issue-feedback"].private === true
22+
23+
// copy contents for private repository
24+
if (isPrivateRepo) {
25+
const source = this.config.get("root", "./")
26+
const target = path.join("./_book/gitbook/gitbook-plugin-github-issue-feedback/contents", source)
27+
fse.copy(source, target)
28+
}
29+
}
930
}
10-
};
31+
};

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
"type": "string",
3939
"description": "GitHub API Base url e.g.) https://api.github.com/repos/asciidwango/js-primer/blob/master/",
4040
"required": false
41+
},
42+
"private": {
43+
"type": "boolean",
44+
"description": "Is repo private or not?",
45+
"required": false
4146
}
4247
}
4348
},
@@ -76,6 +81,7 @@
7681
"webpack": "^2.6.1"
7782
},
7883
"dependencies": {
84+
"fs-extra": "^5.0.0",
7985
"position-map-text-to-markdown": "^1.0.1",
8086
"url-join": "^2.0.2"
8187
}

src/plugin.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ function quoteText(text) {
99
}).join("\n");
1010
}
1111

12-
function getContentAsync(apiURL) {
12+
function getContentAsync(contentURL) {
13+
if (/^https:\/\/api.github.com\/repos/.test(contentURL)) {
1314
// https://github.com/jser/jser.info/edit/gh-pages/data/2015/08/index.json
1415
// => https://api.github.com/repos/jser/jser.info/contents/data/2015/08/index.json
15-
return fetch(apiURL).then(function(response) {
16+
return fetch(contentURL).then(function(response) {
1617
return response.json();
1718
}).then(function(response) {
1819
return decodeURIComponent(escape(atob(response.content)));
1920
});
21+
} else {
22+
return fetch(contentURL).then(function(response) {
23+
return response.text();
24+
});
25+
}
2026
}
2127

2228
function getResourceURL(config, filePath, branch) {
@@ -29,11 +35,19 @@ function getResourceURL(config, filePath, branch) {
2935
function getEditURL(config, filePath, branch) {
3036
return urlJoin(`https://github.com/`, config.repo, `edit`, branch, filePath);
3137
}
32-
function getAPIURL(config, filePath) {
33-
if (config["githubAPIBaseURL"]) {
34-
return urlJoin(config["githubAPIBaseURL"], filePath);
38+
function getContentURL(config, filePath) {
39+
if (config.private) {
40+
return urlJoin(
41+
location.origin,
42+
"gitbook/gitbook-plugin-github-issue-feedback/contents",
43+
filePath
44+
);
45+
} else {
46+
if (config["githubAPIBaseURL"]) {
47+
return urlJoin(config["githubAPIBaseURL"], filePath);
48+
}
49+
return urlJoin(`https://api.github.com/repos/`, config.repo, `contents`, filePath);
3550
}
36-
return urlJoin(`https://api.github.com/repos/`, config.repo, `contents`, filePath);
3751
}
3852

3953
function getIssueURL(config) {
@@ -53,14 +67,14 @@ window.require(["gitbook"], function(gitbook) {
5367
var clickEvent = ("ontouchstart" in window) ? "touchend" : "click";
5468
reportElement.addEventListener(clickEvent, function(event) {
5569
var pathname = path.join(gitbook.state.config.root || "./", gitbook.state.filepath);
56-
var apiURL = getAPIURL(config, pathname);
70+
var contentURL = getContentURL(config, pathname);
5771
var resourceURL = getResourceURL(config, pathname, "master");
5872
var editURL = getEditURL(config, pathname, "master");
5973
var chapterTitle = gitbook.state.chapterTitle;
6074
var bug = new BugReporter(getIssueURL(config));
6175
var selectedText = bug.getSelectedText().trim();
6276
bug.setTitle(chapterTitle);
63-
getContentAsync(apiURL).then(function(markdown) {
77+
getContentAsync(contentURL).then(function(markdown) {
6478
let body = 'URL : ' + resourceURL + "\n\n";
6579
if (selectedText && selectedText.length > 0) {
6680
var matches = findAllPositions({

yarn.lock

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,14 @@ form-data@~2.1.1:
13501350
combined-stream "^1.0.5"
13511351
mime-types "^2.1.12"
13521352

1353+
fs-extra@^5.0.0:
1354+
version "5.0.0"
1355+
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-5.0.0.tgz#414d0110cdd06705734d055652c5411260c31abd"
1356+
dependencies:
1357+
graceful-fs "^4.1.2"
1358+
jsonfile "^4.0.0"
1359+
universalify "^0.1.0"
1360+
13531361
fs-readdir-recursive@^1.0.0:
13541362
version "1.0.0"
13551363
resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.0.0.tgz#8cd1745c8b4f8a29c8caec392476921ba195f560"
@@ -1437,7 +1445,7 @@ globals@^9.0.0:
14371445
version "9.17.0"
14381446
resolved "https://registry.yarnpkg.com/globals/-/globals-9.17.0.tgz#0c0ca696d9b9bb694d2e5470bd37777caad50286"
14391447

1440-
graceful-fs@^4.1.2, graceful-fs@^4.1.4:
1448+
graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6:
14411449
version "4.1.11"
14421450
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
14431451

@@ -1771,6 +1779,12 @@ json5@^0.5.0, json5@^0.5.1:
17711779
version "0.5.1"
17721780
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
17731781

1782+
jsonfile@^4.0.0:
1783+
version "4.0.0"
1784+
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
1785+
optionalDependencies:
1786+
graceful-fs "^4.1.6"
1787+
17741788
jsonify@~0.0.0:
17751789
version "0.0.0"
17761790
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
@@ -2975,6 +2989,10 @@ universal-deep-strict-equal@^1.2.1:
29752989
indexof "0.0.1"
29762990
object-keys "^1.0.0"
29772991

2992+
universalify@^0.1.0:
2993+
version "0.1.1"
2994+
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.1.tgz#fa71badd4437af4c148841e3b3b165f9e9e590b7"
2995+
29782996
url-join@^2.0.2:
29792997
version "2.0.2"
29802998
resolved "https://registry.yarnpkg.com/url-join/-/url-join-2.0.2.tgz#c072756967ad24b8b59e5741551caac78f50b8b7"

0 commit comments

Comments
 (0)