-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientPlugin.js
63 lines (56 loc) · 1.45 KB
/
clientPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { findPageByKey } from '@app/util'
import tagMap from '@dynamic/tag'
import categoryMap from '@dynamic/category'
class MetaFactory {
constructor (metaMap, pages) {
this._metaMap = Object.assign({}, metaMap);
Object.keys(this._metaMap).forEach(metaName => {
const { pageKeys } = this._metaMap[metaName];
this._metaMap[metaName].pages = pageKeys.map(key => findPageByKey(pages, key));
})
}
get length () {
return Object.keys(this._metaMap).length;
}
get map () {
return this._metaMap;
}
get list () {
return this.toArray();
}
toArray () {
const tags = [];
Object.keys(this._metaMap).forEach(name => {
const { pages, path } = this._metaMap[name];
tags.push({ pages, path, name })
})
return tags;
}
getItemByName (name) {
return this._metaMap[name];
}
}
export default ({ Vue }) => {
Vue.mixin({
computed: {
$tags () {
const { pages } = this.$site;
const tags = new MetaFactory(tagMap, pages);
return tags;
},
$categories () {
const { pages } = this.$site;
const categories = new MetaFactory(categoryMap, pages);
return categories;
},
$tag () {
const tagName = this.$route.meta.tagName;
return this.$tags.getItemByName(tagName);
},
$category () {
const categoryName = this.$route.meta.categoryName;
return this.$categories[categoryName];
}
}
})
}