* fix: default backlinks search to site-wide scope for root URLs URLs with an explicit protocol but no path (e.g. https://example.com/) were incorrectly auto-selecting "Exact page" scope. Now only URLs with an actual path beyond "/" trigger page scope auto-selection. * test: update backlinks scope tests for root URL behavior change
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import type { BacklinksTargetScope } from "@/types/schemas/backlinks";
|
|
|
|
export function inferBacklinksSearchScopeFromTarget(
|
|
target: string,
|
|
): BacklinksTargetScope {
|
|
const trimmed = target.trim();
|
|
if (!trimmed) {
|
|
return "domain";
|
|
}
|
|
|
|
const hasExplicitProtocol = /^[a-zA-Z][a-zA-Z\d+.-]*:\/\//.test(trimmed);
|
|
|
|
try {
|
|
const parsed = new URL(
|
|
hasExplicitProtocol ? trimmed : `https://${trimmed}`,
|
|
);
|
|
return parsed.pathname !== "/" ? "page" : "domain";
|
|
} catch {
|
|
return "domain";
|
|
}
|
|
}
|
|
|
|
export function resolveBacklinksSearchScope({
|
|
target,
|
|
selectedScope,
|
|
userSelectedScope,
|
|
}: {
|
|
target: string;
|
|
selectedScope: BacklinksTargetScope;
|
|
userSelectedScope: boolean;
|
|
}): BacklinksTargetScope {
|
|
if (userSelectedScope) {
|
|
return selectedScope;
|
|
}
|
|
|
|
return inferBacklinksSearchScopeFromTarget(target);
|
|
}
|
|
|
|
export function getPersistedBacklinksSearchScope(
|
|
target: string,
|
|
scope: BacklinksTargetScope,
|
|
): BacklinksTargetScope | undefined {
|
|
return scope === inferBacklinksSearchScopeFromTarget(target)
|
|
? undefined
|
|
: scope;
|
|
}
|