-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a7c784
commit 195e7f9
Showing
6 changed files
with
156 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
commonjs: true, | ||
es2021: true, | ||
node: true, | ||
jest: true | ||
}, | ||
extends: [ | ||
'standard' | ||
], | ||
parserOptions: { | ||
ecmaVersion: 12 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,70 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fs = require('fs') | ||
const path = require('path') | ||
const colors = require('colors/safe') | ||
|
||
const colors = require("colors/safe"); | ||
const { getMenuPath, getFilename, filterRootMarkdowns, groupBy, genSidebar, titleSort, sidebarSort, findGroupIndex, genNav } = require("./lib/utils"); | ||
const sidebarOptions = require("./lib/options"); | ||
const { getMenuPath, getFilename, filterRootMarkdowns, groupBy, genSidebar, titleSort, sidebarSort, findGroupIndex, genNav } = require('./lib/utils') | ||
const sidebarOptions = require('./lib/options') | ||
|
||
let SIDEBAR = Object.create(null); | ||
let SIDEBAR = Object.create(null) | ||
|
||
module.exports = (options, ctx) => ({ | ||
name: "vuepress-plugin-auto-sidebar", | ||
async ready() { | ||
name: 'vuepress-plugin-auto-sidebar', | ||
async ready () { | ||
try { | ||
const mergeOptions = Object.assign({}, sidebarOptions, options); | ||
const { pages } = ctx; | ||
const mergeOptions = Object.assign({}, sidebarOptions, options) | ||
const { pages } = ctx | ||
|
||
// 整理 pages 数据 | ||
const mapPages = pages.filter(page => page.relativePath).map(page => ({ | ||
frontmatter: page.frontmatter, | ||
menuPath: getMenuPath(page.relativePath), | ||
filename: getFilename(page.relativePath) | ||
})).filter(filterRootMarkdowns); | ||
})).filter(filterRootMarkdowns) | ||
|
||
// 过滤出待排序的 | ||
const sortQueue = mapPages.filter(page => page.frontmatter.autoPrev || page.frontmatter.autoNext); | ||
const defaultPages = mapPages.filter(page => !page.frontmatter.autoPrev && !page.frontmatter.autoNext); | ||
const sortQueue = mapPages.filter(page => page.frontmatter.autoPrev || page.frontmatter.autoNext) | ||
const defaultPages = mapPages.filter(page => !page.frontmatter.autoPrev && !page.frontmatter.autoNext) | ||
|
||
const groupByDepth = groupBy(defaultPages, "menuPath"); | ||
const groupByDepth = groupBy(defaultPages, 'menuPath') | ||
|
||
titleSort(groupByDepth, mergeOptions.sort); | ||
let sortQueueCache = []; | ||
titleSort(groupByDepth, mergeOptions.sort) | ||
let sortQueueCache = [] | ||
while (sortQueue.length) { | ||
const current = sortQueue.pop(); | ||
const index = findGroupIndex(current, groupByDepth); | ||
const current = sortQueue.pop() | ||
const index = findGroupIndex(current, groupByDepth) | ||
|
||
if (index !== -1) { | ||
current.frontmatter.autoPrev ? | ||
groupByDepth[current.menuPath].splice(index + 1, 0, current) : | ||
groupByDepth[current.menuPath].splice(index, 0, current) | ||
current.frontmatter.autoPrev | ||
? groupByDepth[current.menuPath].splice(index + 1, 0, current) | ||
: groupByDepth[current.menuPath].splice(index, 0, current) | ||
|
||
sortQueue.push(...sortQueueCache); | ||
sortQueueCache = []; | ||
sortQueue.push(...sortQueueCache) | ||
sortQueueCache = [] | ||
} else { | ||
sortQueueCache.push(current); | ||
sortQueueCache.push(current) | ||
} | ||
} | ||
|
||
if (sortQueueCache.length) { | ||
console.log(colors.red("\nvuepress plugin auto sidebar(精准排序): "), `\n [${colors.green(sortQueueCache.map(q => `${q.filename}(${q.frontmatter.title})`).join("、"))}] \t共 ${sortQueueCache.length} 个文件指向了不存在的 prev 或 next`); | ||
console.log(colors.red('\nvuepress plugin auto sidebar(精准排序): '), `\n [${colors.green(sortQueueCache.map(q => `${q.filename}(${q.frontmatter.title})`).join('、'))}] \t共 ${sortQueueCache.length} 个文件指向了不存在的 prev 或 next`) | ||
} | ||
|
||
SIDEBAR = genSidebar(sidebarSort(groupByDepth), mergeOptions); | ||
SIDEBAR = genSidebar(sidebarSort(groupByDepth), mergeOptions) | ||
|
||
const nav = genNav(SIDEBAR); | ||
const dest = path.join(ctx.sourceDir, ".vuepress/nav.js"); | ||
const nav = genNav(SIDEBAR) | ||
const dest = path.join(ctx.sourceDir, '.vuepress/nav.js') | ||
|
||
if (mergeOptions.nav && !fs.existsSync(dest)) { | ||
await fs.writeFileSync(dest, `module.exports = ${JSON.stringify(nav)};`); | ||
await fs.writeFileSync(dest, `module.exports = ${JSON.stringify(nav)};`) | ||
} | ||
} catch (ex) { | ||
console.error(ex); | ||
console.error(ex) | ||
} | ||
}, | ||
async enhanceAppFiles() { | ||
async enhanceAppFiles () { | ||
return { | ||
name: "auto-sidebar-enhance", | ||
name: 'auto-sidebar-enhance', | ||
content: `export default ({ siteData, options }) => { siteData.themeConfig.sidebar = ${JSON.stringify(SIDEBAR)} }` | ||
} | ||
} | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
module.exports = { | ||
sort: "asc", // 排序 | ||
titleMode: "default", // 标题模式 | ||
sort: 'asc', // 排序 | ||
titleMode: 'default', // 标题模式 | ||
titleMap: {}, // 标题映射 | ||
nav: false, // 导航栏 | ||
sidebarDepth: 1, // 标题深度 | ||
collapsable: false, // 折叠 | ||
collapseList: [], // 折叠列表 | ||
uncollapseList: [], // 不折叠列表 | ||
} | ||
uncollapseList: [] // 不折叠列表 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.