Skip to content

Commit e4fe9c8

Browse files
authored
Improve support for certain emojis in labels (#49)
1 parent 07f3216 commit e4fe9c8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,24 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
244244
return pathname.replaceAll(/^[/]|[/]$/g, '') + url.search + hash + query;
245245
}
246246

247+
// Without this, <a>%%</a> would throw an error
248+
function safeDecode(url) {
249+
try {
250+
return decodeURIComponent(url);
251+
} catch {
252+
return url;
253+
}
254+
}
255+
247256
export function applyToLink(a, currentUrl) {
248257
// Shorten only if the link name hasn't been customized.
249258
// .href automatically adds a / to naked origins so that needs to be tested too
250259
// `trim` makes it compatible with this feature: https://github.com/sindresorhus/refined-github/pull/3085
251-
const url = a.dataset.originalHref ?? a.href;
260+
// `safeDecode` is needed because some URLs are encoded in different ways in the DOM and in the `href` property: https://github.com/refined-github/shorten-repo-url/issues/19
261+
const url = safeDecode(a.dataset.originalHref ?? a.href);
262+
const label = safeDecode(a.textContent);
252263
if (
253-
(url === a.textContent.trim() || url === `${a.textContent}/`)
264+
(url === label.trim() || url === `${label}/`)
254265
&& !a.firstElementChild
255266
) {
256267
const shortened = shortenRepoUrl(url, currentUrl);

index.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ test('GitHub.com URLs', urlMatcherMacro, new Map([
206206
'https://github.com/nodejs/node/labels/Please%21%20♥',
207207
'nodejs/node/Please! ♥ (label)',
208208
],
209+
[
210+
'https://github.com/refined-github/refined-github/labels/Please%21%20♥%EF%B8%8E',
211+
'refined-github/refined-github/Please! ♥︎ (label)',
212+
],
209213
[
210214
'https://github.com/fregante/shorten-repo-url/archive/6.4.1.zip',
211215
'<code>6.4.1</code>.zip',

0 commit comments

Comments
 (0)