-
Notifications
You must be signed in to change notification settings - Fork 101
/
index.js
117 lines (80 loc) · 3.85 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
/* global hexo, __dirname */
'use strict';
const fs = require('hexo-fs');
const path = require('path');
const CryptoJS = require('crypto-js');
const log = require('hexo-log')({
'debug': false,
'silent': false,
});
hexo.extend.filter.register('after_post_render', function encrypt (data) {
// Close the encrypt function
if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) {
return data;
}
if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template
hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html'));
}
if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info
hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>';
}
if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message
hexo.config.encrypt.default_message = 'Please enter the password to read the blog.';
}
if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // Wrong password
hexo.config.encrypt.default_decryption_error = 'Incorrect Password!';
}
if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // No content
hexo.config.encrypt.default_no_content_error = 'No content to display!';
}
if ('password' in data && data.password) {
// Use the blog's config first
log.info(`Encrypted the blog: ${ data.title.trim() }`);
// Store the origin data
data.origin = data.content;
data.encrypt = true;
if (!('abstract' in data && data.abstract)) {
data.abstract = hexo.config.encrypt.default_abstract;
}
if (!('template' in data && data.template)) {
data.template = hexo.config.encrypt.default_template;
}
if (!('message' in data && data.message)) {
data.message = hexo.config.encrypt.default_message;
}
if (!('decryptionError' in data && data.decryptionError)) {
data.decryptionError = hexo.config.encrypt.default_decryption_error;
}
if (!('noContentError' in data && data.noContentError)) {
data.noContentError = hexo.config.encrypt.default_no_content_error;
}
data.content = escape(data.content);
data.content = CryptoJS.enc.Utf8.parse(data.content);
data.content = CryptoJS.enc.Base64.stringify(data.content);
data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString();
data.template = data.template.replace('{{content}}', data.content);
data.template = data.template.replace('{{message}}', data.message);
data.template = data.template.replace('{{message}}', data.message);
data.template = data.template.replace('{{decryptionError}}', data.decryptionError);
data.template = data.template.replace('{{noContentError}}', data.noContentError);
data.content = data.template;
data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script>`;
data.content += `<script src="${hexo.config.root}lib/blog-encrypt.js"></script>`;
data.content += `<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`;
data.more = data.abstract;
data.excerpt = data.more;
}
return data;
});
hexo.extend.generator.register('blog-encrypt', () => [
{
'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')),
'path': 'lib/crypto-js.js',
}, {
'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.js')),
'path': 'lib/blog-encrypt.js',
}, {
'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')),
'path': 'css/blog-encrypt.css',
},
]);