Skip to content

Commit 9e7eaca

Browse files
committed
fix(gitbook): support root
1 parent 554f887 commit 9e7eaca

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
},
7878
"dependencies": {
7979
"browserify": "^14.4.0",
80-
"position-map-text-to-markdown": "^1.0.1"
80+
"position-map-text-to-markdown": "^1.0.1",
81+
"url-join": "^2.0.2"
8182
}
8283
}

src/plugin.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var path = require("path");
1+
var urlJoin = require("url-join");
22
var BugReporter = require("./BugReporter");
33
var findAllPositions = require("position-map-text-to-markdown").findAllPositions;
44

@@ -20,26 +20,26 @@ function getContentAsync(apiURL) {
2020

2121
function getResourceURL(config, filePath, branch) {
2222
if (config["markdownBaseURL"]) {
23-
return path.join(config["markdownBaseURL"], filePath);
23+
return urlJoin(config["markdownBaseURL"], filePath);
2424
}
25-
return `https://github.com/${config.repo}/blob/${branch}/${filePath}`
25+
return urlJoin(`https://github.com/`, config.repo, `blob`, branch, filePath);
2626
}
2727

2828
function getEditURL(config, filePath, branch) {
29-
return `https://github.com/${config.repo}/edit/${branch}/${filePath}`
29+
return urlJoin(`https://github.com/`, config.repo, `edit`, branch, filePath);
3030
}
3131
function getAPIURL(config, filePath) {
3232
if (config["githubAPIBaseURL"]) {
33-
return path.join(config["githubAPIBaseURL"], filePath);
33+
return urlJoin(config["githubAPIBaseURL"], filePath);
3434
}
35-
return `https://api.github.com/repos/${config.repo}/contents/${filePath}`;
35+
return urlJoin(`https://api.github.com/repos/`, config.repo, `contents`, filePath);
3636
}
3737

3838
function getIssueURL(config) {
3939
if (config["newIssueURL"]) {
4040
return config["newIssueURL"];
4141
}
42-
return `https://github.com/${config.repo}/issues/new`
42+
return urlJoin(`https://github.com/`, config.repo, `/issues/new`);
4343
}
4444
window.require(["gitbook"], function(gitbook) {
4545
// plugin config
@@ -51,8 +51,9 @@ window.require(["gitbook"], function(gitbook) {
5151
reportElement.setAttribute("style", "position:fixed; right:0;bottom:0;");
5252
var clickEvent = ("ontouchstart" in window) ? "touchend" : "click";
5353
reportElement.addEventListener(clickEvent, function(event) {
54-
var apiURL = getAPIURL(config, gitbook.state.filepath);
55-
var resourceURL = getResourceURL(config, gitbook.state.filepath, "master");
54+
var pathname = urlJoin(gitbook.state.config.root, gitbook.state.filepath);
55+
var apiURL = getAPIURL(config, pathname);
56+
var resourceURL = getResourceURL(config, pathname, "master");
5657
var editURL = getEditURL(config, gitbook.state.filepath, "master");
5758
getContentAsync(apiURL).then(function(markdown) {
5859
var bug = new BugReporter(getIssueURL(config));

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,6 +3317,10 @@ universal-deep-strict-equal@^1.2.1:
33173317
indexof "0.0.1"
33183318
object-keys "^1.0.0"
33193319

3320+
url-join@^2.0.2:
3321+
version "2.0.2"
3322+
resolved "https://registry.yarnpkg.com/url-join/-/url-join-2.0.2.tgz#c072756967ad24b8b59e5741551caac78f50b8b7"
3323+
33203324
url@^0.11.0, url@~0.11.0:
33213325
version "0.11.0"
33223326
resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1"

0 commit comments

Comments
 (0)