1
|
|
|
/* global hexo, __dirname */ |
2
|
|
|
|
3
|
|
|
'use strict'; |
4
|
|
|
|
5
|
|
|
const fs = require('hexo-fs'); |
6
|
|
|
const path = require('path'); |
7
|
|
|
const CryptoJS = require('crypto-js'); |
8
|
|
|
const log = require('hexo-log')({ |
9
|
|
|
'debug': false, |
10
|
|
|
'silent': false, |
11
|
|
|
}); |
12
|
|
|
|
13
|
|
|
hexo.extend.filter.register('after_post_render', function encrypt (data) { |
14
|
|
|
|
15
|
|
|
// Close the encrypt function |
16
|
|
|
if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) { |
17
|
|
|
|
18
|
|
|
return data; |
19
|
|
|
|
20
|
|
|
} |
21
|
|
|
if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template |
22
|
|
|
|
23
|
|
|
hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html')); |
24
|
|
|
|
25
|
|
|
} |
26
|
|
|
if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info |
27
|
|
|
|
28
|
|
|
hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>'; |
29
|
|
|
|
30
|
|
|
} |
31
|
|
|
if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message |
32
|
|
|
|
33
|
|
|
hexo.config.encrypt.default_message = 'Please enter the password to read the blog.'; |
34
|
|
|
|
35
|
|
|
} |
36
|
|
|
if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // Wrong password |
37
|
|
|
|
38
|
|
|
hexo.config.encrypt.default_decryption_error = 'Incorrect Password!'; |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // No content |
42
|
|
|
|
43
|
|
|
hexo.config.encrypt.default_no_content_error = 'No content to display!'; |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ('password' in data && data.password) { |
48
|
|
|
|
49
|
|
|
// Use the blog's config first |
50
|
|
|
log.info(`Encrypted the blog: ${ data.title.trim() }`); |
51
|
|
|
|
52
|
|
|
// Store the origin data |
53
|
|
|
data.origin = data.content; |
54
|
|
|
data.encrypt = true; |
55
|
|
|
|
56
|
|
|
if (!('abstract' in data && data.abstract)) { |
57
|
|
|
|
58
|
|
|
data.abstract = hexo.config.encrypt.default_abstract; |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
if (!('template' in data && data.template)) { |
62
|
|
|
|
63
|
|
|
data.template = hexo.config.encrypt.default_template; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
if (!('message' in data && data.message)) { |
67
|
|
|
|
68
|
|
|
data.message = hexo.config.encrypt.default_message; |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
if (!('decryptionError' in data && data.decryptionError)) { |
72
|
|
|
|
73
|
|
|
data.decryptionError = hexo.config.encrypt.default_decryption_error; |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
if (!('noContentError' in data && data.noContentError)) { |
77
|
|
|
|
78
|
|
|
data.noContentError = hexo.config.encrypt.default_no_content_error; |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
data.content = escape(data.content); |
83
|
|
|
data.content = CryptoJS.enc.Utf8.parse(data.content); |
84
|
|
|
data.content = CryptoJS.enc.Base64.stringify(data.content); |
85
|
|
|
data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString(); |
86
|
|
|
|
87
|
|
|
data.template = data.template.replace('{{content}}', data.content); |
88
|
|
|
data.template = data.template.replace('{{message}}', data.message); |
89
|
|
|
data.template = data.template.replace('{{message}}', data.message); |
90
|
|
|
data.template = data.template.replace('{{decryptionError}}', data.decryptionError); |
91
|
|
|
data.template = data.template.replace('{{noContentError}}', data.noContentError); |
92
|
|
|
|
93
|
|
|
data.content = data.template; |
94
|
|
|
data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script>`; |
95
|
|
|
data.content += `<script src="${hexo.config.root}lib/blog-encrypt.js"></script>`; |
96
|
|
|
data.content += `<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`; |
97
|
|
|
|
98
|
|
|
data.more = data.abstract; |
99
|
|
|
data.excerpt = data.more; |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
return data; |
103
|
|
|
|
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
hexo.extend.generator.register('blog-encrypt', () => [ |
107
|
|
|
{ |
108
|
|
|
'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')), |
109
|
|
|
'path': 'lib/crypto-js.js', |
110
|
|
|
}, { |
111
|
|
|
'data': function() { |
112
|
|
|
const Readable = require('stream').Readable; |
113
|
|
|
let stream = new Readable; |
114
|
|
|
stream.push(fs.readFileSync(path.resolve(__dirname, 'lib/blog-encrypt.js')) |
115
|
|
|
.replace('{callback}', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : '')); |
116
|
|
|
stream.push(null); // indicates the end of the stream |
117
|
|
|
return stream; |
118
|
|
|
}, |
119
|
|
|
'path': 'lib/blog-encrypt.js', |
120
|
|
|
}, { |
121
|
|
|
'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')), |
122
|
|
|
'path': 'css/blog-encrypt.css', |
123
|
|
|
}, |
124
|
|
|
]); |
125
|
|
|
|