| Conditions | 10 |
| Paths | 73 |
| Total Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 */ |
||
| 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() }`); |
||
|
|
|||
| 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 | |||
| 92 |