Skip to content

Commit

Permalink
feat: 新增忽略功能
Browse files Browse the repository at this point in the history
  • Loading branch information
shanyuhai123 committed Mar 22, 2021
1 parent 1de9a6d commit 24a954c
Show file tree
Hide file tree
Showing 21 changed files with 3,240 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
git --no-pager log --oneline -n 20
yarn
lerna bootstrap
lerna run build:lib
lerna run build
lerna run docs:build
- name: Deploy Github 🚀
uses: peaceiris/actions-gh-pages@v3
Expand Down
4 changes: 2 additions & 2 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Vuepress Plugin Auto Sidebar

[![Deploy Vuepress Plugin Auto Sidebar](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml/badge.svg)](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml)
[![Vuepress Plugin Auto Sidebar](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml/badge.svg)](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml)


[English](./README.md) | 简体中文
Expand Down Expand Up @@ -33,7 +33,7 @@ module.exports = {
}
```

:book: **更详细的文档见 [vuepress 自动生成侧边栏](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**
:book: **更详细的文档见 [vuepress-plugin-auto-sidebar](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**



Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Vuepress Plugin Auto Sidebar

[![Deploy Vuepress Plugin Auto Sidebar](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml/badge.svg)](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml)
[![Vuepress Plugin Auto Sidebar](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml/badge.svg)](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml)


English | [简体中文](./README-zh_CN.md)
Expand Down Expand Up @@ -33,7 +33,7 @@ module.exports = {
}
```

:book: **For more detailed documentation, you can visit [vuepress 自动生成侧边栏](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**
:book: **For more detailed documentation, you can visit [vuepress-plugin-auto-sidebar](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**



Expand Down
10 changes: 9 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"useWorkspaces": true,
"npmClient": "yarn",
"packages": [
"packages/*"
],
"version": "2.0.0"
"version": "independent",
"command": {
"publish": {
"ignoreChanges": ["*.md"],
"message": "chore(release): publish",
"registry": "https://registry.npmjs.org/"
}
}
}
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"private": true,
"version": "1.0.0",
"workspaces": [
"packages/*"
],
"author": "shanyuhai123",
"license": "MIT",
"repository": {
Expand All @@ -11,6 +14,11 @@
"url": "https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/issues"
},
"homepage": "https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar#readme",
"scripts": {
"build": "lerna run --stream --sort build",
"lint": "eslint --ext .js,.ts packages",
"release": "yarn lint && yarn build && lerna publish"
},
"devDependencies": {
"@types/jest": "^26.0.20",
"@typescript-eslint/eslint-plugin": "^4.17.0",
Expand Down
2 changes: 0 additions & 2 deletions packages/vuepress-docs-gh-pages/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ module.exports = {
}
```

:book: **更详细的文档见 [vuepress 自动生成侧边栏](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**



### 1. 简单的导航栏
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,18 @@ autoGroup-1: 分组名称

效果展示:

<img :src="$withBase('/assets/group-config-effect.png')" alt="分组效果">
<img :src="$withBase('/assets/group-config-effect.png')" alt="分组效果">



## 忽略文件

希望当前文件不在侧边栏中显示:

```yaml
---
autoIgnore: true
---
```

如果希望批量隐藏,以正则的形式匹配可参考[插件的可选项](/plugin-options)
12 changes: 11 additions & 1 deletion packages/vuepress-docs-gh-pages/docs/features/plugin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ module.exports = {
collapseList: ["/menu1/menu1-2/"],
// 选择要打开的目录,例如
uncollapseList: ["/menu1/menu1-3/"]
}
},
// 忽略,例如
ignore: [
{
// 指定路径
menu: "/menu3/menu3-3/",
// 对该路径下使用的正则,默认为 `.*`
// 例如你想忽略以 `ignore-` 开头的文件
regex: "ignore-*"
}
]
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/vuepress-docs-test/docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ module.exports = {
open: true,
collapseList: ["/menu1/menu1-2/"],
uncollapseList: ["/menu1/menu1-3/"]
}
},
ignore: [
{
menu: "/menu3/menu3-3/",
regex: "ignore-*"
}
]
}
},
themeConfig: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Ignore Item 1
---

Hello Ignore Item 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Ignore Item 2
autoIgnore: true
---

Hello Ignore Item 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Ignore Regex 1
---

Hello Ignore Regex 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Ignore Regex 2
---

Hello Ignore Regex 2
5 changes: 4 additions & 1 deletion packages/vuepress-plugin-auto-sidebar/README-zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Vuepress Plugin Auto Sidebar

[![Vuepress Plugin Auto Sidebar](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml/badge.svg)](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml)


[English](./README.md) | 简体中文


Expand Down Expand Up @@ -30,7 +33,7 @@ module.exports = {
}
```

:book: **更详细的文档见 [vuepress 自动生成侧边栏](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**
:book: **更详细的文档见 [vuepress-plugin-auto-sidebar](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**



Expand Down
5 changes: 4 additions & 1 deletion packages/vuepress-plugin-auto-sidebar/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Vuepress Plugin Auto Sidebar

[![Vuepress Plugin Auto Sidebar](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml/badge.svg)](https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/actions/workflows/deploy-docs.yml)


English | [简体中文](./README-zh_CN.md)


Expand Down Expand Up @@ -30,7 +33,7 @@ module.exports = {
}
```

:book: **For more detailed documentation, you can visit [vuepress 自动生成侧边栏](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**
:book: **For more detailed documentation, you can visit [vuepress-plugin-auto-sidebar](https://shanyuhai123.github.io/vuepress-plugin-auto-sidebar)**



Expand Down
6 changes: 3 additions & 3 deletions packages/vuepress-plugin-auto-sidebar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
"url": "git+https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar.git"
},
"scripts": {
"build:lib": "rimraf lib && tsc -p tsconfig.json"
"build": "rimraf lib && tsc -p tsconfig.json"
},
"bugs": {
"url": "https://github.com/shanyuhai123/vuepress-plugin-auto-sidebar/issues"
},
"dependencies": {
"colors": "^1.4.0"
"colors": "^1.4.0",
"merge": "^2.1.1"
},
"devDependencies": {
"merge": "^2.1.1",
"rimraf": "^3.0.2"
},
"gitHead": "a8a16e6995ecec66b280599b3e8597bad6ed1cd7"
Expand Down
3 changes: 2 additions & 1 deletion packages/vuepress-plugin-auto-sidebar/src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export const AutoSidebarOptionsDefault: AutoSidebarPluginOptions = {
open: false, // 默认是否折叠
collapseList: [], // 折叠列表
uncollapseList: [] // 不折叠列表
}
},
ignore: []
}
7 changes: 5 additions & 2 deletions packages/vuepress-plugin-auto-sidebar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { join } from 'path'
import { AutoSidebarOptionsDefault } from './config/options'
import { AutoSidebarPluginOptions } from './types'
import { genNav } from './utils/nav'
import { distinguishSpecifiedSortPages, groupPagesByMenuPath, handlePages } from './utils/pages'
import { distinguishSpecifiedSortPages, groupPagesByMenuPath, handleIgnorePages, handlePages } from './utils/pages'
import { genSidebar } from './utils/sidebar'
import { pagesSort, pagesGroupSort, specifiedPagesSort } from './utils/sort'

const AutoSidebarPlugin = (
options: AutoSidebarPluginOptions,
ctx: Context
) => {
const MERGE_OPTIONS = merge.recursive({}, AutoSidebarOptionsDefault, options)
const MERGE_OPTIONS: AutoSidebarPluginOptions = merge.recursive(AutoSidebarOptionsDefault, options)
let AUTO_SIDEBAR_DATA = Object.create(null)

return {
Expand All @@ -29,6 +29,9 @@ const AutoSidebarPlugin = (
// 对 defaultPages 进行分组
const defaultPagesGroupByMenuPath = groupPagesByMenuPath(defaultPages)

// 从侧边栏隐藏指定文件
handleIgnorePages(defaultPagesGroupByMenuPath, MERGE_OPTIONS.ignore)

// 排序优先级
// 1. 首先会根据 sort 参数进行排序(内置或自定义)
// 1.1 在内置规则下会判断 README 是否提前,默认为提前,而自定义规则时自行处理
Expand Down
9 changes: 9 additions & 0 deletions packages/vuepress-plugin-auto-sidebar/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface AutoSidebarPageFrontmatter {
autoPrev?: string
autoNext?: string
autoGroup?: string
autoIgnore?: boolean
}

export interface AutoSidebarPage {
Expand All @@ -33,6 +34,13 @@ type SIDEBAR_OPTIONS_TITLE =
| 'kebabcase'
| 'titlecase'

interface IgnoreOption {
menu: string
regex?: RegExp
}

export type IgnoreOptions = IgnoreOption[]

export interface SortOptions {
mode?: SIDEBAR_OPTIONS_SORT
fn?: ArraySortFn<AutoSidebarPage>
Expand Down Expand Up @@ -60,6 +68,7 @@ export interface AutoSidebarPluginOptions {
title: TitleOptions
sidebarDepth: number
collapse: CollapseOptions
ignore: IgnoreOptions
}

export interface GroupPagesResult {
Expand Down
23 changes: 21 additions & 2 deletions packages/vuepress-plugin-auto-sidebar/src/utils/pages.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Context } from 'vuepress-types'
import { AutoSidebarPage, GroupPagesResult } from '../types'
import * as colors from 'colors'
import { AutoSidebarPage, GroupPagesResult, IgnoreOptions } from '../types'
import { filterRootMarkdowns, getMenuPath } from './path'

// 从 pages 中提取部分参数
// 并过滤掉异常 page(非 markdown 生成的)
export const handlePages = (ctx: Context): AutoSidebarPage[] => ctx.pages
.filter(page => page.relativePath)
.map(page => ({
.map((page) => ({
xxx: page.path,
relativePath: page.relativePath,
menuPath: getMenuPath(page.relativePath),
Expand All @@ -15,6 +16,24 @@ export const handlePages = (ctx: Context): AutoSidebarPage[] => ctx.pages
filename: page.filename
}))
.filter(filterRootMarkdowns)
.filter((page: AutoSidebarPage) => !page.frontmatter.autoIgnore)

export const handleIgnorePages = (groupPages: GroupPagesResult, ignoreOptions: IgnoreOptions) => {
// console.log(groupPages)
ignoreOptions.forEach(({ menu, regex }) => {
const pages = groupPages[menu]

if (!pages) {
console.log(colors.red(`未匹配到路径 ${menu}`))
} else {
const re = regex ? RegExp(regex) : /.*/

const filterPages = pages.filter(page => !page.filename.match(re))

groupPages[menu] = filterPages
}
})
}

// 从所有 pages 中分类为待指定排序的和无需排序的
export const distinguishSpecifiedSortPages = (pages: AutoSidebarPage[]) => pages
Expand Down
Loading

0 comments on commit 24a954c

Please sign in to comment.