Skip to content

Commit 15c23d3

Browse files
committed
feat: add RegExp for readingDir
1 parent ed0dc0d commit 15c23d3

File tree

6 files changed

+63
-60
lines changed

6 files changed

+63
-60
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[README](README.md) | [CHANGELOG](CHANGELOG.md)
44

5+
## 1.0.8
6+
7+
- add RegExp for readingDir
8+
59
## 1.0.7
610

711
- fix bug

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
55
[README](README.md) | [CHANGELOG](CHANGELOG.md)
66

7-
**This plugin is for Vuepress 1.x which is currently in alpha**
8-
97
add reading progress bar for vuepress
108

119
![vuepress-plugin-reading-progress](https://ououe.com/img/vuepress_plugin_reading_progress.gif)
@@ -16,7 +14,7 @@ add reading progress bar for vuepress
1614

1715
``` sh
1816
yarn add vuepress-plugin-reading-progress
19-
// or
17+
# or
2018
npm i vuepress-plugin-reading-progress
2119
```
2220

@@ -36,7 +34,7 @@ module.exports = {
3634
## Options
3735

3836
### readingDir
39-
- Type: `null`, `string`, `array`, `Object`
37+
- Type: `null`, `string`, `array`, `object`, `RegExp`
4038
- Default: `null`
4139

4240
Specify folders that display reading progress bar
@@ -53,6 +51,9 @@ example
5351
posts2: 'bottom',
5452
posts3: 'left'
5553
}
54+
// or RegExp
55+
readingDir: /[^\/]+$/ // exclude regularPath ending with /
56+
readingDir: new RegExp('[^\/]+$')
5657
}
5758
```
5859

ReadingProgress.vue

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ export default {
2323
this.base()
2424
},
2525
beforeDestroy () {
26-
window.removeEventListener('scroll', () => {
27-
this.getReadingBase()
28-
})
26+
this.$readingShow && window.removeEventListener('scroll', this.getReadingBase)
2927
},
3028
methods: {
3129
base () {
3230
if (this.$readingShow) {
3331
this.transform = this.getTransform()
3432
this.progressStyle = this.getProgressStyle()
35-
window.addEventListener('scroll', () => {
36-
this.getReadingBase()
37-
}, 200)
33+
window.addEventListener('scroll', this.getReadingBase, 200)
3834
}
3935
},
4036
getReadingBase () {
@@ -68,15 +64,15 @@ export default {
6864
case 'top':
6965
case 'bottom':
7066
if (this.transform[0]) {
71-
return `${this.transform[0]}: translate(${progress * 100 / 2 - 50}%, 0) scale(${progress}, 1)`
67+
return `${this.transform[0]}: scaleX(${progress})`
7268
} else {
7369
return `width: ${progress * 100}%`
7470
}
7571
break
7672
case 'left':
7773
case 'right':
7874
if (this.transform[0]) {
79-
return `${this.transform[0]}: translate(0, ${progress * 100 / 2 - 50}%) scale(1, ${progress})`
75+
return `${this.transform[0]}: scaleY(${progress})`
8076
} else {
8177
return `height: ${progress * 100}%`
8278
}
@@ -110,6 +106,7 @@ $readingProgressImage ?= none
110106
height 100%
111107
background $readingProgressColor
112108
background-image $readingProgressImage
109+
transform-origin 0% 0%
113110
transition: transform .2s ease-out
114111
.top
115112
top 0

enhanceAppFile.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,11 @@
11
import ReadingProgress from './ReadingProgress.vue'
2-
import config from '@dynamic/readingProgress.js'
3-
4-
const setConfig = (regularPath, dir, fixed) => {
5-
return regularPath.includes(dir) ? fixed : false
6-
}
72

83
export default ({ Vue }) => {
94
Vue.component('ReadingProgress', ReadingProgress)
105
Vue.mixin({
116
computed: {
12-
$readingShow () {
13-
const { readingDir, fixed } = config
14-
const regularPath = this.$page.regularPath
15-
16-
if (this.$frontmatter.readingShow !== undefined) {
17-
const readingShow = this.$frontmatter.readingShow
18-
return typeof readingShow === 'boolean' && readingShow
19-
? 'top'
20-
: readingShow
21-
} else if (regularPath) {
22-
if (readingDir === null) {
23-
return fixed
24-
} else if (typeof readingDir === 'string') {
25-
return setConfig(regularPath, readingDir, fixed)
26-
} else if (Array.isArray(readingDir)) {
27-
for (let i = 0; i < readingDir.length; i++) {
28-
const item = readingDir[i]
29-
const isShow = setConfig(regularPath, item, fixed)
30-
if (isShow) return fixed
31-
}
32-
return false
33-
} else {
34-
for (const key in readingDir) {
35-
const item = readingDir[key]
36-
const isShow = setConfig(regularPath, key, item)
37-
if (isShow) return item
38-
}
39-
return false
40-
}
41-
} else {
42-
return false
43-
}
7+
$readingShow() {
8+
return this.$page.frontmatter.readingShow
449
}
4510
}
4611
})

index.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
11
const { path } = require('@vuepress/shared-utils')
22

3-
module.exports = (options, ctx) => {
3+
function checkRegularPath(regularPath, readingDir, fixed) {
4+
if (readingDir === null) {
5+
return fixed
6+
} else if (typeof readingDir === 'string') {
7+
return setConfig(regularPath, readingDir, fixed)
8+
} else if (Array.isArray(readingDir)) {
9+
for (let i = 0; i < readingDir.length; i++) {
10+
const item = readingDir[i]
11+
const isShow = setConfig(regularPath, item, fixed)
12+
if (isShow) return fixed
13+
}
14+
return false
15+
} else if (readingDir.constructor === RegExp) {
16+
return readingDir.test(regularPath) ? fixed : false
17+
} else {
18+
for (const key in readingDir) {
19+
const item = readingDir[key]
20+
const isShow = setConfig(regularPath, key, item)
21+
if (isShow) return item
22+
}
23+
return false
24+
}
25+
}
26+
27+
function setConfig(regularPath, dir, fixed) {
28+
return regularPath.includes(dir) ? fixed : false
29+
}
30+
31+
module.exports = options => {
432
const {
533
readingDir = null,
634
fixed = 'top'
735
} = options
836

937
return {
10-
async clientDynamicModules () {
11-
return [
12-
{
13-
name: 'readingProgress.js',
14-
content: `export default ${JSON.stringify({ readingDir, fixed })}`
15-
}
16-
]
38+
extendPageData($page) {
39+
const { regularPath, frontmatter: { readingShow }} = $page
40+
let type = fixed
41+
42+
if (readingShow !== undefined) {
43+
type = typeof readingShow === 'boolean' && readingShow
44+
? fixed
45+
: readingShow
46+
} else if (regularPath) {
47+
type = checkRegularPath(regularPath, readingDir, fixed)
48+
} else {
49+
type = false
50+
}
51+
52+
$page.frontmatter.readingShow = type
1753
},
1854
enhanceAppFiles: [
1955
path.resolve(__dirname, 'enhanceAppFile.js')

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuepress-plugin-reading-progress",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"main": "index.js",
55
"description": "A reading progress bar plugin for VuePress",
66
"author": "tolking <qw13131wang@gmail.com>",
@@ -20,7 +20,7 @@
2020
"progress"
2121
],
2222
"devDependencies": {
23-
"@vuepress/shared-utils": "^1.0.0-alpha.39",
24-
"vuepress": "^1.0.0-alpha.39"
23+
"@vuepress/shared-utils": "^1.0.0",
24+
"vuepress": "^1.0.0"
2525
}
2626
}

0 commit comments

Comments
 (0)