refactor: simplified ajaxify.check and likely made it equally more confusing

This commit is contained in:
Julian Lam
2024-09-11 14:43:51 -04:00
parent c2600b124f
commit cf6c4c52b8

View File

@@ -32,10 +32,12 @@ ajaxify.widgets = { render: render };
* null (no action) * null (no action)
*/ */
let urlObj; let urlObj;
let pathname; let pathname = item instanceof Element ? item.getAttribute('href') : undefined;
try { try {
urlObj = new URL(item, `${document.location.origin}${config.relative_path}`); urlObj = new URL(item, `${document.location.origin}${config.relative_path}`);
if (!pathname) {
({ pathname } = urlObj); ({ pathname } = urlObj);
}
} catch (e) { } catch (e) {
return false; return false;
} }
@@ -44,35 +46,39 @@ ajaxify.widgets = { render: render };
// eslint-disable-next-line no-script-url // eslint-disable-next-line no-script-url
const hrefEmpty = href => href === undefined || href === '' || href === 'javascript:;'; const hrefEmpty = href => href === undefined || href === '' || href === 'javascript:;';
if (item instanceof Element && item.getAttribute('data-ajaxify') === 'false') { if (item instanceof Element) {
if (item.getAttribute('data-ajaxify') === 'false') {
if (!internalLink) { if (!internalLink) {
return; return false;
} }
return null; return null;
} }
// Default behaviour for rss feeds
if (internalLink && pathname.endsWith('.rss')) {
return false;
}
// Default behaviour for sitemap
if (internalLink && String(pathname).startsWith(config.relative_path + '/sitemap') && pathname.endsWith('.xml')) {
return false;
}
// Default behaviour for uploads and direct links to API urls
if (internalLink && ['/uploads', '/assets/', '/api/'].some(function (prefix) {
return String(pathname).startsWith(config.relative_path + prefix);
})) {
return false;
}
// eslint-disable-next-line no-script-url // eslint-disable-next-line no-script-url
if (hrefEmpty(urlObj.href) || urlObj.protocol === 'javascript:' || pathname === '#' || pathname === '') { if (hrefEmpty(urlObj.href) || urlObj.protocol === 'javascript:' || pathname === '#' || pathname === '') {
return null; return null;
} }
}
if (internalLink) {
// Default behaviour for rss feeds
if (pathname.endsWith('.rss')) {
return false;
}
// Default behaviour for sitemap
if (String(pathname).startsWith(config.relative_path + '/sitemap') && pathname.endsWith('.xml')) {
return false;
}
// Default behaviour for uploads and direct links to API urls
if (['/uploads', '/assets/', '/api/'].some(function (prefix) {
return String(pathname).startsWith(config.relative_path + prefix);
})) {
return false;
}
}
return true; return true;
}; };