Skip to content

Commit 3deedf1

Browse files
authored
Alternatively use data-original-href in applyToLink (#38)
1 parent a7c8131 commit 3deedf1

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function commentIndicator(hash) {
3939

4040
// Filter out null values
4141
function joinValues(array, delimiter = '/') {
42-
return array.filter(s => s).join(delimiter);
42+
return array.filter(Boolean).join(delimiter);
4343
}
4444

4545
function shortenURL(href, currentUrl = 'https://github.com') {
@@ -235,11 +235,12 @@ function applyToLink(a, currentUrl) {
235235
// Shorten only if the link name hasn't been customized.
236236
// .href automatically adds a / to naked origins so that needs to be tested too
237237
// `trim` makes it compatible with this feature: https://github.com/sindresorhus/refined-github/pull/3085
238+
const url = a.dataset.originalHref ?? a.href;
238239
if (
239-
(a.href === a.textContent.trim() || a.href === `${a.textContent}/`)
240+
(url === a.textContent.trim() || url === `${a.textContent}/`)
240241
&& !a.firstElementChild
241242
) {
242-
const shortened = shortenURL(a.href, currentUrl);
243+
const shortened = shortenURL(url, currentUrl);
243244
a.innerHTML = shortened;
244245
return true;
245246
}

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
"github-reserved-names": "^2.0.4"
3838
},
3939
"devDependencies": {
40-
"@sindresorhus/tsconfig": "^1.0.2",
41-
"ava": "^3.15.0",
42-
"tsd": "^0.17.0",
43-
"xo": "^0.44.0"
40+
"@sindresorhus/tsconfig": "^3.0.1",
41+
"ava": "^4.3.3",
42+
"tsd": "^0.23.0",
43+
"xo": "^0.52.3"
4444
},
4545
"engines": {
46-
"node": ">=10.0.0"
46+
"node": ">=14.0.0"
4747
}
4848
}

readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ The URL of the current page, to build relative URLs like `<code>v0.12</code>` in
5252

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

55-
Automatically shorten the link's text if the text matches the URL, i.e. `<a href="https://github.com">https://github.com</a>`
55+
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.
56+
57+
Note: this function will never change the `href` of the link, it only changes the text.
5658

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

0 commit comments

Comments
 (0)