-
-
Notifications
You must be signed in to change notification settings - Fork 158
feat: auto generate sidebar #130
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const test = require('ava') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const {run} = require('../helpers/test-utils.js') | ||
|
||
const genPath = path.join(__dirname, 'generate-cmd') | ||
const docsPath = path.join(genPath, 'docs') | ||
|
||
test.before('create temp directory', () => { | ||
// Cleanup if the directory already exists | ||
if (fs.existsSync(genPath)) { | ||
fs.rmdirSync(genPath, {recursive: true}) | ||
} | ||
|
||
fs.mkdirSync(genPath) | ||
}) | ||
|
||
test.after('cleanup', () => { | ||
fs.rmdirSync(genPath, {recursive: true}) | ||
}) | ||
|
||
test('generate _sidebar.md', t => { | ||
sy-records marked this conversation as resolved.
Show resolved
Hide resolved
|
||
run(['init', 'docs'], {cwd: genPath}) | ||
run(['generate', 'docs'], {cwd: genPath}) | ||
// Check for existence | ||
t.true(fs.existsSync(path.join(docsPath, '_sidebar.md'))) | ||
}) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,79 @@ | ||||||
'use strict' | ||||||
|
||||||
const fs = require('fs') | ||||||
const os = require('os') | ||||||
const {cwd, exists} = require('../util') | ||||||
const chalk = require('chalk') | ||||||
const path = require('path') | ||||||
const ignoreFiles = ['_navbar', '_coverpage', '_sidebar'] | ||||||
|
||||||
// eslint-disable-next-line | ||||||
module.exports = function (path = '', sidebar) { | ||||||
const cwdPath = cwd(path || '.') | ||||||
|
||||||
if (exists(cwdPath)) { | ||||||
if (sidebar) { | ||||||
const sidebarPath = cwdPath + '/' + sidebar || '_sidebar.md' | ||||||
|
||||||
if (!exists(sidebarPath)) { | ||||||
genSidebar(cwdPath, sidebarPath) | ||||||
console.log(chalk.green(`Generate sidebar file '${sidebar}' success.`)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to have a - console.error(chalk.red(msg))
+ logger.error(msg) // logger.js
const error = (msg) => console.error(chalk.red(msg)); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After the merge, I will submit a PR to amend this |
||||||
return true | ||||||
} | ||||||
|
||||||
console.log(chalk.red(`${sidebarPath}`) + ' already exists.') | ||||||
sy-records marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return false | ||||||
Koooooo-7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
} | ||||||
|
||||||
console.log(chalk.red(`${cwdPath}`) + ' directory does not exist.') | ||||||
sy-records marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
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 | ||||||
} | ||||||
|
||||||
if (splitPath.length > 1) { | ||||||
if (splitPath[0] !== lastPath) { | ||||||
lastPath = splitPath[0] | ||||||
tree += os.EOL + '- ' + splitPath[0] + os.EOL | ||||||
} | ||||||
|
||||||
tree += ' ' + nodeName | ||||||
} else { | ||||||
if (lastPath !== '') { | ||||||
lastPath = '' | ||||||
tree += os.EOL | ||||||
} | ||||||
|
||||||
tree += nodeName | ||||||
} | ||||||
}) | ||||||
fs.writeFile(sidebarPath, tree, 'utf8', err => { | ||||||
if (err) { | ||||||
console.log(chalk.red(`Generate sidebar file error, message: ${err.message}`)) | ||||||
sy-records marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
}) | ||||||
} | ||||||
|
||||||
function getDirFiles(dir, callback) { | ||||||
fs.readdirSync(dir).forEach(function (file) { | ||||||
let pathname = path.join(dir, file) | ||||||
|
||||||
if (fs.statSync(pathname).isDirectory()) { | ||||||
getDirFiles(pathname, callback) | ||||||
} else if (path.extname(file) === '.md') { | ||||||
callback(pathname) | ||||||
} | ||||||
}) | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
init: require('./commands/init'), | ||
serve: require('./commands/serve'), | ||
start: require('./commands/start') | ||
start: require('./commands/start'), | ||
generate: require('./commands/generate') | ||
} |
Uh oh!
There was an error while loading. Please reload this page.