Skip to content

fix($plugin-pagination): better function transfer (close: #1338) #1345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions packages/@vuepress/plugin-pagination/enhanceAppFile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import paginationMeta from '@dynamic/pagination'
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) {
Expand Down
47 changes: 30 additions & 17 deletions packages/@vuepress/plugin-pagination/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
}
Expand All @@ -63,7 +58,25 @@ 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 = ${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
}