Completed
Push — master ( 74a408...4c6568 )
by Dongxin
10s
created

index.js ➔ encrypt   C

Complexity

Conditions 10
Paths 73

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
c 1
b 0
f 0
nc 73
nop 1
dl 0
loc 70
rs 5.9999

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like index.js ➔ encrypt often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
33
  if ('password' in data && data.password) {
34
35
    // Use the blog's config first
36
    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...
37
38
    // Store the origin data
39
    data.origin = data.content;
40
    data.encrypt = true;
41
42
    if (!('abstract' in data && data.abstract)) {
43
44
      data.abstract = hexo.config.encrypt.default_abstract;
45
46
    }
47
    if (!('template' in data && data.template)) {
48
49
      data.template = hexo.config.encrypt.default_template;
50
51
    }
52
    if (!('message' in data && data.message)) {
53
54
      data.message = hexo.config.encrypt.default_message;
55
56
    }
57
58
    data.content = escape(data.content);
59
    data.content = CryptoJS.enc.Utf8.parse(data.content);
60
    data.content = CryptoJS.enc.Base64.stringify(data.content);
61
    data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString();
62
    // Console.log(data.content);
63
    data.template = data.template.replace('{{content}}', data.content);
64
    data.template = data.template.replace('{{message}}', data.message);
65
    data.template = data.template.replace('{{message}}', data.message);
66
67
    data.content = data.template;
68
    data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script>
69
<script src="${hexo.config.root}lib/blog-encrypt.js"></script>'
70
<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`;
71
72
    data.more = data.abstract;
73
    data.excerpt = data.more;
74
75
  }
76
  return data;
77
78
});
79
80
hexo.extend.generator.register('blog-encrypt', () => [
81
  {
82
    'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')),
83
    'path': 'lib/crypto-js.js',
84
  }, {
85
    'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.js')),
86
    'path': 'lib/blog-encrypt.js',
87
  }, {
88
    'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')),
89
    'path': 'css/blog-encrypt.css',
90
  },
91
]);
92