Skip to content

Instantly share code, notes, and snippets.

@TheOnlyTails
Last active November 12, 2021 19:58
Show Gist options
  • Save TheOnlyTails/222e5b7dd1dbfae38501e61d66ef9a53 to your computer and use it in GitHub Desktop.
Save TheOnlyTails/222e5b7dd1dbfae38501e61d66ef9a53 to your computer and use it in GitHub Desktop.
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