From 78f277082a0de88dc91293fe41eeaff1819db26b Mon Sep 17 00:00:00 2001 From: MasssiveJuice Date: Wed, 7 Aug 2024 19:44:57 +1200 Subject: [PATCH 1/2] Fix: exclude `/contributing/` from randomPage --- quartz/components/scripts/randomPage.inline.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/quartz/components/scripts/randomPage.inline.ts b/quartz/components/scripts/randomPage.inline.ts index 87bc8998..3b5c6dee 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 @@ -15,7 +19,7 @@ async function navigateToRandomPage() { // 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)); } window.location.href = newSlug; } From fda37355337a85c63902af83b3136b4f3160e7c4 Mon Sep 17 00:00:00 2001 From: MasssiveJuice Date: Thu, 8 Aug 2024 05:16:46 +1200 Subject: [PATCH 2/2] Fix: actually fix random page exclusions Credit to Descawed on MMC Discord for the fix: - [1](https://discord.com/channels/210394599246659585/1243854227605028866/1270656132318232639) - [2](https://discord.com/channels/210394599246659585/1243854227605028866/1270697825201557505) --- quartz/components/scripts/randomPage.inline.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/quartz/components/scripts/randomPage.inline.ts b/quartz/components/scripts/randomPage.inline.ts index 3b5c6dee..a9b4672e 100644 --- a/quartz/components/scripts/randomPage.inline.ts +++ b/quartz/components/scripts/randomPage.inline.ts @@ -5,7 +5,7 @@ function getRandomInt(max: number) { } function isValidUrl(newSlug: string, oldSlug: String) { - return oldSlug === newSlug || newSlug.includes("/contributing/") + return oldSlug !== newSlug && !newSlug.includes("/contributing/") } async function navigateToRandomPage() { @@ -15,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 (isValidUrl(newSlug, fullSlug)); + while (!isValidUrl(newSlug, fullSlug)) { + newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`; } window.location.href = newSlug; }