Skip to content

Add folder exclusions to randomPage script #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions quartz/components/scripts/randomPage.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ 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
const allPosts = Object.keys(data).map((slug) => simplifySlug(slug as FullSlug))
// 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;
}
Expand Down