Skip to content

Commit 280f397

Browse files
Add folder exclusions to randomPage script (#75)
* Fix: exclude `/contributing/` from randomPage 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)
1 parent f5810a2 commit 280f397

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

quartz/components/scripts/randomPage.inline.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ function getRandomInt(max: number) {
44
return Math.floor(Math.random() * max);
55
}
66

7+
function isValidUrl(newSlug: string, oldSlug: String) {
8+
return oldSlug !== newSlug && !newSlug.includes("/contributing/")
9+
}
10+
711
async function navigateToRandomPage() {
812
const fullSlug = getFullSlug(window)
913
const data = await fetchData
1014
const allPosts = Object.keys(data).map((slug) => simplifySlug(slug as FullSlug))
1115
// window.location.href = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`
1216
let newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`;
1317

14-
if (newSlug === fullSlug) {
15-
// Generate a new random slug until it's different from the starting fullSlug
16-
do {
17-
newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`;
18-
} while (newSlug === fullSlug);
18+
while (!isValidUrl(newSlug, fullSlug)) {
19+
newSlug = `${pathToRoot(fullSlug)}/${allPosts[getRandomInt(allPosts.length - 1)]}`;
1920
}
2021
window.location.href = newSlug;
2122
}

0 commit comments

Comments
 (0)