-
Notifications
You must be signed in to change notification settings - Fork 8
/
docs-index-pages.ts
76 lines (75 loc) · 2.24 KB
/
docs-index-pages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {
Store,
defDocType,
DocType,
getDocTypePath,
getCategoryPath,
removeTrailingSlash,
ensureStartingSlash,
} from '@component-controls/core';
import { getUniquesByField } from './docs-pages';
export interface DocHomePagesPath {
type: DocType;
path: string;
docId?: string;
storyId?: string;
lastModified?: string;
}
export const getHomePages = (store?: Store): DocHomePagesPath[] => {
const { pages = {}, categories = [] } = store?.config || {};
if (pages && store) {
const docs = Object.keys(store.docs);
const paths: DocHomePagesPath[] = Object.keys(pages)
.map((type: DocType) => {
const page = pages[type];
const path = getDocTypePath(store, page.basePath) as string;
const typeDocs = docs.filter(
key => type === (store.docs[key].type || defDocType),
);
if (!typeDocs.length) {
return { type, path: '' };
}
const docId = page.indexHome
? undefined
: docs.find(key => {
const doc = store.docs[key];
return (
removeTrailingSlash(ensureStartingSlash(doc?.route || '')) ===
path
);
}) ||
docs.find(key => (store.docs[key].type || defDocType) === type);
const doc = docId ? store.docs[docId] : undefined;
const docStories: string[] = doc?.stories || [];
const storyId: string | undefined = docStories.length
? docStories[0]
: undefined;
return {
lastModified: doc?.dateModified
? new Date(doc?.dateModified).toISOString()
: undefined,
type,
path,
docId,
storyId,
};
})
.filter(({ path }) => path);
const categoryPaths: DocHomePagesPath[] = categories
.map(category => {
const uniques = getUniquesByField(store, category);
return {
lastModified: undefined,
type: category,
docId: undefined,
storyId: undefined,
path: Object.keys(uniques).length
? getCategoryPath(store, category)
: '',
};
})
.filter(({ path }) => path);
return [...paths, ...categoryPaths];
}
return [];
};