From 93f830b6e1d687287a7483199cf0a2ab7458d140 Mon Sep 17 00:00:00 2001 From: Anilople <15523186+Anilople@users.noreply.github.com> Date: Thu, 4 Nov 2021 20:38:14 +0800 Subject: [PATCH 1/3] feat: generate sidebar nested --- lib/commands/generate.js | 115 +++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 40 deletions(-) diff --git a/lib/commands/generate.js b/lib/commands/generate.js index 95943dc..9e1cbfc 100644 --- a/lib/commands/generate.js +++ b/lib/commands/generate.js @@ -6,6 +6,7 @@ const {cwd, exists} = require('../util') const path = require('path') const logger = require('../util/logger') const ignoreFiles = ['_navbar', '_coverpage', '_sidebar'] +const indent = ' ' // eslint-disable-next-line module.exports = function (path = '', sidebar) { @@ -16,7 +17,8 @@ module.exports = function (path = '', sidebar) { const sidebarPath = cwdPath + '/' + sidebar || '_sidebar.md' if (!exists(sidebarPath)) { - genSidebar(cwdPath, sidebarPath) + const lines = generateSidebar(cwdPath) + writeSidebar(lines, sidebarPath) logger.success(`Successfully generated the sidebar file '${sidebar}'.`) return true } @@ -30,51 +32,84 @@ module.exports = function (path = '', sidebar) { logger.error(`${cwdPath} directory does not exist.`) } -function genSidebar(cwdPath, sidebarPath) { - let tree = '' - let lastPath = '' - let nodeName = '' - getDirFiles(cwdPath, function (pathname) { - path.relative(pathname, cwdPath) - pathname = pathname.replace(cwdPath + '/', '') - let filename = path.basename(pathname, '.md') - let splitPath = pathname.split(path.sep) - - if (ignoreFiles.indexOf(filename) === -1) { - nodeName = '- [' + filename + '](' + pathname + ')' + os.EOL +function generateSidebar(cwdPath) { + return generateSidebarOfDirectory(cwdPath, cwdPath, 0) +} + +/** + * @returns a list of string + */ +function generateSidebarOfDirectory(baseDirectory, currentDirectory, indentCount) { + let lines = [] + + // Get all directories + const subDirectories = listDirectory(currentDirectory) + for (const subDirectory of subDirectories) { + const contentOfDirectory = generateSidebarContentOfDirectory(currentDirectory, indentCount) + console.log(contentOfDirectory) + lines.push(contentOfDirectory) + const subLines = generateSidebarOfDirectory(baseDirectory, subDirectory, indentCount + 1) + for (const subLine of subLines) { + lines.push(subLine) } + } - if (splitPath.length > 1) { - if (splitPath[0] !== lastPath) { - lastPath = splitPath[0] - tree += os.EOL + '- ' + splitPath[0] + os.EOL - } + // Get all files under currentDirectory + const mdFilePaths = listMdFilePaths(currentDirectory) + for (const mdFilePath of mdFilePaths) { + const mdFilePathLink = generateSidebarLinkOfFile(baseDirectory, mdFilePath, indentCount) + console.log(mdFilePathLink) + lines.push(mdFilePathLink) + } - tree += ' ' + nodeName - } else { - if (lastPath !== '') { - lastPath = '' - tree += os.EOL + return lines +} + +function listDirectory(directory) { + return fs.readdirSync(directory) + .map(relativePath => path.join(directory, relativePath)) + .filter(path => fs.statSync(path).isDirectory()) +} + +function listMdFilePaths(directory) { + return fs.readdirSync(directory) + .map(relativePath => path.join(directory, relativePath)) + .filter(pathname => !fs.statSync(pathname).isDirectory() && path.extname(pathname) === '.md') + .filter(pathname => { + for (const ignoreFile of ignoreFiles) { + if (pathname.includes(ignoreFile)) { + return false + } } - tree += nodeName - } - }) - fs.writeFile(sidebarPath, tree, 'utf8', err => { - if (err) { - logger.error(`Couldn't generate the sidebar file, error: ${err.message}`) - } - }) + return true + }) } -function getDirFiles(dir, callback) { - fs.readdirSync(dir).forEach(function (file) { - let pathname = path.join(dir, file) +function generateSidebarContentOfDirectory(currentDirectory, indentCount) { + const directoryName = path.basename(path.resolve(currentDirectory)) + return generateIndents(indentCount) + '- ' + directoryName +} - if (fs.statSync(pathname).isDirectory()) { - getDirFiles(pathname, callback) - } else if (path.extname(file) === '.md') { - callback(pathname) - } - }) +function generateSidebarLinkOfFile(baseDirectory, filePath, indentCount) { + return generateIndents(indentCount) + '- ' + generateLinkOfFile(baseDirectory, filePath) +} + +function generateIndents(indentCount) { + return indent.repeat(indentCount) +} + +function generateRelativePath(baseDirectory, fullPath) { + return path.relative(baseDirectory, fullPath) +} + +function generateLinkOfFile(baseDirectory, filePath) { + const name = path.basename(filePath, '.md') + const relativePath = generateRelativePath(baseDirectory, filePath) + return ''.concat('[', name, ']', '(', relativePath, ')') +} + +function writeSidebar(lines, sidebarPath) { + const content = lines.join(os.EOL) + fs.writeFileSync(sidebarPath, content, 'utf8') } From 81cbc478086e20cc33d5badb82b5524ce62d25ba Mon Sep 17 00:00:00 2001 From: Anilople <15523186+Anilople@users.noreply.github.com> Date: Thu, 4 Nov 2021 22:25:45 +0800 Subject: [PATCH 2/3] fix: directory name duplicate --- lib/commands/generate.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/commands/generate.js b/lib/commands/generate.js index 9e1cbfc..08a3da6 100644 --- a/lib/commands/generate.js +++ b/lib/commands/generate.js @@ -41,13 +41,14 @@ function generateSidebar(cwdPath) { */ function generateSidebarOfDirectory(baseDirectory, currentDirectory, indentCount) { let lines = [] + // Current directory + const contentOfCurrentDirectory = generateSidebarContentOfDirectory(currentDirectory, indentCount) + console.log(contentOfCurrentDirectory) + lines.push(contentOfCurrentDirectory) // Get all directories const subDirectories = listDirectory(currentDirectory) for (const subDirectory of subDirectories) { - const contentOfDirectory = generateSidebarContentOfDirectory(currentDirectory, indentCount) - console.log(contentOfDirectory) - lines.push(contentOfDirectory) const subLines = generateSidebarOfDirectory(baseDirectory, subDirectory, indentCount + 1) for (const subLine of subLines) { lines.push(subLine) @@ -57,7 +58,7 @@ function generateSidebarOfDirectory(baseDirectory, currentDirectory, indentCount // Get all files under currentDirectory const mdFilePaths = listMdFilePaths(currentDirectory) for (const mdFilePath of mdFilePaths) { - const mdFilePathLink = generateSidebarLinkOfFile(baseDirectory, mdFilePath, indentCount) + const mdFilePathLink = generateSidebarLinkOfFile(baseDirectory, mdFilePath, indentCount + 1) console.log(mdFilePathLink) lines.push(mdFilePathLink) } From 0c145607999acb5631c940539758fd431af0ad9f Mon Sep 17 00:00:00 2001 From: Anilople <15523186+Anilople@users.noreply.github.com> Date: Thu, 4 Nov 2021 22:46:49 +0800 Subject: [PATCH 3/3] fix: path sep is '\' in windows, change to '/' --- lib/commands/generate.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/commands/generate.js b/lib/commands/generate.js index 08a3da6..e1ef75e 100644 --- a/lib/commands/generate.js +++ b/lib/commands/generate.js @@ -107,7 +107,8 @@ function generateRelativePath(baseDirectory, fullPath) { function generateLinkOfFile(baseDirectory, filePath) { const name = path.basename(filePath, '.md') const relativePath = generateRelativePath(baseDirectory, filePath) - return ''.concat('[', name, ']', '(', relativePath, ')') + const relativePathNormalize = relativePath.split(path.sep).join('/') + return ''.concat('[', name, ']', '(', relativePathNormalize, ')') } function writeSidebar(lines, sidebarPath) {