Skip to content
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

feat(addon-doc): add tuiSortPages #5817

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions projects/addon-doc/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ export * from './coerce-boolean';
export * from './coerce-value';
export * from './generate-routes';
export * from './inspect';
export * from './is-page-group';
export * from './parse-code-block';
export * from './raw-load';
export * from './raw-load-record';
export * from './sort-pages';
export * from './to-flat-map-pages';
export * from './transliterate-keyboard-layout';
7 changes: 7 additions & 0 deletions projects/addon-doc/utils/is-page-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {TuiDocPage, TuiDocPageGroup} from '@taiga-ui/addon-doc/interfaces';

export function tuiIsPageGroup(
page: TuiDocPage | TuiDocPageGroup,
): page is TuiDocPageGroup {
return `subPages` in page;
}
41 changes: 41 additions & 0 deletions projects/addon-doc/utils/sort-pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {TuiDocPage, TuiDocPageGroup} from '@taiga-ui/addon-doc/interfaces';

import {tuiIsPageGroup} from './is-page-group';

export function tuiSortPages<T extends TuiDocPage | TuiDocPageGroup>(
pages: readonly T[],
excludeSections = new Set<string>(),
): readonly T[] {
const sections = Array.from(new Set(pages.map(page => page.section)));

const sortedPages = pages.slice().sort((a, b) => {
if (
excludeSections.has(a.section ?? ``) ||
excludeSections.has(b.section ?? ``)
) {
return 0;
}

const aSectionIndex = sections.indexOf(a.section);
const bSectionIndex = sections.indexOf(b.section);

if (aSectionIndex !== bSectionIndex) {
return aSectionIndex - bSectionIndex;
}

if (a.title > b.title) {
return 1;
}

return a.title.localeCompare(b.title);
});

return sortedPages.map(page =>
tuiIsPageGroup(page)
? {
...page,
subPages: tuiSortPages(page.subPages, excludeSections),
}
: page,
);
}
3 changes: 2 additions & 1 deletion projects/demo/src/modules/app/app.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
tuiDocExampleOptionsProvider,
TuiDocSourceCodePathOptions,
} from '@taiga-ui/addon-doc';
import {tuiSortPages} from '@taiga-ui/addon-doc';
import {
TUI_BASE_HREF,
TUI_DIALOG_CLOSES_ON_BACK,
Expand Down Expand Up @@ -124,7 +125,7 @@ export const APP_PROVIDERS: Provider[] = [
},
{
provide: TUI_DOC_PAGES,
useValue: pages,
useValue: tuiSortPages(pages, new Set([`Documentation`, `Foundations`])),
},
{
provide: TUI_DOC_SEE_ALSO,
Expand Down
Loading