This repository was archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathsidebar-generator.js
60 lines (55 loc) · 2.41 KB
/
sidebar-generator.js
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
const fs = require('fs')
const path = require('path')
const migrationLinksRecursive = (sidebarItems) => {
const result = sidebarItems.map((sidebarItem) => {
if (sidebarItem.type === 'category') {
if (sidebarItem.label === 'How-To Guides' || sidebarItem.label === 'Tutorials') {
return {
type: 'link',
href: sidebarItem.customProps.migration.href,
label: sidebarItem.customProps.migration.label,
}
}
return {...sidebarItem, items: migrationLinksRecursive(sidebarItem.items)}
}
if (sidebarItem.customProps?.migration?.href && sidebarItem.customProps?.migration?.label) {
sidebarItem = {
type: 'link',
href: sidebarItem.customProps.migration.href,
label: sidebarItem.customProps.migration.label,
}
}
return sidebarItem
})
return result
}
module.exports = async ({ defaultSidebarItemsGenerator, ...args }) => {
if (args.version.contentPath.endsWith('docs')) {
/**
* Adds a custom class name to the "Tutorials" and "How-To Guides" index
* pages, which we then use in the `/src/css/custom.scss` file to hide
* the `<ul>` element that is contained within the item. The result is a
* single "Tutorials/Guides" page that contains the list of all the docs
* underneath it, while those items are not displayed in the sidebar.
*/
args.docs.map((doc) => {
if (doc.id === 'guides/README' || doc.id === 'tutorials/README') {
doc.frontMatter.sidebar_class_name = "sidebar-category-items-hidden"
}
})
} else if (args.version.contentPath.endsWith('guides')) {
/**
* Adds the `hide_table_of_contents` directive to each of the "Guides"
* docs so that the page width isn't shrunk down.
*
* @todo For whatever reason, this doesn't appear to work like the
* sidebar class name does (above). For now, make sure to manually add
* `hide_table_of_contents: true` to the front matter of each guide doc.
*/
args.docs.map((doc) => {
doc.frontMatter.hide_table_of_contents = true
})
}
const sidebarItems = await defaultSidebarItemsGenerator({ ...args })
return migrationLinksRecursive(sidebarItems)
}