From 6ab38f5ecf5a87a31ef54b9e5684ab162beb2abf Mon Sep 17 00:00:00 2001 From: mon_sat Date: Sat, 18 Jun 2022 17:12:08 +0900 Subject: [PATCH 1/4] feat: update VuePress version --- package.json | 2 +- src/node/index.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index a89111b..244e53d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/node/index.ts b/src/node/index.ts index 0674822..9105afb 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -1,4 +1,4 @@ -import type { Plugin, PluginObject, Page } from '@vuepress/core' +import type { Page, PluginObject } from '@vuepress/core' export interface UsePagesPluginOptions { startsWith?: string @@ -8,11 +8,11 @@ export interface UsePagesPluginOptions { file?: string } -const usePagesPlugin: Plugin = (options, app): PluginObject => { +export const usePagesPlugin = (options: UsePagesPluginOptions): PluginObject => { const name = 'vuepress-plugin-use-pages' const multiple = true - const onInitialized = () => { + const onPrepared: PluginObject['onPrepared'] = (app) => { const defaultSort = (a: Page, b: Page) => { if (!a.data.frontmatter.date || !b.data.frontmatter.date) { return 0 @@ -39,7 +39,7 @@ const usePagesPlugin: Plugin = (options, app): PluginObject => { return { name, multiple, - onInitialized, + onPrepared, } } From c50060f1cbd929ab740d3468cc9848ba10ac189f Mon Sep 17 00:00:00 2001 From: mon_sat Date: Sat, 18 Jun 2022 17:13:18 +0900 Subject: [PATCH 2/4] refactor: import plugin file --- src/node/index.ts | 45 ++------------------------------------ src/node/usePagesPlugin.ts | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 src/node/usePagesPlugin.ts diff --git a/src/node/index.ts b/src/node/index.ts index 9105afb..5cc4174 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -1,46 +1,5 @@ -import type { Page, PluginObject } 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 -} - -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, - } -} +export * from './usePagesPlugin' export default usePagesPlugin diff --git a/src/node/usePagesPlugin.ts b/src/node/usePagesPlugin.ts new file mode 100644 index 0000000..5876f9a --- /dev/null +++ b/src/node/usePagesPlugin.ts @@ -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, + } +} From cdb401a473d1567e8239867da598667775c035b6 Mon Sep 17 00:00:00 2001 From: mon_sat Date: Sat, 18 Jun 2022 17:39:19 +0900 Subject: [PATCH 3/4] fix: param is not required --- src/node/usePagesPlugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/usePagesPlugin.ts b/src/node/usePagesPlugin.ts index 5876f9a..cb0a965 100644 --- a/src/node/usePagesPlugin.ts +++ b/src/node/usePagesPlugin.ts @@ -8,7 +8,7 @@ export interface UsePagesPluginOptions { file?: string } -export const usePagesPlugin = (options: UsePagesPluginOptions): PluginObject => { +export const usePagesPlugin = (options?: UsePagesPluginOptions): PluginObject => { const name = 'vuepress-plugin-use-pages' const multiple = true From 3004552013affab456614c609225b82c94ddcaf5 Mon Sep 17 00:00:00 2001 From: mon_sat Date: Sun, 19 Jun 2022 10:03:19 +0900 Subject: [PATCH 4/4] docs: update for version 1.0.5 --- README.md | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e92b0d8..086c6e6 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 - }], + }), ], ``` @@ -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' }), ], ```