diff --git a/app/src/helpers/helpers.test.ts b/app/src/helpers/helpers.test.ts index c7792d9..3b86ec7 100644 --- a/app/src/helpers/helpers.test.ts +++ b/app/src/helpers/helpers.test.ts @@ -1,6 +1,10 @@ import { linkIsInternal, getCounterValue } from './helpers'; const internalUrl = 'https://medium.com/'; +const internalUrlWww = 'https://www.medium.com/'; +const sameBaseDomainUrl = 'https://app.medium.com/'; +const internalUrlCoUk = 'https://medium.co.uk/'; +const sameBaseDomainUrlCoUk = 'https://app.medium.co.uk/'; const internalUrlSubPath = 'topic/technology'; const externalUrl = 'https://www.wikipedia.org/wiki/Electron'; const wildcardRegex = /.*/; @@ -27,6 +31,28 @@ test('all urls should be internal with wildcard regex', () => { expect(linkIsInternal(internalUrl, externalUrl, wildcardRegex)).toEqual(true); }); +test('a "www." of a domain should be considered internal', () => { + expect(linkIsInternal(internalUrl, internalUrlWww, undefined)).toEqual(true); +}); + +test('urls on the same "base domain" should be considered internal', () => { + expect(linkIsInternal(internalUrl, sameBaseDomainUrl, undefined)).toEqual( + true, + ); +}); + +test('urls on the same "base domain" should be considered internal, even with a www', () => { + expect(linkIsInternal(internalUrlWww, sameBaseDomainUrl, undefined)).toEqual( + true, + ); +}); + +test('urls on the same "base domain" should be considered internal, long SLD', () => { + expect( + linkIsInternal(internalUrlCoUk, sameBaseDomainUrlCoUk, undefined), + ).toEqual(true); +}); + const smallCounterTitle = 'Inbox (11) - nobody@example.com - Gmail'; const largeCounterTitle = 'Inbox (8,756) - nobody@example.com - Gmail'; const noCounterTitle = 'Inbox - nobody@example.com - Gmail'; diff --git a/app/src/helpers/helpers.ts b/app/src/helpers/helpers.ts index 3eaa233..4658f99 100644 --- a/app/src/helpers/helpers.ts +++ b/app/src/helpers/helpers.ts @@ -33,9 +33,17 @@ export function linkIsInternal( } try { - const currentDomain = new URL(currentUrl).hostname; - const newDomain = new URL(newUrl).hostname; - return currentDomain === newDomain; + // Consider as "same domain-ish", without TLD/SLD list: + // 1. app.foo.com and foo.com + // 2. www.foo.com and foo.com + // 3. www.foo.com and app.foo.com + const currentDomain = new URL(currentUrl).hostname.replace(/^www\./, ''); + const newDomain = new URL(newUrl).hostname.replace(/^www./, ''); + const [longerDomain, shorterDomain] = + currentDomain.length > newDomain.length + ? [currentDomain, newDomain] + : [newDomain, currentDomain]; + return longerDomain.endsWith(shorterDomain); } catch (err) { console.warn( 'Failed to parse domains as determining if link is internal. From:', diff --git a/docs/api.md b/docs/api.md index 18a7144..a1449e0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -400,7 +400,13 @@ Forces the packaged app to ignore web security errors, such as [Mixed Content](h --internal-urls ``` -Regular expression of URLs to consider "internal"; all other URLs will be opened in an external browser. Defaults to URLs on same second-level domain as app. +Regular expression of URLs to consider "internal" while following a hyperlink. +Internal URLs will open in Nativefier, other URLs will open in your preferred browser. + +Defaults to view as "internal" two URLs that share the same base domain, +once stripped of `www.`. For example, by default, +- URLs from/to `foo.com`, `app.foo.com`, `www.foo.com` are considered internal. +- URLs from/to `abc.com` and `xyz.com` are considered external. Example: