Skip to content

Commit

Permalink
Alternatively use data-original-href in applyToLink (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Sep 12, 2022
1 parent a7c8131 commit 3deedf1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function commentIndicator(hash) {

// Filter out null values
function joinValues(array, delimiter = '/') {
return array.filter(s => s).join(delimiter);
return array.filter(Boolean).join(delimiter);
}

function shortenURL(href, currentUrl = 'https://github.com') {
Expand Down Expand Up @@ -235,11 +235,12 @@ function applyToLink(a, currentUrl) {
// Shorten only if the link name hasn't been customized.
// .href automatically adds a / to naked origins so that needs to be tested too
// `trim` makes it compatible with this feature: https://github.com/sindresorhus/refined-github/pull/3085
const url = a.dataset.originalHref ?? a.href;
if (
(a.href === a.textContent.trim() || a.href === `${a.textContent}/`)
(url === a.textContent.trim() || url === `${a.textContent}/`)
&& !a.firstElementChild
) {
const shortened = shortenURL(a.href, currentUrl);
const shortened = shortenURL(url, currentUrl);
a.innerHTML = shortened;
return true;
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
"github-reserved-names": "^2.0.4"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^1.0.2",
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
"@sindresorhus/tsconfig": "^3.0.1",
"ava": "^4.3.3",
"tsd": "^0.23.0",
"xo": "^0.52.3"
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
}
}
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ The URL of the current page, to build relative URLs like `<code>v0.12</code>` in

### 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>`
Automatically shorten the link's text if the text matches the URL, i.e. `<a href="https://github.com">https://github.com</a>`. If a `data-original-href` attribute is present, it will be used when comparing the link’s text and when generating the shortened URL.

Note: this function will never change the `href` of the link, it only changes the text.

It will return `true` or `false` depending on whether the link was shortened.

Expand Down

0 comments on commit 3deedf1

Please sign in to comment.