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:

Some thoughts:

  1. If the user has modified the search, then it will be persisted
  2. It should not activate on isRepoRoot
  3. For files, search in its parent path (e.g. path:foo when viewing foo/bar.ts)
    • And according to 2, it should not activate on root level files (e.g. /foo.ts)

Demo

1tqWPWcmQP

<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>


Fund this Issue

$0.00
Funded
Only logged in users can fund an issue

Pull requests