summaryrefslogtreecommitdiff
path: root/source/features/link-to-github-io.tsx
blob: 82fee725d8cc524b287b4468fcc2605391788846 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'dom-chef';
import onetime from 'onetime';
import {observe} from 'selector-observer';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';
import LinkExternalIcon from 'octicon/link-external.svg';

import features from '.';
import {getRepo} from '../github-helpers';

function initRepoList(): void {
	observe('a[href$=".github.io"][itemprop="name codeRepository"]:not(.rgh-github-io)', {
		constructor: HTMLAnchorElement,
		add(repository) {
			repository.classList.add('rgh-github-io');
			repository.after(
				' ',
				<a
					href={`https://${repository.textContent!.trim()}`}
					target="_blank"
					rel="noopener noreferrer"
				>
					<LinkExternalIcon className="v-align-middle"/>
				</a>
			);
		}
	});
}

async function initRepo(): Promise<void> {
	const repoTitle = await elementReady('[itemprop="name"]');
	repoTitle!.after(
		<a
			className="mr-2"
			href={`https://${repoTitle!.textContent!.trim()}`}
			target="_blank"
			rel="noopener noreferrer"
		>
			<LinkExternalIcon className="v-align-middle"/>
		</a>
	);
}

void features.add(__filebasename, {
	exclude: [
		() => !getRepo()?.name.endsWith('.github.io')
	],
	init: initRepo
}, {
	include: [
		pageDetect.isUserProfileRepoTab,
		pageDetect.isOrganizationProfile
	],
	init: onetime(initRepoList)
});