Skip to content

Commit 710e8ec

Browse files
committed
Update Book
1 parent 2916b0b commit 710e8ec

File tree

6 files changed

+161
-23
lines changed

6 files changed

+161
-23
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,26 @@ Install with [npm](https://www.npmjs.com/):
1010

1111
## Usage
1212

13-
- [ ] Write usage instructions
13+
You should set `repo` in your `book.json`.
14+
15+
`book.json`
16+
17+
- `repo`: `owner/name`
18+
19+
```json
20+
{
21+
"gitbook": ">=3.0.0",
22+
"title": "Example",
23+
"plugins": [
24+
"github-issue-feedback"
25+
],
26+
"pluginsConfig": {
27+
"github-issue-feedback": {
28+
"repo": "azu/gitbook-plugin-github-issue-feedback"
29+
}
30+
}
31+
}
32+
```
1433

1534
## Changelog
1635

example/README.md

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,67 @@
1-
# Example
1+
# gitbook-plugin-github-issue-feedback
22

3-
## How to Feedback
3+
GitHub issue feedback for gitbook.
44

5-
1. Select Text
6-
2. Press Feedback
5+
![feeback button](https://monosnap.com/file/h2F1HbP6l58FkJoNXgnLNvD8LBvcbu.png)
76

8-
Dummy text.
7+
- Put button to right button
8+
9+
## Install
10+
11+
Install with [npm](https://www.npmjs.com/):
12+
13+
npm install gitbook-plugin-github-issue-feedback
14+
15+
## Usage
16+
17+
You should set `repo` in your `book.json`.
18+
19+
`book.json`
20+
21+
- `repo`: `owner/name`
22+
23+
```json
24+
{
25+
"gitbook": ">=3.0.0",
26+
"title": "Example",
27+
"plugins": [
28+
"github-issue-feedback"
29+
],
30+
"pluginsConfig": {
31+
"github-issue-feedback": {
32+
"repo": "azu/gitbook-plugin-github-issue-feedback"
33+
}
34+
}
35+
}
36+
```
37+
38+
## Changelog
39+
40+
See [Releases page](https://github.com/azu/gitbook-plugin-github-issue-feedback/releases).
41+
42+
## Running tests
43+
44+
Install devDependencies and Run `npm test`:
45+
46+
npm i -d && npm test
47+
48+
## Contributing
49+
50+
Pull requests and stars are always welcome.
51+
52+
For bugs and feature requests, [please create an issue](https://github.com/azu/gitbook-plugin-github-issue-feedback/issues).
53+
54+
1. Fork it!
55+
2. Create your feature branch: `git checkout -b my-new-feature`
56+
3. Commit your changes: `git commit -am 'Add some feature'`
57+
4. Push to the branch: `git push origin my-new-feature`
58+
5. Submit a pull request :D
59+
60+
## Author
61+
62+
- [github/azu](https://github.com/azu)
63+
- [twitter/azu_re](https://twitter.com/azu_re)
64+
65+
## License
66+
67+
MIT © azu

example/book.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ module.exports = {
77
],
88
"pluginsConfig": {
99
"github-issue-feedback": {
10-
"newIssueURL": "https://github.com/asciidwango/js-primer/issues/new",
11-
"markdownBaseURL": "https://github.com/asciidwango/js-primer/contents",
12-
"githubAPIBaseURL": "https://api.github.com/repos/asciidwango/js-primer/blob/master/"
10+
"repo": "azu/gitbook-plugin-github-issue-feedback"
1311
}
1412
}
1513
};

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,32 @@
1919
},
2020
"gitbook": {
2121
"properties": {
22+
"repo": {
23+
"type": "string",
24+
"description": "GitHub repo path. e.g.) asciidwango/js-primer ",
25+
"required": true
26+
},
2227
"newIssueURL": {
2328
"type": "string",
2429
"description": "GitHub New Issue url e.g.) https://github.com/azu/promises-book/issues/new",
25-
"required": true
30+
"required": false
2631
},
2732
"markdownBaseURL": {
2833
"type": "string",
2934
"description": "Base url for resouce e.g.) https://github.com/asciidwango/js-primer/blob/master/",
30-
"required": true
35+
"required": false
36+
},
37+
"githubAPIBaseURL": {
38+
"type": "string",
39+
"description": "GitHub API Base url e.g.) https://api.github.com/repos/asciidwango/js-primer/blob/master/",
40+
"required": false
3141
}
3242
}
3343
},
3444
"scripts": {
3545
"test": "mocha test/",
36-
"build": "browserify src/plugin.js -o assets/plugin.js",
46+
"build": "webpack -p",
47+
"watch": "webpack --watch",
3748
"prepublish": "npm run --if-present build"
3849
},
3950
"keywords": [

src/plugin.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
1+
var path = require("path");
12
var BugReporter = require("./BugReporter");
23
var findAllPositions = require("position-map-text-to-markdown").findAllPositions;
3-
var path = require("path");
44

55
function quoteText(text) {
66
return text.split("\n").map(function(line) {
77
return "> " + line;
88
}).join("\n");
99
}
1010

11-
function getContentAsync(filePath) {
11+
function getContentAsync(apiURL) {
1212
// https://github.com/jser/jser.info/edit/gh-pages/data/2015/08/index.json
1313
// => https://api.github.com/repos/jser/jser.info/contents/data/2015/08/index.json
14-
var apiPath = filePath.replace("github.com/", "api.github.com/repos/");
15-
return fetch(apiPath).then(function(response) {
14+
return fetch(apiURL).then(function(response) {
1615
return response.json();
1716
}).then(function(response) {
1817
return decodeURIComponent(escape(atob(response.content)));
1918
});
2019
}
20+
21+
function getResourceURL(config, filePath, branch) {
22+
if (config["markdownBaseURL"]) {
23+
return path.join(config["markdownBaseURL"], filePath);
24+
}
25+
return `https://github.com/${config.repo}/blob/${branch}/${filePath}`
26+
}
27+
28+
function getEditURL(config, filePath, branch) {
29+
return `https://github.com/${config.repo}/edit/${branch}/${filePath}`
30+
}
31+
function getAPIURL(config, filePath) {
32+
if (config["githubAPIBaseURL"]) {
33+
return path.join(config["githubAPIBaseURL"], filePath);
34+
}
35+
return `https://api.github.com/repos/${config.repo}/contents/${filePath}`;
36+
}
37+
38+
function getIssueURL(config) {
39+
if (config["newIssueURL"]) {
40+
return config["newIssueURL"];
41+
}
42+
return `https://github.com/${config.repo}/issues/new`
43+
}
2144
window.require(["gitbook"], function(gitbook) {
2245
// plugin config
2346
gitbook.events.bind("start", function(e, pluginConfig) {
2447
var config = pluginConfig["github-issue-feedback"];
2548
var reportElement = document.createElement("button");
26-
reportElement.textContent = "BugReport";
49+
reportElement.textContent = "Bug Report";
50+
reportElement.className = "gitbook-plugin-github-issue-feedback";
2751
reportElement.setAttribute("style", "position:fixed; right:0;bottom:0;");
2852
var clickEvent = ("ontouchstart" in window) ? "touchend" : "click";
2953
reportElement.addEventListener(clickEvent, function(event) {
30-
var resourcePath = path.join(config["markdownBaseURL"], gitbook.state.filepath);
31-
getContentAsync(resourcePath).then(function(markdown) {
32-
var bug = new BugReporter(config["newIssueURL"]);
54+
var apiURL = getAPIURL(config, gitbook.state.filepath);
55+
var resourceURL = getResourceURL(config, gitbook.state.filepath, "master");
56+
var editURL = getEditURL(config, gitbook.state.filepath, "master");
57+
getContentAsync(apiURL).then(function(markdown) {
58+
var bug = new BugReporter(getIssueURL(config));
3359
var selectedText = bug.getSelectedText().trim();
34-
let body = 'URL : ' + resourcePath + "\n\n";
60+
let body = 'URL : ' + resourceURL + "\n\n";
3561
if (selectedText && selectedText.length > 0) {
3662
var matches = findAllPositions({
3763
text: selectedText,
3864
markdown: markdown
3965
});
66+
console.log(matches);
4067
matches.forEach(function(match) {
41-
body += quoteText(match.markdown) + "\n";
42-
body += resourcePath + "#L" + match.loc.start.line + "\n";
68+
var editLink = `[:memo:](${editURL}#L${match.loc.start.line} "Edit")`;
69+
body += quoteText(match.markdown + "\n" +
70+
`${editLink} <${resourceURL}#L${match.loc.start.line}>`) + "\n";
4371
});
4472
}
4573
bug.setBody(body);

webpack.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const webpack = require('webpack');
2+
const path = require('path');
3+
module.exports = {
4+
entry: "./src/plugin.js",
5+
6+
output: {
7+
filename: 'plugin.js',
8+
path: path.resolve(__dirname, 'assets')
9+
},
10+
11+
module: {
12+
rules: [
13+
{
14+
test: /\.js$/,
15+
exclude: /node_modules/,
16+
loader: 'babel-loader',
17+
options: {
18+
presets: ['es2015']
19+
}
20+
}
21+
]
22+
}
23+
};

0 commit comments

Comments
 (0)