Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/refined-github/shorten-repo-url
Shorten GitHub links like GitHub shortens Issues and Commit links.
https://github.com/refined-github/shorten-repo-url
Last synced: about 2 months ago
JSON representation
Shorten GitHub links like GitHub shortens Issues and Commit links.
- Host: GitHub
- URL: https://github.com/refined-github/shorten-repo-url
- Owner: refined-github
- License: mit
- Created: 2017-06-24T07:38:25.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2024-07-30T20:16:51.000Z (about 2 months ago)
- Last Synced: 2024-07-31T00:36:08.052Z (about 2 months ago)
- Language: JavaScript
- Homepage: https://npm.im/shorten-repo-url
- Size: 79.1 KB
- Stars: 79
- Watchers: 8
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
Awesome Lists containing this project
README
# shorten-repo-url
> Shorten GitHub links like GitHub shortens Issues and Commit links. Used on [refined-github](https://github.com/refined-github/refined-github)
![Demo](https://user-images.githubusercontent.com/1402241/27252232-8fdf8ed0-538b-11e7-8f19-12d317c9cd32.png)
Look at [the tests](https://github.com/refined-github/shorten-repo-url/blob/main/index.test.js) to see what each URL is shortened to. GitLab URLs are mostly compatible but they're not officially supported.
It works on any domain, so GitHub Enterprise is also supported.
## Install
```
$ npm install shorten-repo-url
```## Usage
```js
const shortenRepoUrl = require('shorten-repo-url');const HTML = shortenRepoUrl(
'https://github.com/nodejs/node/tree/v0.12/doc',
'https://github.com/nodejs/node' // same repo
);
//=> 'v0.12
' // repo-less URLconst HTML = shortenRepoUrl(
'https://github.com/nodejs/node/tree/v0.12/doc',
'https://github.com' // not the same repo
);
//=> 'nodejs/node@v0.12
' // URL with repo
```## API
### shortenRepoUrl(url, currentUrl)
Returns the shortened URL in HTML as a `string` like `nodejs/node@
v0.12
`.#### url
Type: `string`
The GitHub URL to shorten.
#### currentUrl
Type: `string`, like `location.href`
The URL of the current page, to build relative URLs like `
v0.12
` instead of the longer `nodejs/node@v0.12
`### shortenRepoUrl.applyToLink(link, currentUrl)
Automatically shorten the link's text if the text matches the URL, i.e. `https://github.com`. If a `data-original-href` attribute is present, it will be used when comparing the link’s text and when generating the shortened URL.
Note: this function will never change the `href` of the link, it only changes the text.
It will return `true` or `false` depending on whether the link was shortened.
#### link
Type: `Element`
Example: `shortenRepoUrl.applyToLink(document.querySelector(a))`
#### currentUrl
Type: `string`, like `location.href`
Same as before.
## License
MIT © [Federico Brigante](https://fregante.com)