summaryrefslogtreecommitdiff
path: root/source/features/linkify-code.tsx
blob: d2bd8d2bf242e624b9f7dc028a44b968c55dc29d (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
import select from 'select-dom';
import onetime from 'onetime';
import {observe} from 'selector-observer';
import * as pageDetect from 'github-url-detection';

import features from '.';
import {linkifiedURLClass, linkifyURLs, linkifyIssues} from '../github-helpers/dom-formatters';

function init(): void {
	const selectors = [
		'.js-blob-wrapper',
		'.blob-wrapper',
		'.comment-body',
		'.blob-expanded'
	].map(selector => selector + `:not(.${linkifiedURLClass})`).join();

	observe(selectors, {
		add(wrappers) {
			// Linkify full URLs
			// `.blob-code-inner` in diffs
			// `pre` in GitHub comments
			for (const element of select.all('.blob-code-inner, pre', wrappers)) {
				linkifyURLs(element);
			}

			// Linkify issue refs in comments
			for (const element of select.all('span.pl-c', wrappers)) {
				linkifyIssues(element);
			}

			// Mark code block as touched
			wrappers.classList.add(linkifiedURLClass);
		}
	});
}

void features.add({
	id: __filebasename,
	description: 'Linkifies URLs and issue references in code.',
	screenshot: 'https://cloud.githubusercontent.com/assets/170270/25370217/61718820-29b3-11e7-89c5-2959eaf8cac8.png'
}, {
	include: [
		pageDetect.hasCode
	],
	init: onetime(init)
});