This repository was archived by the owner on Jun 21, 2023. It is now read-only.
forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadd-readme-buttons.js
More file actions
54 lines (47 loc) · 1.51 KB
/
add-readme-buttons.js
File metadata and controls
54 lines (47 loc) · 1.51 KB
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
import {h} from 'dom-chef';
import select from 'select-dom';
import toSemver from 'to-semver';
import * as icons from '../libs/icons';
import * as pageDetect from '../libs/page-detect';
const repoUrl = pageDetect.getRepoURL();
export default function () {
const readmeContainer = select('.repository-content > #readme');
if (!readmeContainer) {
return;
}
const buttons = <div id="refined-github-readme-buttons"></div>;
/**
* Generate Release button
*/
const tags = select.all('.branch-select-menu [data-tab-filter="tags"] .select-menu-item')
.map(element => [
element.getAttribute('data-name'),
element.getAttribute('href')
]);
const releases = new Map(tags);
const [latestRelease] = toSemver([...releases.keys()], {clean: false});
if (latestRelease) {
buttons.appendChild(
<a
class="tooltipped tooltipped-nw rgh-tooltipped"
href={`${releases.get(latestRelease)}#readme`}
aria-label={`View this file at the latest version (${latestRelease})`}>
{icons.tag()}
</a>
);
}
/**
* Generate Edit button
*/
if (select('.branch-select-menu i').textContent === 'Branch:') {
const readmeName = select('#readme > h3').textContent.trim();
const path = select('.breadcrumb').textContent.trim().split('/').slice(1).join('/');
const currentBranch = select('.branch-select-menu .select-menu-item.selected').textContent.trim();
buttons.appendChild(
<a href={`/${repoUrl}/edit/${currentBranch}/${path}${readmeName}`}>
{icons.edit()}
</a>
);
}
readmeContainer.appendChild(buttons);
}