Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generate): auto generate nested sidebar #179 #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 33 additions & 43 deletions lib/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}