forked from PlaceOS/docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidebars.js
105 lines (85 loc) · 3.06 KB
/
sidebars.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Generates sidebar nav based on content file structure.
*
* The following frontmatter can be used to override the default behaviour on
* all documents:
*
* sidebar_label
* sidebar_order
*
* Additionaly, the first document (after sorting) within each category can
* control the category appearance and ordering via:
*
* sidebar_category_label
* sidebar_category_order
*
*/
const fs = require('fs')
const path = require('path')
const { parseMarkdownString } = require('@docusaurus/utils')
// Ignore `.` files and directories, anything that isn't markdown and readme's
const ignored = /(^\.)|(\.(?!md$))|(^readme\.md$)|^node_modules$/i
// Relative path from CWD to the site renderer
const siteDir = process.env.npm_package_config_site_dir || "."
// Relative path from siteDir to the docs content
const docsPath = process.env.npm_package_config_docs_path
const content_path = path.join(siteDir, docsPath)
function parseFrontMatter(path) {
const content = fs.readFileSync(path, 'utf8')
const parsed = parseMarkdownString(content)
return parsed.frontMatter || {}
}
function generateDocItem(baseDir, docPath) {
const {
sidebar_order : frontmatter_order = Infinity,
sidebar_hide : hide = false,
id : docId = path.parse(docPath).name,
slug = undefined
} = parseFrontMatter(path.join(baseDir, docPath))
const id = path.join(path.dirname(docPath), docId)
const order = slug == './' ? -Infinity : frontmatter_order
return hide ? {} : { type: 'doc', id, order }
}
function generateCategoryItem(baseDir, categoryPath) {
const items = generateItems({ baseDir, sourceDir: categoryPath })
const introItem = items.find(item => typeof item === 'string')
let label = path.basename(categoryPath)
let order = Infinity
let hide = false
if (introItem) {
const {
sidebar_category_label = label,
sidebar_category_order = order,
sidebar_category_hide = hide
} = parseFrontMatter(path.join(baseDir, `${introItem}.md`))
label = sidebar_category_label
order = sidebar_category_order
hide = sidebar_category_hide
}
return hide ? {} : { type: 'category', label, order, items }
}
function generateItems({ baseDir, sourceDir }) {
const searchIn = path.join(baseDir, sourceDir)
const files = fs.readdirSync(searchIn, { withFileTypes: true })
const include = dirent => !dirent.name.match(ignored)
const toItem = dirent => {
const { name } = dirent
const itemPath = path.join(sourceDir, name)
if (dirent.isDirectory()) {
return generateCategoryItem(baseDir, itemPath)
} else {
return generateDocItem(baseDir, itemPath)
}
}
return files
.filter(include)
.map(toItem)
.sort((a, b) => a.order - b.order)
.map(({ id, order, ...item }) => id || item)
}
module.exports = {
overview: generateItems({ baseDir: content_path, sourceDir: 'overview' }),
how_to: generateItems({ baseDir: content_path, sourceDir: 'how-to' }),
tutorials: generateItems({ baseDir: content_path, sourceDir: 'tutorial' }),
reference: generateItems({ baseDir: content_path, sourceDir: 'reference' })
};