Skip to content

Commit beaf72b

Browse files
committed
feat(generate): auto generate nested sidebar #179
1 parent 0d725a9 commit beaf72b

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

lib/commands/generate.js

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,58 +30,48 @@ module.exports = function (path, sidebar, options) {
3030
}
3131

3232
function genSidebar(cwdPath, sidebarPath) {
33-
let tree = ''
34-
let lastPath = ''
35-
let nodeName = ''
36-
getDirFiles(cwdPath, function (pathname) {
37-
path.relative(pathname, cwdPath)
38-
pathname = pathname.replace(cwdPath + path.sep, '')
39-
let filename = path.basename(pathname, '.md')
40-
let splitPath = pathname.split(path.sep)
41-
42-
if (ignoreFiles.indexOf(filename) !== -1) {
43-
return true
44-
}
45-
46-
nodeName = '- [' + toCamelCase(filename) + '](' + pathname.replace(/\\/g, '/') + ')' + os.EOL
47-
48-
if (splitPath.length > 1) {
49-
if (splitPath[0] !== lastPath) {
50-
lastPath = splitPath[0]
51-
tree += os.EOL + '- ' + toCamelCase(splitPath[0]) + os.EOL
52-
}
53-
54-
tree += ' ' + nodeName
55-
} else {
56-
if (lastPath !== '') {
57-
lastPath = ''
58-
tree += os.EOL
59-
}
60-
61-
tree += nodeName
62-
}
63-
})
64-
fs.writeFile(sidebarPath, tree, 'utf8', err => {
33+
const sidebarContent = generateContent(cwdPath, cwdPath).join(os.EOL)
34+
fs.writeFileSync(sidebarPath, sidebarContent, 'utf8', err => {
6535
if (err) {
6636
logger.error(`Couldn't generate the sidebar file, error: ${err.message}`)
6737
}
6838
})
6939
}
7040

71-
function getDirFiles(dir, callback) {
72-
fs.readdirSync(dir).forEach(function (file) {
73-
let pathname = path.join(dir, file)
41+
function generateContent(directory, rootPath) {
42+
const content = []
43+
fs.readdirSync(directory).forEach(file => {
44+
const cwdPath = path.join(directory, file)
45+
const relativePath = path.relative(rootPath, cwdPath)
46+
const filePath = relativePath.replace(/\s/g, '%20')
47+
const filename = modifyFileName(file)
7448

75-
if (fs.statSync(pathname).isDirectory()) {
76-
getDirFiles(pathname, callback)
77-
} else if (path.extname(file) === '.md') {
78-
callback(pathname)
49+
if (fs.statSync(cwdPath).isDirectory()) {
50+
const childContent = generateContent(cwdPath, rootPath) // Recursive call
51+
52+
const isReadmePresent = fs.existsSync(path.join(cwdPath, 'README.md'))
53+
const hasChildContent = childContent.length > 0
54+
55+
if (hasChildContent && isReadmePresent) {
56+
content.push(`- [${filename}](${filePath}/README.md)`, ...childContent.map(item => ` ${item}`))
57+
} else if (hasChildContent && !isReadmePresent) {
58+
content.push(`- ${filename}`, ...childContent.map(item => ` ${item}`))
59+
} else if (!hasChildContent && isReadmePresent) {
60+
content.push(`- [${filename}](${filePath}/README.md)`)
61+
} else {
62+
content.push(`- [${filename}](${filePath})`)
63+
}
64+
} else if (path.extname(file) === '.md' &&
65+
file.toLowerCase() !== 'readme.md' &&
66+
ignoreFiles.indexOf(filename) === -1) {
67+
content.push(`- [${filename}](${filePath})`)
7968
}
8069
})
70+
return content
8171
}
8272

83-
function toCamelCase(str) {
84-
return str.replace(/\b(\w)/g, function (match, capture) {
85-
return capture.toUpperCase()
86-
}).replace(/-|_/g, ' ')
73+
function modifyFileName(file) {
74+
const filename = file.split('-')
75+
const fileWithExtension = filename.length > 1 ? filename[1] : filename[0]
76+
return path.basename(fileWithExtension, '.md')
8777
}

0 commit comments

Comments
 (0)