Skip to content

Commit 177a8f1

Browse files
committed
add custom urls checker
1 parent 479fba3 commit 177a8f1

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getPageIndexingStatus,
88
convertToFilePath,
99
checkSiteUrl,
10+
checkCustomUrls,
1011
} from "./shared/gsc";
1112
import { getSitemapPages } from "./shared/sitemap";
1213
import { Status } from "./shared/types";
@@ -56,6 +57,7 @@ export const index = async (input: string = process.argv[2], options: IndexOptio
5657
}
5758
if (!options.urls) {
5859
options.urls = args["urls"] ? args["urls"].split(",") : undefined;
60+
}
5961
if (!options.quota) {
6062
options.quota = {
6163
rpmRetry: args["rpm-retry"] === "true" || process.env.GIS_QUOTA_RPM_RETRY === "true",
@@ -86,9 +88,12 @@ export const index = async (input: string = process.argv[2], options: IndexOptio
8688
process.exit(1);
8789
}
8890

89-
console.log(`👉 Found ${pages.length} URLs in ${sitemaps.length} sitemap`);
90-
9191
pages = pagesFromSitemaps;
92+
93+
console.log(`👉 Found ${pages.length} URLs in ${sitemaps.length} sitemap`);
94+
} else {
95+
pages = checkCustomUrls(siteUrl, pages);
96+
console.log(`👉 Found ${pages.length} URLs in the provided list`);
9297
}
9398

9499
const statusPerUrl: Record<string, { status: Status; lastCheckedAt: string }> = existsSync(cachePath)

src/shared/gsc.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ export async function checkSiteUrl(accessToken: string, siteUrl: string) {
121121
process.exit(1);
122122
}
123123

124+
/**
125+
* Checks if the given URLs are valid.
126+
* @param siteUrl - The URL of the site.
127+
* @param urls - The URLs to check.
128+
* @returns An array containing the corrected URLs if found, otherwise the original URLs
129+
*/
130+
export function checkCustomUrls(siteUrl: string, urls: string[]) {
131+
const protocol = siteUrl.startsWith("http://") ? "http://" : "https://";
132+
const domain = siteUrl.replace("https://", "").replace("http://", "").replace("sc-domain:", "");
133+
const formattedUrls: string[] = urls.map((url) =>{
134+
url = url.trim();
135+
if (url.startsWith("/")) {
136+
// the url is a relative path (e.g. /about)
137+
return `${protocol}${domain}${url}`;
138+
} else if (url.startsWith("http://") || url.startsWith("https://")) {
139+
// the url is already a full url (e.g. https://domain.com/about)
140+
return url;
141+
} else if (url.startsWith(domain)) {
142+
// the url is a full url without the protocol (e.g. domain.com/about)
143+
return `${protocol}${url}`;
144+
} else {
145+
// the url is a relative path without the leading slash (e.g. about)
146+
return `${protocol}${domain}/${url}`
147+
}
148+
});
149+
150+
return formattedUrls;
151+
}
152+
124153
/**
125154
* Retrieves the indexing status of a page.
126155
* @param accessToken - The access token for authentication.

0 commit comments

Comments
 (0)