mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-11-29 21:06:56 +08:00
Add isOnSite() to check a link's protocol and host against the route's domains, and skip the detail request for items that link elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
26 lines
906 B
TypeScript
26 lines
906 B
TypeScript
/**
|
|
* Check if a sub-domain is valid
|
|
* @param {String} hostname sub-domain
|
|
* @returns {Boolean} true if valid
|
|
*/
|
|
const isValidHost = (hostname?: string) => {
|
|
const regex = /^[\dA-Z](?:[\dA-Z-]{0,61}[\dA-Z])?$/i;
|
|
return regex.test(hostname ?? '');
|
|
};
|
|
|
|
/**
|
|
* Whether a link is an http or https URL on one of the given sites, or on a subdomain of one. Check this before a route
|
|
* requests or opens a link taken from an upstream page or API response.
|
|
* @param link absolute URL
|
|
* @param domains host names such as `example.com`
|
|
*/
|
|
const isOnSite = (link: string | undefined, ...domains: string[]) => {
|
|
if (!link || !URL.canParse(link)) {
|
|
return false;
|
|
}
|
|
const { protocol, hostname } = new URL(link);
|
|
return ['http:', 'https:'].includes(protocol) && domains.some((domain) => hostname === domain || hostname.endsWith(`.${domain}`));
|
|
};
|
|
|
|
export { isOnSite, isValidHost };
|