Skip to content

Commit a0ef6b8

Browse files
authored
Include link of current page in documentation issue. (#2366)
1 parent a4a7d1f commit a0ef6b8

File tree

6 files changed

+89
-44
lines changed

6 files changed

+89
-44
lines changed

site/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<script src="/static/js/plugins/plugins.js"></script>
3434
<script src="/static/js/plugins/copy_buttons.js"></script>
3535
<script src="/static/js/plugins/book_page_title.js"></script>
36+
<script src="/static/js/plugins/github_widget.js"></script>
3637
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js"></script>
3738
<script src="https://cdn.jsdelivr.net/npm/docsify-themeable@0/dist/js/docsify-themeable.min.js"></script>
3839
<script src="//cdn.jsdelivr.net/npm/docsify-pagination/dist/docsify-pagination.min.js"></script>

site/static/js/plugins/book_page_title.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function processBookPageTitle(content) {
2424
// Load plugins into Docsify.
2525
window.$docsify = window.$docsify || {};
2626
window.$docsify.plugins = [
27-
(hook) => hook.beforeEach(processBookPageTitle),
27+
(hook, _vm) => hook.beforeEach(processBookPageTitle),
2828
].concat(window.$docsify.plugins || []);
2929

3030
// Export functions for testing.

site/static/js/plugins/copy_buttons.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function addCodeCopyButtons() {
4040

4141
// Load plugins into Docsify.
4242
window.$docsify = window.$docsify || {};
43-
window.$docsify.plugins = [(hook) => hook.doneEach(addCodeCopyButtons)].concat(
43+
window.$docsify.plugins = [(hook, _vm) => hook.doneEach(addCodeCopyButtons)].concat(
4444
window.$docsify.plugins || []
4545
);
4646

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Matches a path like /book/02-concepts/01-packages
2+
const bookPath = /^\/book\/(\d+)-(.+)\/(\d+)?-?(.+)?/;
3+
4+
const issueIcon = document.createElement("span");
5+
issueIcon.innerText = "bug_report";
6+
issueIcon.classList.add("material-icons-outlined");
7+
const createIssue = document.createElement("a");
8+
createIssue.id = "create_issue_button";
9+
createIssue.target = "_blank";
10+
createIssue.title = "Create documentation issue";
11+
createIssue.appendChild(issueIcon);
12+
13+
const editIcon = document.createElement("span");
14+
editIcon.innerText = "edit";
15+
editIcon.classList.add("material-icons-outlined");
16+
const editPage = document.createElement("a");
17+
editPage.id = "edit_page_button";
18+
editPage.target = "_blank";
19+
editPage.title = "Edit this page";
20+
editPage.appendChild(editIcon);
21+
22+
function addGitHubWidget() {
23+
createIssue.href = `https://github.com/GoogleContainerTools/kpt/issues/new?labels=documentation&title=Docs: ${document.title} (${window.location})`;
24+
25+
let path = window.location.pathname;
26+
const pageName = path.match(bookPath) ? "00.md" : "README.md";
27+
path += path.endsWith("/") ? pageName : ".md";
28+
editPage.href = `https://github.com/GoogleContainerTools/kpt/edit/main/site${path}`;
29+
30+
const container = document.createElement("div");
31+
container.classList.add("github-widget");
32+
container.appendChild(createIssue);
33+
container.appendChild(editPage);
34+
document
35+
.getElementsByClassName("docsify-pagination-container")
36+
.item(0)
37+
.append(container);
38+
}
39+
40+
// Load plugins into Docsify.
41+
window.$docsify = window.$docsify || {};
42+
window.$docsify.plugins = [
43+
(hook, _vm) => hook.doneEach(addGitHubWidget),
44+
].concat(window.$docsify.plugins || []);
45+
46+
// Export functions for testing.
47+
if (typeof module !== "undefined") {
48+
module.exports = { addGitHubWidget };
49+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
const plugins = require("./github_widget");
6+
7+
test.each([
8+
[
9+
"https://github.com/GoogleContainerTools/kpt/issues/new?labels=documentation&title=Docs:%20title%20(https://test.test/book/01-the-chapter-title/)",
10+
"https://github.com/GoogleContainerTools/kpt/edit/main/site/book/01-the-chapter-title/00.md",
11+
"book/01-the-chapter-title/",
12+
],
13+
[
14+
"https://github.com/GoogleContainerTools/kpt/issues/new?labels=documentation&title=Docs:%20title%20(https://test.test/book/01-the-chapter-title/05-page.md)",
15+
"https://github.com/GoogleContainerTools/kpt/edit/main/site/book/01-the-chapter-title/05-page.md.md",
16+
"book/01-the-chapter-title/05-page.md",
17+
],
18+
[
19+
"https://github.com/GoogleContainerTools/kpt/issues/new?labels=documentation&title=Docs:%20title%20(https://test.test/faq/)",
20+
"https://github.com/GoogleContainerTools/kpt/edit/main/site/faq/README.md",
21+
"faq/",
22+
],
23+
])("github urls are correct", (expectedIssueUrl, expectedEditUrl, path) => {
24+
// Configure test environment.
25+
delete window.location;
26+
window.location = new URL(path, "https://test.test");
27+
document.title = "title";
28+
const container = document.createElement("div");
29+
container.classList.add("docsify-pagination-container");
30+
31+
document.body.append(container);
32+
plugins.addGitHubWidget();
33+
const issueUrl = document.getElementById("create_issue_button").href;
34+
const editUrl = document.getElementById("edit_page_button").href;
35+
expect(issueUrl).toBe(expectedIssueUrl);
36+
expect(editUrl).toBe(expectedEditUrl);
37+
});

site/static/js/plugins/plugins.js

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,3 @@
1-
// Matches a path like /book/02-concepts/01-packages
2-
const bookPath = /^\/book\/(\d+)-(.+)\/(\d+)?-?(.+)?/;
3-
4-
function addGitHubWidget(hook) {
5-
const issueIcon = document.createElement("span");
6-
issueIcon.innerText = "bug_report";
7-
issueIcon.classList.add("material-icons-outlined");
8-
const createIssue = document.createElement("a");
9-
createIssue.target = "_blank";
10-
createIssue.title = "Create documentation issue";
11-
createIssue.appendChild(issueIcon);
12-
13-
const editIcon = document.createElement("span");
14-
editIcon.innerText = "edit";
15-
editIcon.classList.add("material-icons-outlined");
16-
const editPage = document.createElement("a");
17-
editPage.target = "_blank";
18-
editPage.title = "Edit this page";
19-
editPage.appendChild(editIcon);
20-
21-
// Refresh widget links.
22-
hook.doneEach(function () {
23-
createIssue.href = `https://github.com/GoogleContainerTools/kpt/issues/new?labels=documentation&title=Docs: ${document.title}`;
24-
25-
let path = document.location.pathname;
26-
const pageName = path.match(bookPath) ? "00.md" : "README.md";
27-
path += path.endsWith("/") ? pageName : ".md";
28-
editPage.href = `https://github.com/GoogleContainerTools/kpt/edit/main/site${path}`;
29-
30-
const container = document.createElement("div");
31-
container.classList.add("github-widget");
32-
container.appendChild(createIssue);
33-
container.appendChild(editPage);
34-
document
35-
.getElementsByClassName("docsify-pagination-container")
36-
.item(0)
37-
.append(container);
38-
});
39-
}
40-
411
function convertFromHugo(content) {
422
const hugoHideDirectives = /{{% hide %}}.+?{{% \/hide %}}/gms;
433
const hugoDirectiveTags = /{{.*}}/g;
@@ -159,8 +119,6 @@ function localPlugins(hook, _vm) {
159119
// Reset all external links to their appropriate targets.
160120
hook.doneEach(defaultLinkTargets);
161121

162-
addGitHubWidget(hook);
163-
164122
// Process elements in the navigation sidebar.
165123
hook.doneEach(function () {
166124
const sidebar = document.getElementsByClassName("sidebar-nav").item(0);

0 commit comments

Comments
 (0)