diff --git a/quartz/components/scripts/randomPage.inline.ts b/quartz/components/scripts/randomPage.inline.ts index 87bc8998..a9b4672e 100644 --- a/quartz/components/scripts/randomPage.inline.ts +++ b/quartz/components/scripts/randomPage.inline.ts @@ -4,6 +4,10 @@ function getRandomInt(max: number) { return Math.floor(Math.random() * max); } +function isValidUrl(newSlug: string, oldSlug: String) { + return oldSlug !== newSlug && !newSlug.includes("/contributing/") +} + async function navigateToRandomPage() { const fullSlug = getFullSlug(window) const data = await fetchData @@ -11,11 +15,8 @@ async function navigateToRandomPage() { // window.location.href = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}` let newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`; - if (newSlug === fullSlug) { - // Generate a new random slug until it's different from the starting fullSlug - do { - newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`; - } while (newSlug === fullSlug); + while (!isValidUrl(newSlug, fullSlug)) { + newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`; } window.location.href = newSlug; }