fix: normalize trailing slashes for page backlinks (#42)
This commit is contained in:
parent
e6d68493d7
commit
82c2f99ec1
@ -24,6 +24,24 @@ describe("normalizeBacklinksTarget", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("trims trailing slashes from non-root page URLs", () => {
|
||||||
|
expect(
|
||||||
|
normalizeBacklinksTarget("https://github.com/every-app/open-seo/"),
|
||||||
|
).toEqual({
|
||||||
|
apiTarget: "https://github.com/every-app/open-seo",
|
||||||
|
displayTarget: "https://github.com/every-app/open-seo",
|
||||||
|
scope: "page",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps trailing slashes for root page URLs", () => {
|
||||||
|
expect(normalizeBacklinksTarget("https://example.com/")).toEqual({
|
||||||
|
apiTarget: "https://example.com/",
|
||||||
|
displayTarget: "https://example.com/",
|
||||||
|
scope: "page",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("treats bare hostnames as domain lookups", () => {
|
it("treats bare hostnames as domain lookups", () => {
|
||||||
expect(normalizeBacklinksTarget("Example.com")).toEqual({
|
expect(normalizeBacklinksTarget("Example.com")).toEqual({
|
||||||
apiTarget: "example.com",
|
apiTarget: "example.com",
|
||||||
|
|||||||
@ -11,6 +11,17 @@ type NormalizeBacklinksTargetOptions = {
|
|||||||
scope?: BacklinksLookupInput["scope"];
|
scope?: BacklinksLookupInput["scope"];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function normalizePageTargetUrl(url: URL, hostname: string): string {
|
||||||
|
const normalizedUrl = new URL(url.toString());
|
||||||
|
normalizedUrl.hostname = hostname;
|
||||||
|
|
||||||
|
if (normalizedUrl.pathname.length > 1) {
|
||||||
|
normalizedUrl.pathname = normalizedUrl.pathname.replace(/\/+$/, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return normalizedUrl.toString();
|
||||||
|
}
|
||||||
|
|
||||||
export function normalizeBacklinksTarget(
|
export function normalizeBacklinksTarget(
|
||||||
input: string,
|
input: string,
|
||||||
options: NormalizeBacklinksTargetOptions = {},
|
options: NormalizeBacklinksTargetOptions = {},
|
||||||
@ -63,23 +74,26 @@ export function normalizeBacklinksTarget(
|
|||||||
|
|
||||||
if (requestedScope === "page") {
|
if (requestedScope === "page") {
|
||||||
const normalizedUrl = new URL(parsed.toString());
|
const normalizedUrl = new URL(parsed.toString());
|
||||||
normalizedUrl.hostname = exactHostname;
|
|
||||||
if (!hasExplicitProtocol && !hasMeaningfulPath) {
|
if (!hasExplicitProtocol && !hasMeaningfulPath) {
|
||||||
normalizedUrl.pathname = "/";
|
normalizedUrl.pathname = "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const normalizedTarget = normalizePageTargetUrl(
|
||||||
|
normalizedUrl,
|
||||||
|
exactHostname,
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
apiTarget: normalizedUrl.toString(),
|
apiTarget: normalizedTarget,
|
||||||
displayTarget: normalizedUrl.toString(),
|
displayTarget: normalizedTarget,
|
||||||
scope: "page",
|
scope: "page",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasExplicitProtocol || hasMeaningfulPath) {
|
if (hasExplicitProtocol || hasMeaningfulPath) {
|
||||||
const normalizedUrl = new URL(parsed.toString());
|
const normalizedTarget = normalizePageTargetUrl(parsed, exactHostname);
|
||||||
normalizedUrl.hostname = exactHostname;
|
|
||||||
return {
|
return {
|
||||||
apiTarget: normalizedUrl.toString(),
|
apiTarget: normalizedTarget,
|
||||||
displayTarget: normalizedUrl.toString(),
|
displayTarget: normalizedTarget,
|
||||||
scope: "page",
|
scope: "page",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user