Skip to content

Commit

Permalink
Add currentUrl parameter to applyToLink
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jun 24, 2017
1 parent 90a32f3 commit 04142b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,21 @@ Type: `string`, like `location.href`

The URL of the current page, to build relative URLs like `<code>v0.12</code>` instead of the longer `nodejs/node@<code>v0.12</code>`

### shortenRepoUrl.applyToLink(link)
### shortenRepoUrl.applyToLink(link, currentUrl)

Automatically shorten the link's text if the text matches the URL, i.e. `<a href="https://github.com">https://github.com</a>`

#### link

Type: `Element`

Example: `shortenRepoUrl.applyToLink(document.querySelector(a))`

Automatically shorten the link's text if the text matches the URL, i.e. `<a href="https://github.com">https://github.com</a>`
#### currentUrl

Type: `string`, like `location.href`

Same as before.


## License
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ function shortenURL(href, currentLocation = 'https://github.com') {
return `${pathname.replace(/^[/]|[/]$/g, '')}${search}${hash}`;
}

function applyToLink(a) {
function applyToLink(a, currentLocation) {
// Shorten only if the link name hasn't bee customized.
// .href automatically adds a / to naked origins
// so that needs to be tested too
if (a.href === a.textContent || a.href === `${a.textContent}/`) {
const shortened = shortenURL(a.href);
const shortened = shortenURL(a.href, currentLocation);
// Only touch the dom is the URL has been shortened
if (shortened !== a.textContent) {
a.innerHTML = shortened;
Expand Down

0 comments on commit 04142b9

Please sign in to comment.