sindresorhus/refined-github
Do you want to work on this issue?
You can request for a bounty in order to promote it!
Search in current path by default #3612
kidonng posted onGitHub
We can search by file location using path:foo
. It may be helpful to make search scoped to the current path by default, so we don't have to type the path by hand or copy and paste.
URL the feature should appear:
isRepoTree
(https://github.com/sindresorhus/refined-github/tree/master/source)isSingleFile
(https://github.com/sindresorhus/refined-github/blob/master/source/background.ts)
Some thoughts:
- If the user has modified the search, then it will be persisted
- It should not activate on
isRepoRoot
- For files, search in its parent path (e.g.
path:foo
when viewingfoo/bar.ts
)- And according to 2, it should not activate on root level files (e.g.
/foo.ts
)
- And according to 2, it should not activate on root level files (e.g.
Demo
<details> <summary>A rough implementation</summary>
import select from 'select-dom';
import onetime from 'onetime';
import * as pageDetect from 'github-url-detection';
import features from '.';
import {getCleanPathname} from '../github-helpers';
let search = '';
function getSearch(): string {
const path = getCleanPathname().split('/');
if (pageDetect.isRepoTree() && !pageDetect.isRepoRoot()) {
return `path:${path.slice(4).join('/')} `;
}
if (pageDetect.isSingleFile() && path.length !== 5) {
return `path:${path.slice(4, path.length - 1).join('/')} `;
}
return ''
}
function setSearch(): void {
const searchInput = select<HTMLInputElement>('[data-hotkey="s,/"]')!;
if (searchInput.value === search) {
search = getSearch();
searchInput.value = search;
}
}
function init(): void {
setSearch();
document.addEventListener('pjax:end', setSearch);
}
features.add({
id: __filebasename,
description: '',
screenshot: '',
}, {
include: [
pageDetect.isRepoTree,
pageDetect.isSingleFile
],
init: onetime(init)
});
</details>