|
| 1 | +var path = require("path"); |
1 | 2 | var BugReporter = require("./BugReporter"); |
2 | 3 | var findAllPositions = require("position-map-text-to-markdown").findAllPositions; |
3 | | -var path = require("path"); |
4 | 4 |
|
5 | 5 | function quoteText(text) { |
6 | 6 | return text.split("\n").map(function(line) { |
7 | 7 | return "> " + line; |
8 | 8 | }).join("\n"); |
9 | 9 | } |
10 | 10 |
|
11 | | -function getContentAsync(filePath) { |
| 11 | +function getContentAsync(apiURL) { |
12 | 12 | // https://github.com/jser/jser.info/edit/gh-pages/data/2015/08/index.json |
13 | 13 | // => 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) { |
16 | 15 | return response.json(); |
17 | 16 | }).then(function(response) { |
18 | 17 | return decodeURIComponent(escape(atob(response.content))); |
19 | 18 | }); |
20 | 19 | } |
| 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 | +} |
21 | 44 | window.require(["gitbook"], function(gitbook) { |
22 | 45 | // plugin config |
23 | 46 | gitbook.events.bind("start", function(e, pluginConfig) { |
24 | 47 | var config = pluginConfig["github-issue-feedback"]; |
25 | 48 | var reportElement = document.createElement("button"); |
26 | | - reportElement.textContent = "BugReport"; |
| 49 | + reportElement.textContent = "Bug Report"; |
| 50 | + reportElement.className = "gitbook-plugin-github-issue-feedback"; |
27 | 51 | reportElement.setAttribute("style", "position:fixed; right:0;bottom:0;"); |
28 | 52 | var clickEvent = ("ontouchstart" in window) ? "touchend" : "click"; |
29 | 53 | 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)); |
33 | 59 | var selectedText = bug.getSelectedText().trim(); |
34 | | - let body = 'URL : ' + resourcePath + "\n\n"; |
| 60 | + let body = 'URL : ' + resourceURL + "\n\n"; |
35 | 61 | if (selectedText && selectedText.length > 0) { |
36 | 62 | var matches = findAllPositions({ |
37 | 63 | text: selectedText, |
38 | 64 | markdown: markdown |
39 | 65 | }); |
| 66 | + console.log(matches); |
40 | 67 | 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"; |
43 | 71 | }); |
44 | 72 | } |
45 | 73 | bug.setBody(body); |
|
0 commit comments