forked from speechmatics/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebar-generator.ts
More file actions
66 lines (61 loc) · 1.88 KB
/
sidebar-generator.ts
File metadata and controls
66 lines (61 loc) · 1.88 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
import type {
NormalizedSidebarItem,
SidebarItemsGeneratorOption,
} from "@docusaurus/plugin-content-docs/src/sidebars/types.js";
// Only include these endpoints in the sidebar, even though more exist in the spec
// TODO: Investigate this requirement. It would be ideal if we could either
// 1. Include all spec endpoints in the site, or
// 2. Remove irrelevant endpoints from the spec
const batchAPIPaths = [
"/jobs",
"/jobs/{jobid}",
"/jobs/{jobid}/transcript",
"/usage",
];
export const sidebarItemsGenerator: SidebarItemsGeneratorOption = async ({
defaultSidebarItemsGenerator,
...args
}) => {
const docs = args.docs
.filter((doc) => {
if (doc.frontMatter.api_path) {
return batchAPIPaths.includes(doc.frontMatter.api_path as string);
}
// TODO we can also filter schema docs we don't want here.
if (doc.frontMatter.sidebar_exclude) {
return false;
}
return true;
})
.map((doc) => {
// All index pages should be called "Overview" and be the first item in the sidebar
if (doc.id.match(/index$/)) {
const ret = {
...doc,
frontMatter: {
...doc.frontMatter,
sidebar_label: "Overview",
},
sidebarPosition: 0,
};
return ret;
}
return doc;
});
const defaults = await defaultSidebarItemsGenerator({ ...args, docs });
return defaults.map((item) => withNormalizedLabel(item));
};
// Capitalize the first letter of each word in the label, and replace hyphens with spaces
function withNormalizedLabel(item: NormalizedSidebarItem) {
if (item.type === "category") {
const normalizedLabel = item.label
.replace(/-/g, " ")
.replace(/\b\w/g, (char) => char.toUpperCase());
return {
...item,
label: normalizedLabel,
items: item.items.map(withNormalizedLabel),
};
}
return item;
}