Skip to content

Commit e893cb0

Browse files
authored
feat(dgeni): hardcode design nav list sort order (#3133)
1 parent d5cad9a commit e893cb0

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

tools/dgeni/src/transforms/daffodil-guides-package/index.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { IdSanitizer } from '../../services/id-sanitizer';
2727
import { outputPathsConfigurator } from '../../utils/configurator/output';
2828
import { pathsConfigurator } from '../../utils/configurator/path';
2929
import { generateNavigationTrieFromDocuments } from '../../utils/navigation-trie';
30+
import { sortTrie } from '../../utils/trie-sort';
3031
import {
3132
API_SOURCE_PATH,
3233
DESIGN_PATH,
@@ -139,15 +140,20 @@ export const designDocsPackage = new Package('design-docs', [design])
139140
];
140141
})
141142
.config((generateNavList: GenerateNavListProcessor) => {
142-
generateNavList.transform = (docs) => generateNavigationTrieFromDocuments([
143+
generateNavList.transform = (docs) => sortTrie(
144+
generateNavigationTrieFromDocuments([
145+
{
146+
id: 'components',
147+
title: 'Components',
148+
path: `/${DAFF_DOCS_PATH}/${DAFF_DOCS_DESIGN_PATH}/${DAFF_DOC_KIND_PATH_SEGMENT_MAP[DaffDocKind.COMPONENT]}`,
149+
tableOfContents: '',
150+
},
151+
...docs.map(transformDesignGuideDoc),
152+
]),
143153
{
144-
id: 'components',
145-
title: 'Components',
146-
path: `/${DAFF_DOCS_PATH}/${DAFF_DOCS_DESIGN_PATH}/${DAFF_DOC_KIND_PATH_SEGMENT_MAP[DaffDocKind.COMPONENT]}`,
147-
tableOfContents: '',
154+
'': ['overview', 'whats-new', 'getting-started', 'foundations', 'components'],
148155
},
149-
...docs.map(transformDesignGuideDoc),
150-
]);
156+
);
151157
});
152158

153159
export const designExplanationsPackage = pathsConfigurator({

tools/dgeni/src/utils/trie-sort.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export interface Trie {
2+
id: string;
3+
children: Array<Trie>;
4+
}
5+
6+
/**
7+
* Defines the order of a trie's immediate children.
8+
*/
9+
export type TrieSortData = Record<Trie['id'], Array<Trie['id']>>;
10+
11+
/**
12+
* Sorts a trie top to bottom according to the passed sort data.
13+
* If the sort data does not contain data for a trie child, it is sorted after all children with sort data.
14+
*/
15+
export const sortTrie = <T extends Trie>(trie: T, sortData: TrieSortData): T => {
16+
if (sortData[trie.id]) {
17+
trie.children = trie.children.sort((a, b) => {
18+
const aIndex = sortData[trie.id].indexOf(a.id);
19+
const bIndex = sortData[trie.id].indexOf(b.id);
20+
21+
return aIndex === bIndex
22+
? 0
23+
: aIndex < 0
24+
? 1
25+
: bIndex < 0
26+
? -1
27+
: aIndex - bIndex;
28+
});
29+
}
30+
trie.children = trie.children.map((child) => sortTrie(child, sortData));
31+
32+
return trie;
33+
};

0 commit comments

Comments
 (0)