summaryrefslogtreecommitdiff
path: root/source/features/edit-readme.tsx
blob: c98cff91e5c972cf5ec3f45a8e07af866d7fa749 (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
import React from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';
import * as icons from '../libs/icons';
import getDefaultBranch from '../libs/get-default-branch';

async function init(): Promise<void | false> {
	const readmeHeader = select('#readme .Box-header h3');
	if (!readmeHeader) {
		return false;
	}

	// The button already exists on repos you can push to.
	if (select.exists('a[aria-label="Edit this file"]')) {
		return false;
	}

	const isPermalink = /Tag|Tree/.test(select('.branch-select-menu i')!.textContent!);

	const filename = readmeHeader.textContent!.trim();
	const pathnameParts = select<HTMLAnchorElement>(`.files [title="${filename}"]`)!.pathname.split('/');
	pathnameParts[3] = 'edit'; // Replaces /blob/
	if (isPermalink) {
		pathnameParts[4] = await getDefaultBranch(); // Replaces /${tag|commit}/
	}

	readmeHeader.after(
		<a href={pathnameParts.join('/')}
			className="Box-btn-octicon btn-octicon float-right"
			aria-label="Edit this file">
			{icons.edit()}
		</a>
	);
}

features.add({
	id: __featureName__,
	description: 'Adds an Edit button on previewed Readmes in folders, even if you have to make a fork.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/62073307-a8378880-b26a-11e9-9e31-be6525d989d2.png',
	include: [
		features.isRepoTree
	],
	load: features.onAjaxedPages,
	init
});