Skip to content

Instantly share code, notes, and snippets.

@DoctorDerek
Last active June 28, 2021 20:36
Show Gist options
  • Save DoctorDerek/71d9f6ecdaea6657474e51383c890944 to your computer and use it in GitHub Desktop.
Save DoctorDerek/71d9f6ecdaea6657474e51383c890944 to your computer and use it in GitHub Desktop.
Real world use case for .filter(Boolean) by @DoctorDerek https://medium.com/p/87b5112f9f6d
export async function getStaticPaths() {
// search 1 level recursively in @/data/
const dataRegExpMarkdown = /(.+)?\\(.+)?\\(.+)?\.md/
// ["data\\hosting\\packages.md", "data", "hosting", "packages"]
const paths: any[] = getFilesRecursively("data")
.map((path: string) => dataRegExpMarkdown.exec(path))
.filter((item: any[]) => Boolean(item)) // remove falsy
.map((item: any[]) => ({
params: {
path: item[2], // "hosting" (supports 1 directory)
slug: item[3], // "packages" (excluding .md or .mdx)
},
}))
return {
paths: paths,
fallback: false,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment