Skip to content

Commit

Permalink
Merge pull request #2 from monsat/fix-vuepress-update
Browse files Browse the repository at this point in the history
Update for VuePress 2.0.0-beta.40
  • Loading branch information
monsat authored Jun 19, 2022
2 parents a3e0a9d + 3004552 commit 0bb17aa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 50 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ $ yarn add -D vuepress-plugin-use-pages

Add `vuepress-plugin-use-pages` in your config file.

> Since version 1.0.5 is for VuePress 2.0.0-beta.40 or later.
> Usage of plugins is changed.
```ts:docs/.vuepress/config.ts
import { defineUserConfig } from 'vuepress'
import { viteBundler } from 'vuepress'
import { usePagesPlugin } from 'vuepress-plugin-use-pages'

export default defineUserConfig({
bundler: viteBundler(),
plugins: [
['vuepress-plugin-use-pages'],
usePagesPlugin(),
],
})
```

in your doc file or component.
Expand Down Expand Up @@ -62,13 +72,13 @@ export default defineComponent({

```ts:docs/.vuepress/config.ts
plugins: [
['vuepress-plugin-use-pages', {
usePagesPlugin({
startsWith: '/articles/', // fetch only matched paths
filter: (page) => page.data.lang === 'ja', // fetch only filtered pages
sort: (a, b) => b.data.git.updatedTime - a.data.git.updatedTime
limit: 30, // maximum cached size
file: 'articles.js', // temp file name
}],
}),
],
```

Expand Down Expand Up @@ -110,9 +120,9 @@ This plugin is usable for multiple times.

```ts:docs/.vuepress/config.ts
plugins: [
['vuepress-plugin-use-pages', { startsWith: '/articles/', file: 'articles.js' }],
['vuepress-plugin-use-pages', { startsWith: '/posts/', file: 'posts.js' }],
['vuepress-plugin-use-pages', { startsWith: '/pages/', file: 'pages.js' }],
usePagesPlugin({ startsWith: '/articles/', file: 'articles.js' }),
usePagesPlugin({ startsWith: '/posts/', file: 'posts.js' }),
usePagesPlugin({ startsWith: '/pages/', file: 'pages.js' }),
],
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepare": "npm run build"
},
"dependencies": {
"@vuepress/core": "^2.0.0-beta.27"
"@vuepress/core": "^2.0.0-beta.48"
},
"devDependencies": {
"typescript": "^4.4.4"
Expand Down
45 changes: 2 additions & 43 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,5 @@
import type { Plugin, PluginObject, Page } from '@vuepress/core'
import { usePagesPlugin } from './usePagesPlugin'

export interface UsePagesPluginOptions {
startsWith?: string
filter?: (page: Page) => boolean
sort?: (a: Page, b: Page) => number
limit?: number | false
file?: string
}

const usePagesPlugin: Plugin = (options, app): PluginObject => {
const name = 'vuepress-plugin-use-pages'
const multiple = true

const onInitialized = () => {
const defaultSort = (a: Page, b: Page) => {
if (!a.data.frontmatter.date || !b.data.frontmatter.date) {
return 0
}
return (new Date(b.data.frontmatter.date).getTime()) - (new Date(a.data.frontmatter.date).getTime())
}
const {
startsWith = '/articles/',
filter,
sort = defaultSort,
limit = false,
file = 'pages.js',
}: UsePagesPluginOptions = options || {}

const docs = app.pages.filter(p => p.data.path.startsWith(startsWith))
const filtered = filter ? docs.filter(filter) : docs
filtered.sort(sort) // Sorted in place
const limited = limit ? filtered.slice(0, limit) : filtered
const pageData = limited.map(p => p.data)
const content = `export const usePages = () => ${JSON.stringify(pageData)}`
app.writeTemp(file, content)
}

return {
name,
multiple,
onInitialized,
}
}
export * from './usePagesPlugin'

export default usePagesPlugin
44 changes: 44 additions & 0 deletions src/node/usePagesPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Page, PluginObject } from '@vuepress/core'

export interface UsePagesPluginOptions {
startsWith?: string
filter?: (page: Page) => boolean
sort?: (a: Page, b: Page) => number
limit?: number | false
file?: string
}

export const usePagesPlugin = (options?: UsePagesPluginOptions): PluginObject => {
const name = 'vuepress-plugin-use-pages'
const multiple = true

const onPrepared: PluginObject['onPrepared'] = (app) => {
const defaultSort = (a: Page, b: Page) => {
if (!a.data.frontmatter.date || !b.data.frontmatter.date) {
return 0
}
return (new Date(b.data.frontmatter.date).getTime()) - (new Date(a.data.frontmatter.date).getTime())
}
const {
startsWith = '/articles/',
filter,
sort = defaultSort,
limit = false,
file = 'pages.js',
}: UsePagesPluginOptions = options || {}

const docs = app.pages.filter(p => p.data.path.startsWith(startsWith))
const filtered = filter ? docs.filter(filter) : docs
filtered.sort(sort) // Sorted in place
const limited = limit ? filtered.slice(0, limit) : filtered
const pageData = limited.map(p => p.data)
const content = `export const usePages = () => ${JSON.stringify(pageData)}`
app.writeTemp(file, content)
}

return {
name,
multiple,
onPrepared,
}
}

0 comments on commit 0bb17aa

Please sign in to comment.