Last active
November 12, 2021 19:58
-
-
Save TheOnlyTails/222e5b7dd1dbfae38501e61d66ef9a53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type DocsMap = { | |
name: string; | |
path?: string; | |
type?: "page" | "category"; | |
pages?: DocsMap[]; | |
}; | |
function filterPages(docsStructure: DocsMap[] | DocsMap): DocsMap[] { | |
if (Array.isArray(docsStructure)) { | |
// it's an array of pages/categories | |
return docsStructure | |
.map(page => filterPages(page)) // recursively flatten the structure and filter to only include pages | |
.flat(Infinity) as DocsMap[]; // flatten the structure to get rid of any nesting | |
} else { | |
// it's a single page/category, not a structure | |
if (docsStructure.type === "category") { | |
// it's a category | |
return docsStructure.pages | |
.map(page => filterPages(page)) // filter down and down until only pages are left | |
.flat(Infinity) as DocsMap[]; // flatten the array | |
} else { | |
// it's a page | |
return [docsStructure]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment