Skip to content
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

Turn into ES Module #48

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 16
node-version-file: package.json
- run: npm install
- run: npx xo

Expand All @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 16
node-version-file: package.json
- run: npm install
- run: npx tsd

Expand All @@ -31,6 +31,6 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 16
node-version-file: package.json
- run: npm install
- run: npx ava
6 changes: 3 additions & 3 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
NPM:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
node-version-file: package.json
registry-url: https://registry.npmjs.org
- run: npm ci || npm install
- uses: fregante/setup-git-user@v2
Expand Down
19 changes: 8 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ Shortens a GitHub URL string.
@example https://github.com/nodejs/node/tree/v0.12/doc becomes nodejs/node@<code>v0.12</code>

*/
declare function shortenRepoUrl(url: string, currentUrl?: string): string;
export default function shortenRepoUrl(url: string, currentUrl?: string): string;

declare namespace shortenRepoUrl {
/**
Shortens a GitHub URL in a DOM anchor.
/**
Shortens a GitHub URL in a DOM anchor if the link label is not customized (i.e. if the `href` matches the `textContent`)

@param anchor An HTMLAnchorElement
@param url The GitHub URL to shorten.
@example https://github.com/nodejs/node/tree/v0.12/doc becomes nodejs/node@<code>v0.12</code>
@param anchor An HTMLAnchorElement
@param url The GitHub URL to shorten.
@example<a href="https://github.com">https://github.com</a> becomes <a href="https://github.com">github.com</a>

*/
const applyToLink: (anchor: HTMLAnchorElement, url?: string) => void;
}
export = shortenRepoUrl;
*/
export function applyToLink(anchor: HTMLAnchorElement, url?: string): void;
23 changes: 11 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const reservedPaths = require('github-reserved-names/reserved-names.json');
import reservedNames from 'github-reserved-names/reserved-names.json' with { type: 'json' };

const patchDiffRegex = /[.](patch|diff)$/;
const releaseRegex = /^releases[/]tag[/]([^/]+)/;
Expand Down Expand Up @@ -43,7 +43,7 @@ function joinValues(array, delimiter = '/') {
return array.filter(Boolean).join(delimiter);
}

function shortenURL(href, currentUrl = 'https://github.com') {
function shortenRepoUrl(href, currentUrl = 'https://github.com') {
if (!href) {
return;
}
Expand Down Expand Up @@ -101,20 +101,20 @@ function shortenURL(href, currentUrl = 'https://github.com') {

const isLocal = origin === currentUrl.origin;
const isThisRepo = (isLocal || isRaw || isRedirection) && currentRepo === `${user}/${repo}`;
const isReserved = reservedPaths.includes(user);
const isReserved = reservedNames.includes(user);
const isDependents = dependentsRegex.test(repoPath);
const isDependencies = dependenciesRegex.test(repoPath);
const [, diffOrPatch] = repoPath.match(patchDiffRegex) || [];
const [, release] = repoPath.match(releaseRegex) || [];
const [, releaseTag, releaseTagExt] = repoPath.match(releaseArchiveRegex) || [];
const [, releaseTag, releaseTagExtension] = repoPath.match(releaseArchiveRegex) || [];
const [, downloadTag, downloadFilename] = repoPath.match(releaseDownloadRegex) || [];
const [, label] = repoPath.match(labelRegex) || [];
const [, compare] = repoPath.match(compareRegex) || [];
const {pull, pullPage, pullPartialStart, pullPartialEnd} = repoPath.match(pullRegex)?.groups ?? {};
const [, issue] = isRedirection ? repoPath.match(issueRegex) || [] : [];
const [, commit] = isRedirection ? repoPath.match(commitRegex) || [] : [];
const [, wiki] = repoPath.match(wikiRegex) || [];
const isFileOrDir = revision && [
const isFileOrDirectory = revision && [
'raw',
'tree',
'blob',
Expand All @@ -139,7 +139,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
return `@${user}${search}${hash}`;
}

if (isFileOrDir) {
if (isFileOrDirectory) {
const revisioned = joinValues(
[joinValues([repoUrl, revision], '@'), filePath],
'/',
Expand All @@ -162,9 +162,9 @@ function shortenURL(href, currentUrl = 'https://github.com') {
return `${partial}${search}${hash} (release)`;
}

if (releaseTagExt) {
if (releaseTagExtension) {
const partial = joinValues([repoUrl, `<code>${releaseTag}</code>`], '@');
return `${partial}${releaseTagExt}${search}${hash}`;
return `${partial}${releaseTagExtension}${search}${hash}`;
}

if (downloadFilename) {
Expand Down Expand Up @@ -241,7 +241,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
return pathname.replaceAll(/^[/]|[/]$/g, '') + url.search + hash + query;
}

function applyToLink(a, currentUrl) {
export function applyToLink(a, currentUrl) {
// Shorten only if the link name hasn't been customized.
// .href automatically adds a / to naked origins so that needs to be tested too
// `trim` makes it compatible with this feature: https://github.com/sindresorhus/refined-github/pull/3085
Expand All @@ -250,13 +250,12 @@ function applyToLink(a, currentUrl) {
(url === a.textContent.trim() || url === `${a.textContent}/`)
&& !a.firstElementChild
) {
const shortened = shortenURL(url, currentUrl);
const shortened = shortenRepoUrl(url, currentUrl);
a.innerHTML = shortened;
return true;
}

return false;
}

module.exports = shortenURL;
module.exports.applyToLink = applyToLink;
export default shortenRepoUrl;
4 changes: 2 additions & 2 deletions test.js → index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('ava');
const shortenUrl = require('.');
import test from 'ava';
import shortenUrl from './index.js';

const currentLocation = 'https://github.com/fregante/shorten-repo-url/issue/1';

Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"funding": "https://github.com/sponsors/fregante",
"license": "MIT",
"author": "Federico Brigante <me@fregante.com> (https://fregante.com)",
"type": "module",
"exports": "./index.js",
"main": "./index.js",
"types": "./index.d.ts",
Expand All @@ -30,6 +31,7 @@
"test": "xo && tsd && ava"
},
"xo": {
"parser": "@typescript-eslint/parser",
"rules": {
"unicorn/better-regex": "off",
"unicorn/prefer-module": "off",
Expand All @@ -40,12 +42,12 @@
"github-reserved-names": "^2.0.5"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^4.0.0",
"ava": "^5.3.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
"@sindresorhus/tsconfig": "^5.0.0",
"ava": "^6.1.3",
"tsd": "^0.31.1",
"xo": "^0.58.0"
},
"engines": {
"node": ">=18"
"node": ">=20.10"
}
}