diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1d89c82..ea5a75d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -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 @@ -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 diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 8440108..acd82cf 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index f450d50..919be71 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,17 +7,14 @@ Shortens a GitHub URL string. @example https://github.com/nodejs/node/tree/v0.12/doc becomes nodejs/node@v0.12 */ -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@v0.12 +@param anchor An HTMLAnchorElement +@param url The GitHub URL to shorten. +@examplehttps://github.com becomes github.com - */ - const applyToLink: (anchor: HTMLAnchorElement, url?: string) => void; -} -export = shortenRepoUrl; +*/ +export function applyToLink(anchor: HTMLAnchorElement, url?: string): void; diff --git a/index.js b/index.js index 649d5fd..6b0e15e 100644 --- a/index.js +++ b/index.js @@ -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[/]([^/]+)/; @@ -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; } @@ -101,12 +101,12 @@ 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) || []; @@ -114,7 +114,7 @@ function shortenURL(href, currentUrl = 'https://github.com') { 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', @@ -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], '/', @@ -162,9 +162,9 @@ function shortenURL(href, currentUrl = 'https://github.com') { return `${partial}${search}${hash} (release)`; } - if (releaseTagExt) { + if (releaseTagExtension) { const partial = joinValues([repoUrl, `${releaseTag}`], '@'); - return `${partial}${releaseTagExt}${search}${hash}`; + return `${partial}${releaseTagExtension}${search}${hash}`; } if (downloadFilename) { @@ -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 @@ -250,7 +250,7 @@ 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; } @@ -258,5 +258,4 @@ function applyToLink(a, currentUrl) { return false; } -module.exports = shortenURL; -module.exports.applyToLink = applyToLink; +export default shortenRepoUrl; diff --git a/test.js b/index.test.js similarity index 99% rename from test.js rename to index.test.js index 18cc4b5..16742ce 100644 --- a/test.js +++ b/index.test.js @@ -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'; diff --git a/package.json b/package.json index 8a35596..d653874 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "funding": "https://github.com/sponsors/fregante", "license": "MIT", "author": "Federico Brigante (https://fregante.com)", + "type": "module", "exports": "./index.js", "main": "./index.js", "types": "./index.d.ts", @@ -30,6 +31,7 @@ "test": "xo && tsd && ava" }, "xo": { + "parser": "@typescript-eslint/parser", "rules": { "unicorn/better-regex": "off", "unicorn/prefer-module": "off", @@ -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" } }