Skip to content
Closed
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,16 @@ require('yargs')
nargs: 1,
requiresArg: true,
type: 'string'
},
'markdown-titles': {
alias: 't',
default: false,
desc: chalk.gray(y18n.__('generate.markdown_titles')),
nargs: 0,
type: 'boolean'
}
}),
handler: argv => run.generate(argv.path, argv.sidebar)
handler: argv => run.generate(argv.path, argv.sidebar, {markdown_titles: argv['markdown-titles'], index_file: argv['index-file']})
})
.help()
.option('help', {
Expand Down
30 changes: 23 additions & 7 deletions lib/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ const {cwd, exists} = require('../util')
const path = require('path')
const logger = require('../util/logger')
const ignoreFiles = ['_navbar', '_coverpage', '_sidebar']
const marked = require('marked')

// eslint-disable-next-line
module.exports = function (path = '', sidebar) {
module.exports = function (path = '', sidebar, options) {
const cwdPath = cwd(path || '.')

if (exists(cwdPath)) {
if (sidebar) {
const sidebarPath = cwdPath + '/' + sidebar || '_sidebar.md'

if (!exists(sidebarPath)) {
genSidebar(cwdPath, sidebarPath)
genSidebar(cwdPath, sidebarPath, options)
logger.success(`Successfully generated the sidebar file '${sidebar}'.`)
return true
}
Expand All @@ -30,21 +31,36 @@ module.exports = function (path = '', sidebar) {
logger.error(`${cwdPath} directory does not exist.`)
}

function genSidebar(cwdPath, sidebarPath) {
function genSidebar(cwdPath, sidebarPath, options) {
let tree = ''
let lastPath = ''
let nodeName = ''
getDirFiles(cwdPath, function (pathname) {
path.relative(pathname, cwdPath)
pathname = pathname.replace(cwdPath + '/', '')
getDirFiles(cwdPath, function (fullpathname) {
path.relative(fullpathname, cwdPath)
const pathname = fullpathname.replace(cwdPath + '/', '')
let filename = path.basename(pathname, '.md')
let splitPath = pathname.split(path.sep)

if (ignoreFiles.indexOf(filename) !== -1) {
return true
}

nodeName = '- [' + toCamelCase(filename) + '](' + pathname + ')' + os.EOL
let entryTitle = null
if (options.markdown_titles) {
const lexedTokens = marked.lexer(fs.readFileSync(fullpathname).toString())
.filter(token => token.type === 'heading')
.sort((a, b) => (a.depth < b.depth))
.map(i => i.text)
if (lexedTokens.length > 0) {
entryTitle = lexedTokens[0]
}
}

if (entryTitle === null) {
entryTitle = toCamelCase(filename)
}

nodeName = '- [' + entryTitle + '](' + pathname + ')' + os.EOL

if (splitPath.length > 1) {
if (splitPath[0] !== lastPath) {
Expand Down
3 changes: 2 additions & 1 deletion tools/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"serve.indexname": "Custom filename instead of index.html to serve by default",
"generate": "Docsify's generators",
"generate.sidebar": "Generate sidebar file",
"generate.markdown_titles": "Use markdown parser to get titles instead of guessing by filenames",
"livereload.port": "livereload Listen port.",
"usage": "Usage",
"version": "Show version number"
}
}