Skip to content

Commit

Permalink
feat(generate): auto generate nested sidebar docsifyjs#179
Browse files Browse the repository at this point in the history
  • Loading branch information
theMyth333 committed Dec 29, 2023
1 parent 0d725a9 commit beaf72b
Showing 1 changed file with 33 additions and 43 deletions.
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')
}

0 comments on commit beaf72b

Please sign in to comment.