From d089f839b32ef60f5beb5effc249d6b0d511b4c4 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sun, 24 Feb 2019 23:50:10 +0800 Subject: [PATCH 1/3] better function transferring --- .../plugin-pagination/enhanceAppFile.js | 2 +- packages/@vuepress/plugin-pagination/index.js | 32 +++++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/@vuepress/plugin-pagination/enhanceAppFile.js b/packages/@vuepress/plugin-pagination/enhanceAppFile.js index 20bf67ed06..2cbc6198f3 100644 --- a/packages/@vuepress/plugin-pagination/enhanceAppFile.js +++ b/packages/@vuepress/plugin-pagination/enhanceAppFile.js @@ -1,4 +1,4 @@ -import paginationMeta from '@dynamic/pagination' +import * as paginationMeta from '@dynamic/pagination' class Pagination { constructor (pagination, { pages, route }) { diff --git a/packages/@vuepress/plugin-pagination/index.js b/packages/@vuepress/plugin-pagination/index.js index ba27c01b88..1274db273e 100644 --- a/packages/@vuepress/plugin-pagination/index.js +++ b/packages/@vuepress/plugin-pagination/index.js @@ -16,16 +16,16 @@ module.exports = (options, ctx) => ({ ], ready () { - let { postsFilter, postsSorter } = options - postsFilter = postsFilter || (({ type }) => type === 'post') - postsSorter = postsSorter || ((prev, next) => { + const { postsFilter, postsSorter } = options + ctx.postsFilter = postsFilter || (({ type }) => type === 'post') + ctx.postsSorter = postsSorter || ((prev, next) => { const prevTime = new Date(prev.frontmatter.date).getTime() const nextTime = new Date(next.frontmatter.date).getTime() return prevTime - nextTime > 0 ? -1 : 1 }) const { pages } = ctx - const posts = pages.filter(postsFilter) + const posts = pages.filter(ctx.postsFilter) const { perPagePosts = 10, paginationDir = 'page', @@ -34,19 +34,14 @@ module.exports = (options, ctx) => ({ } = options const intervallers = getIntervallers(posts.length, perPagePosts) - const pagination = { - paginationPages: intervallers.map((interval, index) => { - const path = index === 0 - ? firstPagePath - : `/${paginationDir}/${index + 1}/` - return { path, interval } - }), - postsFilter: postsFilter.toString(), - postsSorter: postsSorter.toString() - } + ctx.paginationPages = intervallers.map((interval, index) => { + const path = index === 0 + ? firstPagePath + : `/${paginationDir}/${index + 1}/` + return { path, interval } + }) - ctx.pagination = pagination - pagination.paginationPages.forEach(({ path }, index) => { + ctx.paginationPages.forEach(({ path }, index) => { if (path === '/') { return } @@ -63,7 +58,10 @@ module.exports = (options, ctx) => ({ async clientDynamicModules () { return { name: 'pagination.js', - content: `export default ${JSON.stringify(ctx.pagination, null, 2)}` + content: `\ +export const paginationPages = ${JSON.stringify(ctx.paginationPages, null, 2)} +export const postsFilter = ${ctx.postsFilter} +export const postsSorter = ${ctx.postsSorter}` } } }) From e4eebbf225d975a61bb75a32d319d71a8af65942 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 25 Feb 2019 09:20:59 +0800 Subject: [PATCH 2/3] remove eval --- packages/@vuepress/plugin-pagination/enhanceAppFile.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/@vuepress/plugin-pagination/enhanceAppFile.js b/packages/@vuepress/plugin-pagination/enhanceAppFile.js index 2cbc6198f3..b758120435 100644 --- a/packages/@vuepress/plugin-pagination/enhanceAppFile.js +++ b/packages/@vuepress/plugin-pagination/enhanceAppFile.js @@ -2,14 +2,8 @@ import * as paginationMeta from '@dynamic/pagination' class Pagination { constructor (pagination, { pages, route }) { - let { postsFilter, postsSorter } = pagination - - /* eslint-disable no-eval */ - postsFilter = eval(postsFilter) - postsSorter = eval(postsSorter) - + const { postsFilter, postsSorter, paginationPages } = pagination const { path } = route - const { paginationPages } = pagination paginationPages.forEach((page, index) => { if (page.path === path) { From 0b982ba4c7568dbb53fa89752bd25037a98f4380 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Tue, 26 Feb 2019 21:25:05 +0800 Subject: [PATCH 3/3] fix edge case --- packages/@vuepress/plugin-pagination/index.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/@vuepress/plugin-pagination/index.js b/packages/@vuepress/plugin-pagination/index.js index 1274db273e..d6a06eeabf 100644 --- a/packages/@vuepress/plugin-pagination/index.js +++ b/packages/@vuepress/plugin-pagination/index.js @@ -60,8 +60,23 @@ module.exports = (options, ctx) => ({ name: 'pagination.js', content: `\ export const paginationPages = ${JSON.stringify(ctx.paginationPages, null, 2)} -export const postsFilter = ${ctx.postsFilter} -export const postsSorter = ${ctx.postsSorter}` +export const postsFilter = ${stringifyFunction(ctx.postsFilter)} +export const postsSorter = ${stringifyFunction(ctx.postsSorter)}` } } }) + +function stringifyFunction (input) { + let output = String(input) + if (!/^(function\b|\()/.test(output)) { + /** + * fix edge case: + * ```js + * const foo = { bar () {} } + * stringifyFunction(foo.bar) + * ``` + */ + output = output.replace(/^[^(]+/, 'function') + } + return output +}