This repository has been archived by the owner on Oct 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.js
108 lines (92 loc) · 2.95 KB
/
json.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const _ = require('lodash')
const pathFn = require('path')
function jsonify(data) {
const { config, logger, fs } = this
const { json, public_dir: publicDir, base } = config
if (!json) {
return data
}
const path = json.path || 'json'
const {
posts,
pages,
categories,
tags,
index,
tag,
category,
} = data
const result = { categories: {}, tags: {} }
function getPostById(id) {
let post = _.cloneDeep(posts).find(p => p.id === id)
if (post) {
post = _.omit(post, ['raw', 'content', 'path', 'url', 'prev', 'next'])
post.category = _.pick(post.category, ['id', 'name'])
post.tags = post.tags.map(t => _.pick(t, ['id', 'name']))
return post
}
return ''
}
function output(jsonPath, jsonData) {
fs.outputFileSync(pathFn.join(base, publicDir, path, jsonPath), JSON.stringify(jsonData))
logger.success(jsonPath)
}
result.config = Object.assign(_.omit(config, ['token', 'base', 'cache']), {
posts: posts.map(post => post.id),
pages: pages.map(page => _.pick(page, ['id', 'url'])),
tags: tags.map(item => ({
id: item.id,
name: item.name,
count: item.posts.length,
})),
categories: categories.map(item => ({
id: item.id,
name: item.name,
count: item.posts.length,
})),
})
result.posts = posts.map((p) => {
const post = _.omit(p, ['raw', 'excerpt', 'path', 'url'])
post.prev = getPostById(post.prev)
post.next = getPostById(post.next)
post.category = _.pick(post.category, ['id', 'name'])
post.tags = post.tags.map(t => _.pick(t, ['id', 'name']))
return post
})
result.pages = pages.map(page => _.omit(page, ['path', 'raw']))
result.index = index.map(page => page.posts.map(id => getPostById(id)))
Object.keys(category).forEach((key) => {
result.categories[key] = category[key].map(page => ({
id: +page.base.split('/').slice(-1),
title: page.title,
current: page.current,
total: page.total,
posts: page.posts.map(id => getPostById(id)),
}))
})
Object.keys(tag).forEach((key) => {
result.tags[key] = tag[key].map(page => ({
id: +page.base.split('/').slice(-1),
title: page.title,
current: page.current,
total: page.total,
posts: page.posts.map(id => getPostById(id)),
}))
})
output('config.json', result.config)
result.pages.forEach(p => output(`pages/${p.id}.json`, p))
result.posts.forEach(p => output(`posts/${p.id}.json`, p))
result.index.forEach((p, i) => output(`page/${i + 1}.json`, p))
Object.keys(result.categories).forEach((c) => {
const current = result.categories[c]
const dir = `categories/${current[0].id}`
current.forEach((p, i) => output(`${dir}/${i + 1}.json`, p))
})
Object.keys(result.tags).forEach((t) => {
const current = result.tags[t]
const dir = `tags/${current[0].id}`
current.forEach((p, i) => output(`${dir}/${i + 1}.json`, p))
})
return data
}
module.exports = jsonify