-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
link-to-changelog-file
feature (#3958)
Co-authored-by: Federico <me@fregante.com>
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from 'dom-chef'; | ||
import cache from 'webext-storage-cache'; | ||
import select from 'select-dom'; | ||
import {BookIcon} from '@primer/octicons-react'; | ||
import elementReady from 'element-ready'; | ||
import * as pageDetect from 'github-url-detection'; | ||
|
||
import features from '.'; | ||
import * as api from '../github-helpers/api'; | ||
import {buildRepoURL, getRepo} from '../github-helpers'; | ||
|
||
const getCacheKey = (): string => `changelog:${getRepo()!.nameWithOwner}`; | ||
|
||
function parseFromDom(): false { | ||
void cache.set(getCacheKey(), select('.js-navigation-item [title^="changelog" i]')?.textContent ?? false); | ||
return false; | ||
} | ||
|
||
const getChangelogName = cache.function(async (): Promise<string | false> => { | ||
const {repository} = await api.v4(` | ||
repository() { | ||
object(expression: "HEAD:") { | ||
...on Tree { | ||
entries { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
|
||
for (const file of repository.object.entries) { | ||
if (file.name.toLowerCase().startsWith('changelog')) { | ||
return file.name; | ||
} | ||
} | ||
|
||
return false; | ||
}, { | ||
cacheKey: getCacheKey | ||
}); | ||
|
||
async function init(): Promise<void | false> { | ||
const changelog = await getChangelogName(); | ||
if (!changelog) { | ||
return false; | ||
} | ||
|
||
(await elementReady('.subnav div', {waitForChildren: false}))!.after( | ||
<a | ||
className="btn ml-3 tooltipped tooltipped-n" | ||
aria-label={`View the ${changelog} file`} | ||
href={buildRepoURL('blob', 'HEAD', changelog)} | ||
style={{padding: '6px 16px'}} | ||
role="button" | ||
> | ||
<BookIcon className="text-blue mr-2"/> | ||
<span>Changelog</span> | ||
</a> | ||
); | ||
} | ||
|
||
void features.add(__filebasename, { | ||
include: [ | ||
pageDetect.isReleasesOrTags | ||
], | ||
awaitDomReady: false, | ||
init | ||
}, { | ||
include: [ | ||
pageDetect.isRepoHome | ||
], | ||
exclude: [ | ||
pageDetect.isEmptyRepoRoot | ||
], | ||
init: parseFromDom | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters