Skip to content

Commit 913f1fe

Browse files
author
Andrew Luca
authored
Accept a URL parameter (#1)
1 parent 99f0762 commit 913f1fe

File tree

4 files changed

+121
-90
lines changed

4 files changed

+121
-90
lines changed

readme.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,55 @@ const pageDetect = require('github-url-detection');
2424
## Usage
2525

2626
```js
27-
if (pageDetect.isRepo()) {
28-
alert('You’re looking at a repo!')
27+
const href = 'https://github.com/fregante/github-url-detection/issues/1';
28+
if (pageDetect.isIssue(new URL(href))) { // Pass the URL as an `URL` object
29+
alert('The passed URL is of an issue!')
2930
}
3031

32+
if (pageDetect.isRepo()) { // Uses `window.location.href` by default
33+
alert('You’re looking at a repo!')
34+
}
3135

3236
if (pageDetect.isDiscussionList()) {
3337
alert('You’re looking at a issues and PRs list!')
3438
}
3539
```
3640

41+
## API
42+
3743
In the source you can see the [full list of detections](https://www.unpkg.com/browse/github-url-detection@latest/esm/index.d.ts) and [their matching URLs.](https://github.com/fregante/github-url-detection/blob/master/source/index.ts)
3844

39-
Most tests are URL-based but a handful of them are DOM-based.
45+
Most detections are URL-based while others need access to the current `document`. You can determine which ones are URL-based by looking at their signature: URL-based functions have a `url` parameter.
46+
47+
### URL-based detections
48+
49+
By default, URL-based detections use the `location` global if you don't pass a `url` argument.
50+
51+
```js
52+
if (pageDetect.isDiscussionList()) {
53+
alert('You’re looking at a issues or PRs list!')
54+
}
55+
```
56+
57+
```js
58+
if (pageDetect.isDiscussionList(new URL('https://github.com/fregante/github-url-detection/pulls'))) {
59+
alert('You’re looking at a issues or PRs list!')
60+
}
61+
```
62+
63+
Notice that the `url` parameter is not a plain string but it has to be a proper `URL` or `location` object.
64+
65+
### Document-based detections
66+
67+
By default, `document`-based detections use the `document` global, which means they can only be used if you have the whole page, you can't just test any random URL string.
68+
69+
70+
```js
71+
if (pageDetect.isOrganizationProfile()) {
72+
alert('You’re on an organization profile, like https://github.com/babel')
73+
}
74+
```
75+
4076

4177
## License
4278

0 commit comments

Comments
 (0)