Skip to content

Commit ecd19c4

Browse files
committed
Update gen sidebar
1 parent a91d283 commit ecd19c4

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ require('yargs')
111111
})
112112
.command({
113113
command: 'gen <path>',
114-
aliases: 't',
114+
aliases: 'g',
115115
desc: chalk.gray(y18n.__('gen')),
116116
builder: yargs =>
117117
yargs.options({

lib/commands/gen.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
'use strict'
22

33
const fs = require('fs')
4+
const os = require('os')
45
const {cwd, exists} = require('../util')
56
const chalk = require('chalk')
67
const path = require('path')
8+
const ignoreFiles = ['_navbar', '_coverpage', '_sidebar']
79

810
// eslint-disable-next-line
911
module.exports = function (path = '', sidebar) {
1012
const cwdPath = cwd(path || '.')
1113

1214
if (exists(cwdPath)) {
1315
if (sidebar) {
14-
console.log(sidebar)
1516
const sidebarPath = cwdPath + '/' + sidebar || '_sidebar.md'
1617

1718
if (!exists(sidebarPath)) {
1819
genSidebar(cwdPath, sidebarPath)
20+
console.log(chalk.green(`Generate sidebar file '${sidebar}' success.`))
1921
return true
2022
}
2123

@@ -28,27 +30,48 @@ module.exports = function (path = '', sidebar) {
2830
}
2931

3032
function genSidebar(cwdPath, sidebarPath) {
31-
let node = ''
32-
travel(cwdPath, function (pathname) {
33+
let tree = ''
34+
let lastPath = ''
35+
let nodeName = ''
36+
getDirFiles(cwdPath, function (pathname) {
3337
path.relative(pathname, cwdPath)
3438
pathname = pathname.replace(cwdPath + '/', '')
35-
node += '* [' + path.basename(pathname, '.md') + '](' + pathname + ')\n'
39+
let filename = path.basename(pathname, '.md')
40+
let splitPath = pathname.split(path.sep)
41+
42+
if (ignoreFiles.indexOf(filename) === -1) {
43+
nodeName = '- [' + filename + '](' + pathname + ')' + os.EOL
44+
}
45+
46+
if (splitPath.length > 1) {
47+
if (splitPath[0] !== lastPath) {
48+
lastPath = splitPath[0]
49+
tree += os.EOL + '- ' + splitPath[0] + os.EOL
50+
}
51+
52+
tree += ' ' + nodeName
53+
} else {
54+
if (lastPath !== '') {
55+
lastPath = ''
56+
tree += os.EOL
57+
}
58+
59+
tree += nodeName
60+
}
3661
})
37-
fs.writeFile(sidebarPath, node, 'utf8', err => {
62+
fs.writeFile(sidebarPath, tree, 'utf8', err => {
3863
if (err) {
39-
throw err
64+
console.log(chalk.red(`Generate sidebar file error, message: ${err.message}`))
4065
}
41-
42-
console.log('gen success')
4366
})
4467
}
4568

46-
function travel(dir, callback) {
69+
function getDirFiles(dir, callback) {
4770
fs.readdirSync(dir).forEach(function (file) {
4871
let pathname = path.join(dir, file)
4972

5073
if (fs.statSync(pathname).isDirectory()) {
51-
travel(pathname, callback)
74+
getDirFiles(pathname, callback)
5275
} else if (path.extname(file) === '.md') {
5376
callback(pathname)
5477
}

0 commit comments

Comments
 (0)