-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/Issue-97: Implement the Edit on GitHub component (#111)
Co-authored-by: Owen Buckley <owenbuckley13@gmail.com>
- Loading branch information
1 parent
8f68459
commit 9f32e2f
Showing
6 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import styles from "./edit-on-github.module.css"; | ||
|
||
const REPO_PREFIX = "https://github.com/ProjectEvergreen/www.greenwoodjs.dev/edit/main/src/pages"; | ||
const MAX_ROUTE_DEPTH = 5; | ||
|
||
// one / two segments - /guides/ -> /guides/index.md | ||
// three (max) segments - /guides/getting-started/key-concepts/ -> /guides/getting-started/key-concepts.md | ||
function convertRouteToEditLink(route) { | ||
const segments = route.split("/"); | ||
|
||
// https://stackoverflow.com/a/5497365/417806 | ||
return segments.length === MAX_ROUTE_DEPTH | ||
? `${route.replace(/\/([^/]*)$/, "")}.md` | ||
: `${route}index.md`; | ||
} | ||
|
||
export default class EditOnGitHub extends HTMLElement { | ||
connectedCallback() { | ||
const label = "Edit on GitHub"; | ||
const route = this.getAttribute("route"); | ||
const href = `${REPO_PREFIX}${convertRouteToEditLink(route)}`; | ||
|
||
this.innerHTML = ` | ||
<div class="${styles.container}"> | ||
<a title="${label}" href="${href}" target="_blank"> | ||
${label} | ||
</a> | ||
</div> | ||
`; | ||
} | ||
} | ||
|
||
customElements.define("app-edit-on-github", EditOnGitHub); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.container { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
.container a, | ||
.container a:active, | ||
.container a:focus, | ||
.container a:visited { | ||
padding: calc(var(--size-1) + var(--size-2)); | ||
border-radius: var(--size-px-2); | ||
background-color: var(--color-prism-bg); | ||
color: var(--color-accent); | ||
text-decoration: none; | ||
box-shadow: 0 0 calc(var(--size-1) + var(--size-2)) var(--size-px-00) var(--color-white); | ||
} | ||
|
||
.container a:hover { | ||
text-decoration: underline; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { expect } from "@esm-bundle/chai"; | ||
import "./edit-on-github.js"; | ||
|
||
// attributes | ||
const ROUTE = "/"; | ||
|
||
describe("Components/Edit on GitHub", () => { | ||
let editWrapper; | ||
let editLink; | ||
|
||
before(async () => { | ||
editWrapper = document.createElement("app-edit-on-github"); | ||
editWrapper.setAttribute("route", ROUTE); | ||
|
||
document.body.appendChild(editWrapper); | ||
|
||
await editWrapper.updateComplete; | ||
|
||
editLink = editWrapper.querySelector("a"); | ||
}); | ||
|
||
it("should not be undefined", () => { | ||
expect(editWrapper).not.equal(undefined); | ||
}); | ||
|
||
describe("Anchor tag to GitHub", () => { | ||
const EXPECTED_BASE = | ||
"https://github.com/ProjectEvergreen/www.greenwoodjs.dev/edit/main/src/pages/"; // including trailing | ||
|
||
it("should render an anchor tag targeting a new window", () => { | ||
expect(editLink).not.equal(undefined); | ||
expect(editLink.getAttribute("target")).equal("_blank"); | ||
}); | ||
|
||
it("should include a title attribute with the label 'Edit on GitHub'", () => { | ||
expect(editLink.getAttribute("title")).equal("Edit on GitHub"); | ||
}); | ||
|
||
it("should render the text 'Edit on GitHub'", () => { | ||
// use trim to allow backtick use in code | ||
expect(editLink.text.trim()).equal("Edit on GitHub"); | ||
}); | ||
|
||
describe("when the route is ONLY a slash", () => { | ||
it("should return 'index.md' for root-level ('/')", () => { | ||
const expected = `${EXPECTED_BASE}index.md`; | ||
|
||
expect(editLink.getAttribute("href")).equal(expected); | ||
}); | ||
}); | ||
|
||
describe("when the route IS NOT a directory", () => { | ||
let staticFilepath; | ||
|
||
before(async () => { | ||
staticFilepath = "/some/static/filename/"; // ie: "/guides/hosting/netlify/" | ||
|
||
editWrapper = document.createElement("app-edit-on-github"); | ||
editWrapper.setAttribute("route", staticFilepath); | ||
|
||
document.body.appendChild(editWrapper); | ||
|
||
await editWrapper.updateComplete; | ||
|
||
editLink = editWrapper.querySelector("a"); | ||
}); | ||
|
||
it("should include a href attribute which includes filepath.md", () => { | ||
const expected = `${EXPECTED_BASE}some/static/filename.md`; | ||
|
||
expect(editLink.getAttribute("href")).equal(expected); | ||
}); | ||
}); | ||
|
||
describe("when the route IS a directory", () => { | ||
let missingTrailingPath; | ||
|
||
before(async () => { | ||
missingTrailingPath = "/some/path/to/hosting/"; // ie: "/guides/hosting/" | ||
|
||
editWrapper = document.createElement("app-edit-on-github"); | ||
editWrapper.setAttribute("route", missingTrailingPath); | ||
|
||
document.body.appendChild(editWrapper); | ||
|
||
await editWrapper.updateComplete; | ||
|
||
editLink = editWrapper.querySelector("a"); | ||
}); | ||
|
||
it("should include a href attribute which includes index.md", () => { | ||
const expected = `${EXPECTED_BASE}some/path/to/hosting/index.md`; | ||
|
||
expect(editLink.getAttribute("href")).equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import "./edit-on-github.js"; | ||
|
||
export default { | ||
title: "Components/Edit on GitHub", | ||
component: "app-edit-on-github", | ||
}; | ||
|
||
const Template = (props) => { | ||
return ` | ||
<div style="margin:1em 0;"><p>Linked Route: ${props.args.route}</p></div> | ||
<app-edit-on-github route="${props.args.route}"></app-edit-on-github> | ||
`; | ||
}; | ||
|
||
export const PageRoot = Template.bind( | ||
{}, | ||
{ | ||
args: { | ||
route: "/guides/", | ||
}, | ||
}, | ||
); | ||
|
||
export const SectionRoot = Template.bind( | ||
{}, | ||
{ | ||
args: { | ||
route: "/guides/hosting/", | ||
}, | ||
}, | ||
); | ||
|
||
export const Section = Template.bind( | ||
{}, | ||
{ | ||
args: { | ||
route: "/guides/hosting/netlify/", | ||
}, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters