Conditions | 14 |
Paths | 1057 |
Total Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
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 */ |
||
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 | |||
118 |