Skip to content

Commit

Permalink
Merge pull request #26 from kidonng/page-detect
Browse files Browse the repository at this point in the history
  • Loading branch information
dderevjanik authored Oct 15, 2020
2 parents b71f560 + e6a557d commit 01e4405
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 61 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"homepage": "https://github.com/dderevjanik/github-vscode-icons#readme",
"dependencies": {
"fastdom": "^1.0.8",
"github-url-detection": "^2.0.5",
"react": "^16.4.0",
"react-dom": "^16.13.1",
"select-dom": "^4.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/content/pages/GistGitHub.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isGist } from '../utils/PageDetect';
import { isGist } from 'github-url-detection';
import { getIconForFile, getIconUrl } from '../utils/Icons';
import { mutate } from 'fastdom';

Expand Down
3 changes: 2 additions & 1 deletion packages/content/pages/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
DEFAULT_ROOT,
DEFAULT_FILE
} from '../utils/Icons';
import { isRepoRoot, isHistoryForFile, isRepoTree, isSingleFile, isCommit, isGist } from '../utils/PageDetect';
import { isCommit, isRepoRoot, isSingleFile, isRepoTree } from 'github-url-detection';
import { isHistoryForFile } from '../utils/PageDetect';
import { mutate } from 'fastdom';
import { getFileIcon, getFolderIcon } from '../utils/Dev';

Expand Down
61 changes: 2 additions & 59 deletions packages/content/utils/PageDetect.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,7 @@
// COPIED FROM https://github.com/sindresorhus/refined-github/blob/master/src/libs/page-detect.js

import * as select from 'select-dom';
import { isRepo, utils } from 'github-url-detection';

export const isTrending = () => location.pathname.startsWith('/trending');

export const getRepoPath = () => location.pathname.replace(/^\/[^/]+\/[^/]+/, '');

export const isRepoTree = () => isRepo() && /\/tree\//.test(getRepoPath());

export const isPR = () => isRepo() && /^\/pull\/\d+/.test(getRepoPath());

export const isPRFiles = () => isRepo() && /^\/pull\/\d+\/files/.test(getRepoPath());

export const isPRCommit = () => isRepo() && /^\/pull\/\d+\/commits\/[0-9a-f]{5,40}/.test(getRepoPath());

export const isSingleCommit = () => isRepo() && /^\/commit\/[0-9a-f]{5,40}/.test(getRepoPath());

export const isCompare = () => isRepo() && /^\/compare/.test(getRepoPath());

export const hasDiff = () =>
isRepo() &&
(isSingleCommit() || isPRCommit() || isPRFiles() || isCompare() || (isPR() && select.exists('.diff-table')));

export const isReleases = () => isRepo() && /^\/(releases|tags)/.test(getRepoPath());

export const isNotifications = () => location.pathname.startsWith('/notifications');

export const isRepoSettings = () => isRepo() && /^\/settings/.test(getRepoPath());

export const getOwnerAndRepo = () => {
const [, ownerName, repoName] = location.pathname.split('/');

return {
ownerName,
repoName
};
};

export const hasCommentForm = () => select.exists('.js-previewable-comment-form');

/**
* Github related detections
*/
// @todo Replace with DOM-based test because this is too generic #708
export const isRepo = () => !isGist() && !isTrending() && /^\/[^/]+\/[^/]+/.test(location.pathname);

export const isSingleFile = () => {
const { ownerName, repoName } = getOwnerAndRepo();
const blobPattern = new RegExp(`/${ownerName}/${repoName}/blob/`);
return isRepo() && blobPattern.test(location.href);
};

export const isCommit = () => isSingleCommit() || isPRCommit() || (isPRFiles() && select.exists('.full-commit'));

export const isRepoRoot = () =>
isRepo() && /^(\/?|\/tree\/?)$/.test(getRepoPath()) && select.exists('.repository-content');

export const isHistoryForFile = () => isRepo() && /^\/commits\/[0-9a-f]{5,40}\/.+/.test(getRepoPath());

export const isGist = () => location.hostname.startsWith('gist.') || location.pathname.startsWith('gist/');
export const isHistoryForFile = () => isRepo() && /^\/commits\/[0-9a-f]{5,40}\/.+/.test(utils.getRepoPath());

/**
* BitBucket related detections
Expand Down

0 comments on commit 01e4405

Please sign in to comment.