mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-09-27 20:09:03 +00:00
linkIsInternal: consider shop.foo.com and blog.foo.com as internal (still without extra dep or SLD list) (PR #1171)
Domain-ish URL test now also covers considering internal URLs with different sub-domains like - `https://listen.tidal.com` - `https://login.tidal.com` Co-authored-by: Daniel Fuchs <dfuchs@multamedio.de> Co-authored-by: Adam Weeden <adamweeden@gmail.com> Co-authored-by: Ronan Jouchet <ronan@jouchet.fr>
This commit is contained in:
parent
d6f0a28a90
commit
067327e863
1
.gitignore
vendored
1
.gitignore
vendored
@ -49,6 +49,7 @@ venv
|
|||||||
# IntelliJ project files
|
# IntelliJ project files
|
||||||
.idea
|
.idea
|
||||||
*.iml
|
*.iml
|
||||||
|
.run
|
||||||
out
|
out
|
||||||
gen
|
gen
|
||||||
|
|
||||||
|
@ -4,8 +4,10 @@ const internalUrl = 'https://medium.com/';
|
|||||||
const internalUrlWww = 'https://www.medium.com/';
|
const internalUrlWww = 'https://www.medium.com/';
|
||||||
const sameBaseDomainUrl = 'https://app.medium.com/';
|
const sameBaseDomainUrl = 'https://app.medium.com/';
|
||||||
const internalUrlCoUk = 'https://medium.co.uk/';
|
const internalUrlCoUk = 'https://medium.co.uk/';
|
||||||
const sameBaseDomainUrlCoUk = 'https://app.medium.co.uk/';
|
|
||||||
const differentBaseDomainUrlCoUk = 'https://other.domain.co.uk/';
|
const differentBaseDomainUrlCoUk = 'https://other.domain.co.uk/';
|
||||||
|
const sameBaseDomainUrlCoUk = 'https://app.medium.co.uk/';
|
||||||
|
const sameBaseDomainUrlTidalListen = 'https://listen.tidal.com/';
|
||||||
|
const sameBaseDomainUrlTidalLogin = 'https://login.tidal.com/';
|
||||||
const internalUrlSubPath = 'topic/technology';
|
const internalUrlSubPath = 'topic/technology';
|
||||||
const externalUrl = 'https://www.wikipedia.org/wiki/Electron';
|
const externalUrl = 'https://www.wikipedia.org/wiki/Electron';
|
||||||
const wildcardRegex = /.*/;
|
const wildcardRegex = /.*/;
|
||||||
@ -48,6 +50,16 @@ test('urls on the same "base domain" should be considered internal, even with a
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('urls on the same "base domain" should be considered internal, even with different sub domains', () => {
|
||||||
|
expect(
|
||||||
|
linkIsInternal(
|
||||||
|
sameBaseDomainUrlTidalListen,
|
||||||
|
sameBaseDomainUrlTidalLogin,
|
||||||
|
undefined,
|
||||||
|
),
|
||||||
|
).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
test('urls on the same "base domain" should be considered internal, long SLD', () => {
|
test('urls on the same "base domain" should be considered internal, long SLD', () => {
|
||||||
expect(
|
expect(
|
||||||
linkIsInternal(internalUrlCoUk, sameBaseDomainUrlCoUk, undefined),
|
linkIsInternal(internalUrlCoUk, sameBaseDomainUrlCoUk, undefined),
|
||||||
|
@ -52,7 +52,9 @@ export function linkIsInternal(
|
|||||||
|
|
||||||
if (internalUrlRegex) {
|
if (internalUrlRegex) {
|
||||||
const regex = RegExp(internalUrlRegex);
|
const regex = RegExp(internalUrlRegex);
|
||||||
return regex.test(newUrl);
|
if (regex.test(newUrl)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -60,13 +62,10 @@ export function linkIsInternal(
|
|||||||
// 1. app.foo.com and foo.com
|
// 1. app.foo.com and foo.com
|
||||||
// 2. www.foo.com and foo.com
|
// 2. www.foo.com and foo.com
|
||||||
// 3. www.foo.com and app.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./, '');
|
// Only use the tld and the main domain for domain-ish test
|
||||||
const [longerDomain, shorterDomain] =
|
// Enables domain-ish equality for blog.foo.com and shop.foo.com
|
||||||
currentDomain.length > newDomain.length
|
return domainify(currentUrl) === domainify(newUrl);
|
||||||
? [currentDomain, newDomain]
|
|
||||||
: [newDomain, currentDomain];
|
|
||||||
return longerDomain.endsWith(shorterDomain);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error(
|
log.error(
|
||||||
'Failed to parse domains as determining if link is internal. From:',
|
'Failed to parse domains as determining if link is internal. From:',
|
||||||
@ -79,6 +78,27 @@ export function linkIsInternal(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper to determine domain-ish equality for many cases, the trivial ones
|
||||||
|
* and the trickier ones, e.g. `blog.foo.com` and `shop.foo.com`,
|
||||||
|
* in a way that is "good enough", and doesn't need a list of SLDs.
|
||||||
|
* See chat at https://github.com/nativefier/nativefier/pull/1171#pullrequestreview-649132523
|
||||||
|
*/
|
||||||
|
function domainify(url: string): string {
|
||||||
|
// So here's what we're doing here:
|
||||||
|
// Get the hostname from the url
|
||||||
|
const hostname = new URL(url).hostname;
|
||||||
|
// Drop the first section if the domain
|
||||||
|
const domain = hostname.split('.').slice(1).join('.');
|
||||||
|
// Check the length, if it's too short, the hostname was probably the domain
|
||||||
|
// Or if the domain doesn't have a . in it we went too far
|
||||||
|
if (domain.length < 6 || domain.split('.').length === 0) {
|
||||||
|
return hostname;
|
||||||
|
}
|
||||||
|
// This SHOULD be the domain, but nothing is 100% guaranteed
|
||||||
|
return domain;
|
||||||
|
}
|
||||||
|
|
||||||
export function shouldInjectCss(): boolean {
|
export function shouldInjectCss(): boolean {
|
||||||
try {
|
try {
|
||||||
return fs.existsSync(INJECT_DIR);
|
return fs.existsSync(INJECT_DIR);
|
||||||
|
Loading…
Reference in New Issue
Block a user