diff --git a/lib/commands/generate.js b/lib/commands/generate.js index 6e4ae46..62becd9 100644 --- a/lib/commands/generate.js +++ b/lib/commands/generate.js @@ -30,58 +30,48 @@ module.exports = function (path, sidebar, options) { } function genSidebar(cwdPath, sidebarPath) { - let tree = '' - let lastPath = '' - let nodeName = '' - getDirFiles(cwdPath, function (pathname) { - path.relative(pathname, cwdPath) - pathname = pathname.replace(cwdPath + path.sep, '') - let filename = path.basename(pathname, '.md') - let splitPath = pathname.split(path.sep) - - if (ignoreFiles.indexOf(filename) !== -1) { - return true - } - - nodeName = '- [' + toCamelCase(filename) + '](' + pathname.replace(/\\/g, '/') + ')' + os.EOL - - if (splitPath.length > 1) { - if (splitPath[0] !== lastPath) { - lastPath = splitPath[0] - tree += os.EOL + '- ' + toCamelCase(splitPath[0]) + os.EOL - } - - tree += ' ' + nodeName - } else { - if (lastPath !== '') { - lastPath = '' - tree += os.EOL - } - - tree += nodeName - } - }) - fs.writeFile(sidebarPath, tree, 'utf8', err => { + const sidebarContent = generateContent(cwdPath, cwdPath).join(os.EOL) + fs.writeFileSync(sidebarPath, sidebarContent, 'utf8', err => { if (err) { logger.error(`Couldn't generate the sidebar file, error: ${err.message}`) } }) } -function getDirFiles(dir, callback) { - fs.readdirSync(dir).forEach(function (file) { - let pathname = path.join(dir, file) +function generateContent(directory, rootPath) { + const content = [] + fs.readdirSync(directory).forEach(file => { + const cwdPath = path.join(directory, file) + const relativePath = path.relative(rootPath, cwdPath) + const filePath = relativePath.replace(/\s/g, '%20') + const filename = modifyFileName(file) - if (fs.statSync(pathname).isDirectory()) { - getDirFiles(pathname, callback) - } else if (path.extname(file) === '.md') { - callback(pathname) + if (fs.statSync(cwdPath).isDirectory()) { + const childContent = generateContent(cwdPath, rootPath) // Recursive call + + const isReadmePresent = fs.existsSync(path.join(cwdPath, 'README.md')) + const hasChildContent = childContent.length > 0 + + if (hasChildContent && isReadmePresent) { + content.push(`- [${filename}](${filePath}/README.md)`, ...childContent.map(item => ` ${item}`)) + } else if (hasChildContent && !isReadmePresent) { + content.push(`- ${filename}`, ...childContent.map(item => ` ${item}`)) + } else if (!hasChildContent && isReadmePresent) { + content.push(`- [${filename}](${filePath}/README.md)`) + } else { + content.push(`- [${filename}](${filePath})`) + } + } else if (path.extname(file) === '.md' && + file.toLowerCase() !== 'readme.md' && + ignoreFiles.indexOf(filename) === -1) { + content.push(`- [${filename}](${filePath})`) } }) + return content } -function toCamelCase(str) { - return str.replace(/\b(\w)/g, function (match, capture) { - return capture.toUpperCase() - }).replace(/-|_/g, ' ') +function modifyFileName(file) { + const filename = file.split('-') + const fileWithExtension = filename.length > 1 ? filename[1] : filename[0] + return path.basename(fileWithExtension, '.md') }