|
1 | 1 | const { path } = require('@vuepress/shared-utils') |
2 | 2 |
|
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 => { |
4 | 32 | const { |
5 | 33 | readingDir = null, |
6 | 34 | fixed = 'top' |
7 | 35 | } = options |
8 | 36 |
|
9 | 37 | 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 |
17 | 53 | }, |
18 | 54 | enhanceAppFiles: [ |
19 | 55 | path.resolve(__dirname, 'enhanceAppFile.js') |
|
0 commit comments