-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathsite.mjs
More file actions
75 lines (57 loc) · 2.06 KB
/
Copy pathsite.mjs
File metadata and controls
75 lines (57 loc) · 2.06 KB
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
import { ReflectionKind } from 'typedoc';
import { CATEGORY_RULES } from '../shared/categories.mjs';
const SIDEBAR_KINDS =
ReflectionKind.Project | ReflectionKind.Namespace | ReflectionKind.Class;
const SIDEBAR_GROUP_NAME = 'API Documentation';
const getFirstAtxHeading = text => text.match(/^#\s+(.+)$/m)?.[1]?.trim();
const getFirstPathSegment = url => url.replace(/^\//, '').split('/')[0];
const toSidebarLink = (url, basePath) => {
const path = url.replace(/\.md$/, '').replace(/\/index$/, '');
const prefix = basePath ? `/${basePath.replace(/^\/|\/$/g, '')}` : '';
return path ? `${prefix}/${path}` : prefix || '/';
};
const defaultLabelFor = (target, url) => {
if (url.endsWith('/index.md')) return 'Overview';
if (url.endsWith('/types.md')) return 'Types';
return target.name;
};
const isSidebarTarget = (router, target) => {
if (!target.kindOf?.(SIDEBAR_KINDS)) return false;
if (!router.hasOwnDocument(target)) return false;
const url = router.getFullUrl(target);
return url.endsWith('.md') && !url.includes('#');
};
export const sidebar = (router, basePath) => {
const categories = new Map();
const seen = new Set();
for (const target of router.getLinkTargets()) {
if (!isSidebarTarget(router, target)) continue;
const url = router.getFullUrl(target);
if (seen.has(url)) continue;
seen.add(url);
const firstPathSegment = getFirstPathSegment(url);
const category = CATEGORY_RULES.find(
cat => cat.category === firstPathSegment
);
if (!category) continue;
const headingName = getFirstAtxHeading(
target.comment?.summary?.[0]?.text ?? ''
);
const label = headingName ?? defaultLabelFor(target, url);
const group = categories.get(category.category) ?? {
label: category.label,
items: [],
};
group.items.push({
link: toSidebarLink(url, basePath),
label,
});
categories.set(category.category, group);
}
return [
{
groupName: SIDEBAR_GROUP_NAME,
items: [...categories.values()].filter(category => category.items.length),
},
];
};