Skip to content
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

feat($plugin-blog): refine blog plugin [WIP] #1180

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
5 changes: 4 additions & 1 deletion packages/@vuepress/core/lib/node/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ module.exports = class Page {
relative,
permalink,
frontmatter = {},
permalinkPattern
permalinkPattern,
...rest
}, context) {
this.title = title
this._meta = meta
Expand All @@ -56,6 +57,8 @@ module.exports = class Page {
this._permalinkPattern = permalinkPattern
this._context = context

Object.assign(this, rest)

if (relative) {
this.regularPath = encodeURI(fileToPath(relative))
} else if (_path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { findPageByKey } from '@app/util'
import tagMeta from '@dynamic/tag'
import categoryMeta from '@dynamic/category'
import frontmatterClassifiedPageMap from '@dynamic/vuepress_blog/frontmatterClassified'

class Classifiable {
constructor (metaMap, pages) {
Expand Down Expand Up @@ -38,26 +37,28 @@ class Classifiable {
}

export default ({ Vue }) => {
Vue.mixin({
computed: {
$tags () {
const { pages } = this.$site
const tags = new Classifiable(tagMeta, pages)
return tags
},
$tag () {
const tagName = this.$route.meta.tagName
return this.$tags.getItemByName(tagName)
},
$categories () {
const { pages } = this.$site
const categories = new Classifiable(categoryMeta, pages)
return categories
},
$category () {
const categoryName = this.$route.meta.categoryName
return this.$categories.getItemByName(categoryName)
const computed = Object.keys(frontmatterClassifiedPageMap)
.map(classifiedType => {
const map = frontmatterClassifiedPageMap[classifiedType]
const helperName = `$${classifiedType}`
return {
[helperName] () {
const { pages } = this.$site
const classified = new Classifiable(map, pages)
return classified
},
[`$current${classifiedType.charAt(0).toUpperCase() + classifiedType.slice(1)}`] () {
const tagName = this.$route.meta.frontmatterClassificationKey
return this[helperName].getItemByName(tagName)
}
}
}
})
.reduce((map, item) => {
Object.assign(map, item)
return map
}, {})

Vue.mixin({
computed
})
}
110 changes: 110 additions & 0 deletions packages/@vuepress/plugin-blog/client/pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import Vue from 'vue'
import paginations from '@dynamic/vuepress_blog/paginations'
import frontmatterClassifications from '@dynamic/vuepress_blog/frontmatterClassifications'
import pageFilters from '@dynamic/vuepress_blog/pageFilters'
import pageSorters from '@dynamic/vuepress_blog/pageSorters'
import _debug from 'debug'

const debug = _debug('plugin-blog:pagination')

function getClientFrontmatterPageFilter (rawFilter, pid, value) {
// debug('getClientFrontmatterPageFilter')
// debug('frontmatterClassifications', frontmatterClassifications)
// debug('pid', pid)
const match = frontmatterClassifications.filter(i => i.id === pid)[0]
return page => rawFilter(page, match && match.keys, value)
}

class PaginationGateway {
constructor (paginations) {
this.paginations = paginations
}

get pages () {
return Vue.$vuepress.$get('siteData').pages
}

getPagination (pid, id, route) {
debug('id', id)
debug('this.paginations', this.paginations)
const pagnination = this.paginations.filter(p => p.id === id && p.pid === pid)[0]
return new Pagination(pagnination, this.pages, route)
}
}

const gateway = new PaginationGateway(paginations)

class Pagination {
constructor (pagination, pages, route) {
debug(pagination)
const { pid, id, paginationPages } = pagination

const pageFilter = getClientFrontmatterPageFilter(pageFilters[pid], pid, id)
const pageSorter = pageSorters[pid]

const { path } = route

for (let i = 0, l = paginationPages.length; i < l; i++) {
const page = paginationPages[i]
if (page.path === path) {
this.paginationIndex = i
break
}
}

if (!this.paginationIndex) {
this.paginationIndex = 0
}

this._paginationPages = paginationPages
this._currentPage = paginationPages[this.paginationIndex]
this._matchedPages = pages.filter(pageFilter).sort(pageSorter)
}

setIndexPage (path) {
this._indexPage = path
}

get length () {
return this._paginationPages.length
}

get pages () {
const [start, end] = this._currentPage.interval
return this._matchedPages.slice(start, end + 1)
}

get hasPrev () {
return this.paginationIndex !== 0
}

get prevLink () {
if (this.hasPrev) {
if (this.paginationIndex - 1 === 0 && this._indexPage) {
return this._indexPage
}
return this._paginationPages[this.paginationIndex - 1].path
}
}

get hasNext () {
return this.paginationIndex !== this.length - 1
}

get nextLink () {
if (this.hasNext) {
return this._paginationPages[this.paginationIndex + 1].path
}
}
}

export default ({ Vue }) => {
Vue.mixin({
methods: {
$pagination (pid, id) {
id = id || pid
return gateway.getPagination(pid, id, this.$route)
}
}
})
}
26 changes: 26 additions & 0 deletions packages/@vuepress/plugin-blog/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface Pagination {
postsFilter?: typeof Array.prototype.filter
postsSorter?: typeof Array.prototype.sort
perPagePosts?: number
layout?: string
}

export interface DirectoryClassification {
id: string;
dirname: string;
path: string;
layout?: string;
itemLayout?: string;
itemPermalink?: string;
pagination?: Pagination;
}

export interface FrontmatterClassification {
id: string;
keys: string[];
path: string;
layout?: string;
frontmatter?: Record<string, any>;
itemlayout?: string;
pagination?: Pagination;
}
Loading