Skip to content

feat(dgeni): hardcode design nav list sort order #3133

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
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
20 changes: 13 additions & 7 deletions tools/dgeni/src/transforms/daffodil-guides-package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { IdSanitizer } from '../../services/id-sanitizer';
import { outputPathsConfigurator } from '../../utils/configurator/output';
import { pathsConfigurator } from '../../utils/configurator/path';
import { generateNavigationTrieFromDocuments } from '../../utils/navigation-trie';
import { sortTrie } from '../../utils/trie-sort';
import {
API_SOURCE_PATH,
DESIGN_PATH,
Expand Down Expand Up @@ -139,15 +140,20 @@ export const designDocsPackage = new Package('design-docs', [design])
];
})
.config((generateNavList: GenerateNavListProcessor) => {
generateNavList.transform = (docs) => generateNavigationTrieFromDocuments([
generateNavList.transform = (docs) => sortTrie(
generateNavigationTrieFromDocuments([
{
id: 'components',
title: 'Components',
path: `/${DAFF_DOCS_PATH}/${DAFF_DOCS_DESIGN_PATH}/${DAFF_DOC_KIND_PATH_SEGMENT_MAP[DaffDocKind.COMPONENT]}`,
tableOfContents: '',
},
...docs.map(transformDesignGuideDoc),
]),
{
id: 'components',
title: 'Components',
path: `/${DAFF_DOCS_PATH}/${DAFF_DOCS_DESIGN_PATH}/${DAFF_DOC_KIND_PATH_SEGMENT_MAP[DaffDocKind.COMPONENT]}`,
tableOfContents: '',
'': ['overview', 'whats-new', 'getting-started', 'foundations', 'components'],
},
...docs.map(transformDesignGuideDoc),
]);
);
});

export const designExplanationsPackage = pathsConfigurator({
Expand Down
33 changes: 33 additions & 0 deletions tools/dgeni/src/utils/trie-sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export interface Trie {
id: string;
children: Array<Trie>;
}

/**
* Defines the order of a trie's immediate children.
*/
export type TrieSortData = Record<Trie['id'], Array<Trie['id']>>;

/**
* Sorts a trie top to bottom according to the passed sort data.
* If the sort data does not contain data for a trie child, it is sorted after all children with sort data.
*/
export const sortTrie = <T extends Trie>(trie: T, sortData: TrieSortData): T => {
if (sortData[trie.id]) {
trie.children = trie.children.sort((a, b) => {
const aIndex = sortData[trie.id].indexOf(a.id);
const bIndex = sortData[trie.id].indexOf(b.id);

return aIndex === bIndex
? 0
: aIndex < 0
? 1
: bIndex < 0
? -1
: aIndex - bIndex;
});
}
trie.children = trie.children.map((child) => sortTrie(child, sortData));

return trie;
};
Loading