Passed
Push — master ( 801361...7f4db7 )
by Dongxin
49s
created

index.js ➔ ... ➔ ???.data   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 6
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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