Repository navigation
Expand file tree
/
Copy pathlinkify-code.tsx
More file actions
Ignored revisions
Ignoring revisions in .git-blame-ignore-revs.
55 lines (44 loc) · 2.25 KB
/
Copy pathlinkify-code.tsx
File metadata and controls
55 lines (44 loc) · 2.25 KB
Edit and raw actions
OlderNewer
1
import * as pageDetect from 'github-url-detection';
2
import {$, $$optional} from 'select-dom';
3
4
import features from '../feature-manager.js';
5
import {codeElementsSelector, linkifiedUrlClass, linkifyIssues, linkifyUrls} from '../github-helpers/dom-formatters.js';
6
import {getRepo} from '../github-helpers/index.js';
7
import observe from '../helpers/selector-observer.js';
8
9
function linkifyContent(wrapper: HTMLElement): void {
10
// Mark code block as touched to avoid `shorten-links` from acting on these new links in code
11
wrapper.classList.add(linkifiedUrlClass);
12
13
linkifyUrls(wrapper);
14
15
const currentRepo = pageDetect.isGlobalSearchResults()
16
// Look for the link on the line number
17
? getRepo($('.blob-num a', wrapper.parentElement!).href)
18
: getRepo();
19
// Some non-repo pages like gists have issue references #3844
20
// They make no sense, but we still want `linkifyURLs` to run there
21
if (!currentRepo) {
22
return;
23
}
24
25
// $$optional because the content might not have any comments
26
for (const element of $$optional('.pl-c', wrapper)) {
27
linkifyIssues(currentRepo, element);
28
}
29
}
30
31
function init(signal: AbortSignal): void {
32
observe(codeElementsSelector, linkifyContent, {signal});
33
}
34
35
void features.add(import.meta.url, {
36
include: [
37
pageDetect.hasCode,
38
],
39
init,
40
});
41
42
/*
43
44
## Test URLs
45
46
- URLs/issues in comments: https://github.com/refined-github/sandbox/pull/98
47
- URLs in PR files: https://github.com/refined-github/refined-github/pull/546/files#diff-7296d5f600098588b0af5ec9c84486593be79164f97406a0fc3c4ef5211ab2f9
48
- URLs in regular files: https://github.com/refined-github/refined-github/blob/3f5fc489e417d4f4a14da5ea423775e9ca9246fd/source/features/copy-markdown.js#L45 (broken: #6336)
49
- Issue reference in file: https://github.com/refined-github/refined-github/blob/2ade9a84b1894c7879fab9d3e2d045e876f941a6/source/features/sort-issues-by-update-time.tsx#L18 (broken: #6336)
50
- Code Search: https://github.com/search?q=repo%3AKatsuteDev%2FBackground+marketplace&type=code
51
- Code Search: https://github.com/search?q=%2F%23%5Cd%7B4%2C%7D%2F+language%3Atypescript&type=code
52
- Code Search that might break: https://github.com/search?q=path%3A.npmrc&type=code
53
- Clipped URL that shouldn't be linkified: https://github.com/sindresorhus/linkify-urls/pull/40#pullrequestreview-1593302757
54
55
*/