forked from tea3/hexo-related-popular-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·182 lines (162 loc) · 7.66 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const pathFn = require('path')
const fs = require('hexo-fs')
const util = require('./lib/util.js')
const dr = require('./lib/dateRange.js')
const su = require('./lib/settingsUpdate.js')
const lg = require('./lib/log.js')
const pjson = require('./package.json')
const shash = su.chkUpdate(hexo.config)
const ngwHash = su.chkUpdate_ngw(hexo.config)
// option
let gaData = null
let dateRangeArr = null
let dateRange = 30
let id = null
let email = null
let key = null
let viewId = null
let cache_path = null
let rankingSheet = null
let cache_exexpires = 10
let pvMeasurementsStartDate = ''
let weight_of_tag_relevancy = 1.0
let weight_of_contents_relevancy = 1.0
let isLog = true
// get setting from _config.yml
if ( hexo.config.popularPosts ) {
if ( hexo.config.popularPosts.googleAnalyticsAPI ) {
// Deprecated message
if ( hexo.config.popularPosts.googleAnalyticsAPI.cache && ( hexo.config.popularPosts.googleAnalyticsAPI.cache.path != undefined || hexo.config.popularPosts.googleAnalyticsAPI.cache.expiresDate != undefined ) ) {
lg.log('error', '(Deprecated option) \'googleAnalyticsAPI.cache.path\' and \'googleAnalyticsAPI.cache.expiresDate\' are deprecated. Please set the \'cache.path\' and \'googleAnalyticsAPI.expiresDate\' option.', '_config.yml', true)
return
}
// google analytics option
if ( hexo.config.popularPosts.googleAnalyticsAPI.dateRange) {
dateRange = Number(hexo.config.popularPosts.googleAnalyticsAPI.dateRange)
}
if ( hexo.config.popularPosts.googleAnalyticsAPI.clientId && hexo.config.popularPosts.googleAnalyticsAPI.serviceEmail && hexo.config.popularPosts.googleAnalyticsAPI.key && hexo.config.popularPosts.googleAnalyticsAPI.viewId) {
id = hexo.config.popularPosts.googleAnalyticsAPI.clientId
email = hexo.config.popularPosts.googleAnalyticsAPI.serviceEmail
key = pathFn.join(process.env.PWD || process.cwd(), hexo.config.popularPosts.googleAnalyticsAPI.key)
viewId = 'ga:' + hexo.config.popularPosts.googleAnalyticsAPI.viewId
} else if ( process.env.GOOGLEAPI_CLIENTID && process.env.GOOGLEAPI_EMAIL && process.env.GOOGLEAPI_KEY && process.env.GOOGLEAPI_ANALYTICS_TABLE ) {
id = process.env.GOOGLEAPI_CLIENTID
email = process.env.GOOGLEAPI_EMAIL
key = process.env.GOOGLEAPI_KEY
viewId = process.env.GOOGLEAPI_ANALYTICS_TABLE
} else {
lg.log('error', 'Please set the googleAnalyticsAPI options or environment variables.', '_config.yml', true)
return
}
if ( hexo.config.popularPosts.googleAnalyticsAPI.pvMeasurementsStartDate != undefined) {
let pvmstd = String(hexo.config.popularPosts.googleAnalyticsAPI.pvMeasurementsStartDate)
pvmstd = dr.getDateStrFromDate( new Date(pvmstd) )
if (pvmstd.match(/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/)) {
pvMeasurementsStartDate = pvmstd
} else {
lg.log('error', 'Please check the \'pvMeasurementsStartDate\' option. This option should be written in the form \'YYYY-MM-DD\' .', '_config.yml', true)
return
}
}
if ( hexo.config.popularPosts.googleAnalyticsAPI.expiresDate != undefined) {
cache_exexpires = Number(hexo.config.popularPosts.googleAnalyticsAPI.expiresDate)
}
if ( hexo.config.popularPosts.googleAnalyticsAPI.rankingSheet && hexo.config.popularPosts.googleAnalyticsAPI.rankingSheet) {
rankingSheet = pathFn.join(process.env.PWD || process.cwd(), hexo.config.popularPosts.googleAnalyticsAPI.rankingSheet)
}
}
// related posts weight option
if ( hexo.config.popularPosts.weight ) {
if ( hexo.config.popularPosts.weight.tagRelevancy != undefined) {
weight_of_tag_relevancy = Number(hexo.config.popularPosts.weight.tagRelevancy)
}
if ( hexo.config.popularPosts.weight.contentsRelevancy != undefined) {
weight_of_contents_relevancy = Number(hexo.config.popularPosts.weight.contentsRelevancy)
}
}
// cache option
if ( hexo.config.popularPosts.cache && hexo.config.popularPosts.cache.path) {
cache_path = pathFn.join(process.env.PWD || process.cwd(), hexo.config.popularPosts.cache.path)
}
// log option
if ( hexo.config.popularPosts.log != undefined) {
isLog = hexo.config.popularPosts.log
}
}
dateRangeArr = dr.getDateRange(dateRange)
// orverride config.popularPosts
hexo.config.popularPosts = Object.assign( {},
hexo.config.popularPosts, {
'tmp': {
'isLog': isLog,
'negativewordsUpdate': ngwHash,
'cacheUpdate': '',
'isNgwUpdate': true,
'isGaUpdate': true,
'settingsUpdate': shash,
'version': pjson.version,
'dateRange': dateRange,
'id': id,
'email': email,
'key': key,
'viewId': viewId,
'cache_path': cache_path,
'rankingSheet': rankingSheet,
'cache_exexpires': cache_exexpires,
'pvMeasurementsStartDate': pvMeasurementsStartDate,
'old_cacheDate': '',
'weight_of_tag_relevancy': weight_of_tag_relevancy,
'weight_of_contents_relevancy': weight_of_contents_relevancy,
'startDate': dateRangeArr[0],
'endDate': dateRangeArr[1],
'gaData': [],
'postPath': [],
},
}
)
lg.setConfig(hexo.config)
// load cache data
if (cache_path && fs.existsSync(cache_path)) {
const ndt = new Date().getTime()
let gaDataStr = fs.readFileSync(cache_path)
gaData = JSON.parse(gaDataStr)
hexo.config.popularPosts.tmp.cacheUpdate = su.getMD5(gaDataStr)
// check update of morphologicalAnalysis's negativeKeywordsList
let isNgwUpdate = !gaData[0].ngwHash || gaData[0].ngwHash != ngwHash
hexo.config.popularPosts.tmp.isNgwUpdate = isNgwUpdate
// check the cache format version and hash data
if (gaData[0].version && gaData[0].version == pjson.version && gaData[0].hash && gaData[0].hash == shash) {
if (gaData[0].cachedDate && (ndt - gaData[0].cachedDate) < cache_exexpires*24*60*60*1000 ) {
hexo.config.popularPosts.tmp.isGaUpdate = false
}
hexo.config.popularPosts.tmp.old_cacheDate = gaData[0].cachedDate
} else {
gaData = null
}
if (gaData) {
util.orverrideTmp(gaData[0].gaData, hexo)
} else {
util.orverrideTmp([], hexo)
}
} else if (!cache_path) {
hexo.config.popularPosts.tmp.isNgwUpdate = false
hexo.config.popularPosts.tmp.isGaUpdate = false
}
hexo.extend.filter.register('after_init', () => {
return require('./lib/googleAnalytics')(hexo)
}, {async: true})
hexo.extend.filter.register('after_post_render', ( post ) => {
return require('./lib/collector')(post, hexo)
}, {async: true})
hexo.extend.filter.register('after_generate', () => {
return require('./lib/cache')(hexo)
})
hexo.extend.helper.register('popular_posts', (options, post) => {
return require('./lib/helper')(post, options, hexo)
})
hexo.extend.helper.register('popular_posts_json', (options, post) => {
return require('./lib/helper-json')(post, options, hexo)
})
hexo.extend.helper.register('popular_posts_pv', (post) => {
return require('./lib/pv')(post, hexo)
})