-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Shorten links #473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Shorten links #473
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
fbb6e03
Shorten URLs to files
fregante 2bb9241
Switch to for of loop
fregante 536beaf
Move from string+regex parsing to explicit piece-by-piece handling
fregante a0242ae
Parse first, shorten in one shot if it matches a pattern
fregante a3c9003
Extract into its own file
fregante 5d1863b
Cleanup
fregante 060b5a4
Leave / at the beginning of file repo-less paths
fregante 9d1ef3f
Correct isShortened test target
fregante 591b84e
Shorten commit.diff and commit.patch
fregante 12040ac
Wrap revision in <code> once
fregante f987e52
Cleanup
fregante 71ea9a7
Fix file links inconsistencies
fregante afeac8d
More explicitly drop leading+trailing slash
fregante cc2426e
Shorten releases
fregante cfec987
Shorten labels
fregante 1154c7d
Shorten release archive links
fregante 0a9139a
Shorten release downloads
fregante 24cf74a
Shorten users
fregante 5524500
Add tests
fregante d1a99c9
Wrap test's URL list
fregante effca1f
Mention feature in the readme
fregante File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import select from 'select-dom'; | ||
| import {getRepoURL} from './page-detect'; | ||
|
|
||
| const patchDiffRegex = /[.](patch|diff)$/; | ||
| const releaseRegex = /releases[/]tag[/]([^/]+)/; | ||
| const labelRegex = /labels[/]([^/]+)/; | ||
| const releaseArchiveRegex = /archive[/](.+)([.]zip|[.]tar[.]gz)/; | ||
| const releaseDownloadRegex = /releases[/]download[/]([^/]+)[/](.+)/; | ||
|
|
||
| const reservedPaths = [ | ||
| 'join', | ||
| 'site', | ||
| 'blog', | ||
| 'about', | ||
| 'login', | ||
| 'pulls', | ||
| 'search', | ||
| 'issues', | ||
| 'explore', | ||
| 'contact', | ||
| 'pricing', | ||
| 'trending', | ||
| 'settings', | ||
| 'features', | ||
| 'business', | ||
| 'personal', | ||
| 'security', | ||
| 'dashboard', | ||
| 'showcases', | ||
| 'open-source', | ||
| 'marketplace' | ||
| ]; | ||
|
|
||
| const styleRevision = revision => { | ||
| if (!revision) { | ||
| return; | ||
| } | ||
| revision = revision.replace(patchDiffRegex, ''); | ||
| if (/^[0-9a-f]{40}$/.test(revision)) { | ||
| revision = revision.substr(0, 7); | ||
| } | ||
| return `<code>${revision}</code>`; | ||
| }; | ||
|
|
||
| // Filter out null values | ||
| const joinValues = (array, delimiter = '/') => { | ||
| return array.filter(s => s).join(delimiter); | ||
| }; | ||
|
|
||
| export function shortenUrl(href) { | ||
| /** | ||
| * Parse URL | ||
| */ | ||
| const { | ||
| origin, | ||
| pathname, | ||
| search, | ||
| hash | ||
| } = new URL(href); | ||
|
|
||
| const isRaw = [ | ||
| 'https://raw.githubusercontent.com', | ||
| 'https://cdn.rawgit.com', | ||
| 'https://rawgit.com' | ||
| ].includes(origin); | ||
|
|
||
| let [ | ||
| user, | ||
| repo, | ||
| type, | ||
| revision, | ||
| ...filePath | ||
| ] = pathname.substr(1).split('/'); | ||
|
|
||
| if (isRaw) { | ||
| [ | ||
| user, | ||
| repo, | ||
| // Raw URLs don't have `blob` here | ||
| revision, | ||
| ...filePath | ||
| ] = pathname.substr(1).split('/'); | ||
| type = 'raw'; | ||
| } | ||
|
|
||
| revision = styleRevision(revision); | ||
| filePath = filePath.join('/'); | ||
|
|
||
| const isLocal = origin === location.origin; | ||
| const isThisRepo = (isLocal || isRaw) && getRepoURL() === `${user}/${repo}`; | ||
| const isReserved = reservedPaths.includes(user); | ||
| const [, diffOrPatch] = pathname.match(patchDiffRegex) || []; | ||
| const [, release] = pathname.match(releaseRegex) || []; | ||
| const [, releaseTag, releaseTagExt] = pathname.match(releaseArchiveRegex) || []; | ||
| const [, downloadTag, downloadFilename] = pathname.match(releaseDownloadRegex) || []; | ||
| const [, label] = pathname.match(labelRegex) || []; | ||
| const isFileOrDir = revision && [ | ||
| 'raw', | ||
| 'tree', | ||
| 'blob', | ||
| 'blame', | ||
| 'commits' | ||
| ].includes(type); | ||
|
|
||
| const repoUrl = isThisRepo ? '' : `${user}/${repo}`; | ||
|
|
||
| /** | ||
| * Shorten URL | ||
| */ | ||
|
|
||
| if (isReserved || (!isLocal && !isRaw)) { | ||
| return href | ||
| .replace(/^https:[/][/]/, '') | ||
| .replace(/^www[.]/, '') | ||
| .replace(/[/]$/, ''); | ||
| } | ||
|
|
||
| if (user && !repo) { | ||
| return `@${user}${search}${hash}`; | ||
| } | ||
|
|
||
| if (isFileOrDir) { | ||
| const file = `${repoUrl}${filePath ? '/' + filePath : ''}`; | ||
| const revisioned = joinValues([file, revision], '@'); | ||
| const partial = `${revisioned}${search}${hash}`; | ||
| if (type !== 'blob' && type !== 'tree') { | ||
| return `${partial} (${type})`; | ||
| } | ||
| return partial; | ||
| } | ||
|
|
||
| if (diffOrPatch) { | ||
| const partial = joinValues([repoUrl, revision], '@'); | ||
| return `${partial}.${diffOrPatch}${search}${hash}`; | ||
| } | ||
|
|
||
| if (release) { | ||
| const partial = joinValues([repoUrl, `<code>${release}</code>`], '@'); | ||
| return `${partial}${search}${hash} (release)`; | ||
| } | ||
|
|
||
| if (releaseTagExt) { | ||
| const partial = joinValues([repoUrl, `<code>${releaseTag}</code>`], '@'); | ||
| return `${partial}${releaseTagExt}${search}${hash}`; | ||
| } | ||
|
|
||
| if (downloadFilename) { | ||
| const partial = joinValues([repoUrl, `<code>${downloadTag}</code>`], '@'); | ||
| return `${partial} ${downloadFilename}${search}${hash} (download)`; | ||
| } | ||
|
|
||
| if (label) { | ||
| return joinValues([repoUrl, label]) + `${search}${hash} (label)`; | ||
| } | ||
|
|
||
| // Drop leading and trailing slash of relative path | ||
| return `${pathname.replace(/^[/]|[/]$/g, '')}${search}${hash}`; | ||
| } | ||
|
|
||
| export default () => { | ||
| for (const a of select.all('a[href]')) { | ||
| // Don't change if it was already customized | ||
| // .href automatically adds a / to naked origins | ||
| // so that needs to be tested too | ||
| if (a.href !== a.textContent && a.href !== `${a.textContent}/`) { | ||
| continue; | ||
| } | ||
|
|
||
| const shortened = shortenUrl(a.href); | ||
|
|
||
| // Don't touch the dom if there's nothing to change | ||
| if (shortened === a.textContent) { | ||
| continue; | ||
| } | ||
|
|
||
| a.innerHTML = shortened; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,7 @@ | ||
| const url = require('url'); | ||
| const URL = require('url').URL; | ||
|
|
||
| function WindowMock(initialURI) { | ||
| this._currentURI = initialURI; | ||
| function WindowMock(initialURI = 'https://github.com') { | ||
| this.location = new URL(initialURI); | ||
| } | ||
|
|
||
| WindowMock.prototype.location = { | ||
| set href(uri) { | ||
| const uriParts = url.parse(uri); | ||
| this.hostname = uriParts.hostname; | ||
| this.pathname = uriParts.pathname; | ||
| this._currentURI = uri; | ||
| }, | ||
|
|
||
| get href() { | ||
| return this._currentURI; | ||
| } | ||
| }; | ||
|
|
||
| module.exports = WindowMock; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sindresorhus please verify. This is Node 7+
https://nodejs.org/api/url.html#url_the_whatwg_url_api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍