Passed
Push — master ( a83047...643cce )
by Dongxin
02:49
created

index.js   A

Complexity

Total Complexity 18
Complexity/F 3.6

Size

Lines of Code 111
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 0
c 6
b 0
f 0
nc 8192
dl 0
loc 111
rs 10
wmc 18
mnd 2
bc 13
fnc 5
bpm 2.6
cpm 3.6
noi 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
F ➔ encrypt 0 92 14
A ➔ ??? 0 12 1
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
9
hexo.extend.filter.register('after_post_render', function encrypt (data) {
10
11
  // Close the encrypt function
12
  if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) {
13
14
    return data;
15
16
  }
17
  if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template
18
19
    hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html'));
20
21
  }
22
  if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info
23
24
    hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>';
25
26
  }
27
  if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message
28
29
    hexo.config.encrypt.default_message = 'Please enter the password to read the blog.';
30
31
  }
32
  if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // wrong password
33
34
    hexo.config.encrypt.default_decryption_error = 'Incorrect Password!';
35
36
  }
37
  if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // no content
38
39
    hexo.config.encrypt.default_no_content_error = 'No content to display!';
40
41
  }
42
43
  if ('password' in data && data.password) {
44
45
    // Use the blog's config first
46
    console.log(`encrypt the blog :${ data.title.trim() }`);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
47
48
    // Store the origin data
49
    data.origin = data.content;
50
    data.encrypt = true;
51
52
    if (!('abstract' in data && data.abstract)) {
53
54
      data.abstract = hexo.config.encrypt.default_abstract;
55
56
    }
57
    if (!('template' in data && data.template)) {
58
59
      data.template = hexo.config.encrypt.default_template;
60
61
    }
62
    if (!('message' in data && data.message)) {
63
64
      data.message = hexo.config.encrypt.default_message;
65
66
    }
67
    if (!('decryptionError' in data && data.decryptionError)) {
68
69
      data.decryptionError = hexo.config.encrypt.default_decryption_error;
70
71
    }
72
    if (!('noContentError' in data && data.noContentError)) {
73
74
      data.noContentError = hexo.config.encrypt.default_no_content_error;
75
76
    }
77
78
    data.content = escape(data.content);
79
    data.content = CryptoJS.enc.Utf8.parse(data.content);
80
    data.content = CryptoJS.enc.Base64.stringify(data.content);
81
    data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString();
82
    // Console.log(data.content);
83
    data.template = data.template.replace('{{content}}', data.content);
84
    data.template = data.template.replace('{{message}}', data.message);
85
    data.template = data.template.replace('{{message}}', data.message);
86
    data.template = data.template.replace('{{decryptionError}}', data.decryptionError);
87
    data.template = data.template.replace('{{noContentError}}', data.noContentError);
88
89
    data.content = data.template;
90
    data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script>
91
<script src="${hexo.config.root}lib/blog-encrypt.js"></script>'
92
<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`;
93
94
    data.more = data.abstract;
95
    data.excerpt = data.more;
96
97
  }
98
  return data;
99
100
});
101
102
hexo.extend.generator.register('blog-encrypt', () => [
103
  {
104
    'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')),
105
    'path': 'lib/crypto-js.js',
106
  }, {
107
    'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.js')),
108
    'path': 'lib/blog-encrypt.js',
109
  }, {
110
    'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')),
111
    'path': 'css/blog-encrypt.css',
112
  },
113
]);
114