Skip to content

Commit 97e4f39

Browse files
authored
Clarify and fix isRepoRoot (#25)
1 parent 188b80e commit 97e4f39

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,24 @@ collect.set('isRepoHome', [
293293
'https://github.com/sindresorhus/refined-github?files=1',
294294
]);
295295

296-
export const isRepoRoot = (url?: URL | Location): boolean =>
297-
/^(tree\/[^/]+)?$/.test(getRepoPath(url ?? location)!) &&
298-
(url ? true : (document.title.startsWith(getRepoURL()) && !document.title.endsWith(getRepoURL()))); // #15
296+
export const isRepoRoot = (url?: URL | Location): boolean => {
297+
const repoPath = getRepoPath(url ?? location);
298+
299+
if (repoPath === '') {
300+
// Absolute repo root: `isRepoHome`
301+
return true;
302+
}
303+
304+
if (url) {
305+
// Root of a branch/commit/tag
306+
return /^tree\/[^/]+$/.test(repoPath!);
307+
}
308+
309+
// If we're checking the current page, add support for branches with slashes // #15 #24
310+
const repoURL = getRepoURL();
311+
return String(repoPath).startsWith('tree/') && document.title.startsWith(repoURL) && !document.title.endsWith(repoURL);
312+
};
313+
299314
collect.set('isRepoRoot', [
300315
...collect.get('isRepoHome') as string[],
301316
'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons',

0 commit comments

Comments
 (0)