From ab02fa2f62ac90cd6e1ffa7260a329ce46e2409b Mon Sep 17 00:00:00 2001 From: xiazeyu Date: Sun, 11 Aug 2019 14:42:29 +0800 Subject: [PATCH 1/8] update dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 55ab522..f0d9314 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ }, "dependencies": { "crypto-js": "^3.1.9-1", - "hexo-fs": "^0.2.3", + "hexo-fs": "^1.0.2", "hexo-log": "^0.2.0" }, "description": "Yet, just another blog encrypt plugin for hexo.", @@ -37,6 +37,6 @@ }, "version": "2.2.2", "devDependencies": { - "eslint": "^4.19.1" + "eslint": "^6.1.0" } } From c8a0dbe1767c382218f7508cafff22a52ef286ca Mon Sep 17 00:00:00 2001 From: xiazeyu Date: Sun, 11 Aug 2019 14:52:49 +0800 Subject: [PATCH 2/8] format documents --- .github/CONTRIBUTING.md | 30 ++--- .github/ISSUE_TEMPLATE.md | 11 +- .github/PULL_REQUEST_TEMPLATE.md | 7 +- ReadMe.md | 137 +++++++++++-------- ReadMe.zh.md | 219 ++++++++++++++++--------------- 5 files changed, 223 insertions(+), 181 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 70aafb0..3bab3ae 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,24 +1,22 @@ -## How to Contribute to This Project +# How to Contribute to This Project -#### **Did You Find a Bug?** +## **Did You Find a Bug?** - * **Ensure the bug was not already reported** by searching on GitHub under [Issues]. - * If you're unable to find an open issue addressing the problem, [open a new one]. Be sure to include a **title and clear description**, as much relevant information as possible, and a **code sample** or an **executable test case** demonstrating the expected behavior that is not occurring. +- **Ensure the bug was not already reported** by searching on GitHub under [Issues]. +- If you're unable to find an open issue addressing the problem, [open a new one]. Be sure to include a **title and clear description**, as much relevant information as possible, and a **code sample** or an **executable test case** demonstrating the expected behavior that is not occurring. -#### **Did You Write a Patch That Fixes a Bug?** +## **Did You Write a Patch That Fixes a Bug?** - * Open a new GitHub pull request with the patch. - 1. Fork this project - 1. Create your feature branch: `git checkout -b my-new-feature` - 1. Commit your changes: `git commit -am 'Add some feature'` - 1. Push to the branch: `git push origin my-new-feature` - 1. Submit a pull request :tada: - * Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable. +- Open a new GitHub pull request with the patch. + 1. Fork this project + 2. Create your feature branch: `git checkout -b my-new-feature` + 3. Commit your changes: `git commit -am 'Add some feature'` + 4. Push to the branch: `git push origin my-new-feature` + 5. Submit a pull request :tada: +- Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable. -#### **Do You Intend to Add a New Feature or Change an Existing One?** +## **Do You Intend to Add a New Feature or Change an Existing One?** - * Suggest your change as a new [issue] using the label `enhancement` **BEFORE** you start writing code. +- Suggest your change as a new [issue] using the label `enhancement` **BEFORE** you start writing code. Thanks for contributing! :heart: - - diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index cc5b7ca..1eea0fc 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,14 +1,15 @@ -## Expected Behavior +# Issue +## Expected Behavior ## Actual Behavior - ## Steps to Reproduce the Problem - 1. - 1. - 1. +1. _ +2. _ +3. _ ## Specifications + (The version of the project, operating system, hardware etc.) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 7291250..253f427 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,8 +1,11 @@ +# PR + Issue Fixed # ## Proposed Changes - - - - - +- _ +- _ +- _ @MikeCoder diff --git a/ReadMe.md b/ReadMe.md index df2d54a..2f548af 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -7,83 +7,100 @@ [中文说明](./ReadMe.zh.md) ## What is Hexo Blog Encrypt + > Think about this, you write an article, but not want everyone to read. So you will add a passwrod on the blog, others need to answer the password to access the blog. > It is easy on wordpress or emlog or other blog system. However, when you on hexo, there is no such a plugin or function before. -> Now let me introduce my plugin "Hexo-Blog-Encrypt". +> Now let me introduce my plugin "hexo-blog-encrypt". ## Feature + + Once you enter the correct password, you can get the access for 30 minutes. ## Live Demo + See [Demo Page](https://mhexo.github.io/example-site/2018/06/25/encrypt-test/), **all passwords are `123`**. -# Install +## Install + + `npm install --save hexo-blog-encrypt` + or `yarn add hexo-blog-encrypt` (require [Yarn](https://yarnpkg.com/en/)) -# Quick Start +## Quick Start + + First, make sure your post has content(not empty, or only has space). + Then you should enable the plugin in your `_config.yml` like below: -``` + +```yaml + # Security -## -encrypt: - enable: true +encrypt: # hexo-blog-encrypt + enable: true + ``` + Add password and abstract and message to your blog source like below: -``` +```markdown + --- title: Hello World date: 2016-03-30 21:18:02 password: mikemessi -abstract: Welcome to my blog, enter password to read. -message: Welcome to my blog, enter password to read. +abstract: Something was encrypted, please enter password to read. +message: Welcome to my blog, please enter password to read. --- + ``` + If you want to encrypt you TOC of the blog, you should add the following code to your article.ejs: -``` +```ejs + <% if(post.toc == true){ %> -
style="display:none" <% } %>> - Index - <% if (post.encrypt == true) { %> - <%- toc(post.origin) %> - <% } else { %> - <%- toc(post.content) %> - <% } %> -
+
style="display:none" <% } %>> + Index + <% if (post.encrypt == true) { %> + <%- toc(post.origin) %> + <% } else { %> + <%- toc(post.content) %> + <% } %> +
<% } %> <%- post.content %> + ``` + Then use `hexo clean && hexo g && hexo s` to see your blog. -# Advanced Usage +## For Advanced Users + +### First you should enable the plugin in your _config.yml like below + +```yaml -### First you should enable the plugin in your _config.yml like below. -``` # Security -## -encrypt: - enable: true +encrypt: # hexo-blog-encrypt + enable: true + ``` -### Then, add password to the blogs. +### Then, add password to the blogs + +```markdown -``` --- title: Hello World date: 2016-03-30 21:18:02 password: mikemessi -abstract: Welcome to my blog, enter password to read. +abstract: Something was encrypted, please enter password to read. message: Welcome to my blog, enter password to read. --- + ``` + As we can see above, we add 'password, abstract, message' the new 3 items in the blog info block. + + password is the blog password. + abstract is the content which will be showed in the blog list page. + message is the content which will be showed in the blog detail page. @@ -95,32 +112,35 @@ If you has a post with TOC, you should change the code of template. Use the defa + You should find the *article.ejs* file which is located in *hexo/themes/landscape/layout/_partial/article.ejs*. + Find the code like <% post.content %>, which is usually at line 30. + Replace the <% post.content %> with the following code block: -``` + +```ejs + <% if(post.toc == true){ %> -
style="display:none" <% } %>> - Index - <% if (post.encrypt == true) { %> - <%- toc(post.origin, {list_number: true}) %> - <% } else { %> - <%- toc(post.content, {list_number: true}) %> - <% } %> -
+
style="display:none" <% } %>> + Index + <% if (post.encrypt == true) { %> + <%- toc(post.origin, {list_number: true}) %> + <% } else { %> + <%- toc(post.content, {list_number: true}) %> + <% } %> +
<% } %> <%- post.content %> + ``` ### Change Template If you are not satisfied with the default template, you can just change it to your favorite one. Just follow the following steps. -``` +```yaml + # Security -## -encrypt: - enable: true - default_abstract: the content has been encrypted, enter the password to read.
- default_message: Please enter the password to read. - default_template: +encrypt: # hexo-blog-encrypt + enable: true + default_abstract: Something was encrypted, please enter password to read.
+ default_message: Welcome to my blog, enter password to read. + default_template: |-
@@ -134,19 +154,22 @@ encrypt: + ``` + You can see **default_abstract** and **default_template** and **default_message** here. - + default_abstract: means the default description which will be shown on the blogs list page. - + default_message: means the default message will show above the password input area. - + default_template : means the default detail page which will be shown on the detial page. - + the decryption div's id **must** be 'hbe-security' - + the content div's id **must** be 'encrypt-blog' - + there must be a input's id **must** be pass, which will let reader to input their password - + there must be trigger which calls the 'decryptAES' function + + default_abstract: means the default description which will be shown on the blogs list page. + + default_message: means the default message will show above the password input area. + + default_template : means the default detail page which will be shown on the detial page. + + the decryption div's id **must** be 'hbe-security' + + the content div's id **must** be 'encrypt-blog' + + there must be a input's id **must** be pass, which will let reader to input their password + + there must be trigger which calls the 'decryptAES' function If you want to make the blog special, You can add abstract and template to your blog files, like these: -``` + +```markdown + --- title: hello world date: 2016-03-30 21:18:02 @@ -168,6 +191,7 @@ template: {{content}}
--- + ``` The plugin will use the template content instead of the default one. @@ -177,23 +201,28 @@ The plugin will use the template content instead of the default one. In case that you would like to invoke some code after blog content is decrypted, you can add one config as below demo: ```yaml + encrypt: enable: true callback: |- initLightGallery() initImageResize() initTocBot() + ``` -> the symbol `|-` after `callback` means multi-line value. +> the symbol `|-` after `callback` means multi-line value. -You should write your own js code here, some functions if you defined it elsewhere, do not just copy the code like `initXXXX()` +You should write your own js code here, some functions if you defined it elsewhere, do not just copy the code like `initXXXX()` ## TODO + See [TODO](./TODO.md) file. ## License + See [LICENSE](./LICENSE) file. ## Thanks + Collaborator - [xiazeyu](https://github.com/xiazeyu) diff --git a/ReadMe.zh.md b/ReadMe.zh.md index 356027f..5994213 100644 --- a/ReadMe.zh.md +++ b/ReadMe.zh.md @@ -1,5 +1,4 @@ -hexo-blog-encrypt ---- +# hexo-blog-encrypt [![npm version](https://badge.fury.io/js/hexo-blog-encrypt.svg)](https://badge.fury.io/js/hexo-blog-encrypt) [![Build Status](https://scrutinizer-ci.com/g/MikeCoder/hexo-blog-encrypt/badges/build.png?b=master)](https://scrutinizer-ci.com/g/MikeCoder/hexo-blog-encrypt/build-status/master) @@ -7,134 +6,142 @@ hexo-blog-encrypt [English ReadMe](./ReadMe.md) -# 提 issue 前请学会如何总结归纳,请不要直接一句话描述问题,除非问题十分明确。可以参考这几个 issue: [#79](https://github.com/MikeCoder/hexo-blog-encrypt/issues/79), [#68](https://github.com/MikeCoder/hexo-blog-encrypt/issues/68), [#83](https://github.com/MikeCoder/hexo-blog-encrypt/issues/83), [#21](https://github.com/MikeCoder/hexo-blog-encrypt/issues/21) +> 提 issue 前请学会如何总结归纳,请不要直接一句话描述问题,除非问题十分明确。可以参考这几个 issue: [#79](https://github.com/MikeCoder/hexo-blog-encrypt/issues/79), [#68](https://github.com/MikeCoder/hexo-blog-encrypt/issues/68), [#83](https://github.com/MikeCoder/hexo-blog-encrypt/issues/83), [#21](https://github.com/MikeCoder/hexo-blog-encrypt/issues/21) ## 什么是 Hexo-Blog-Encrypt + > 尝试着想一下,你写了一篇博客,但是,出于某种原因,不太希望每一个人都可以看到他。所以你常常会为这种文章设置一个密码,其他人需要输入密码才可以访问这篇博客。对于 emlog 或者 wordpress 来说,这很容易,但是对于 hexo 来说,之前并没有一个类似的功能。 -> > 所以,Hexo-Blog-Encrypt 因为这个需求而诞生了。 ## 特点 -+ 一旦你输入了正确的密码,你可以在接下来的 30 分钟内,无需密码访问该网页。 + +- 一旦你输入了正确的密码,你可以在接下来的 30 分钟内,无需密码访问该网页。 ## 线上 Demo + 你可以查看 [Demo Page](https://mhexo.github.io/example-site/2018/06/25/encrypt-test/) 所有的密码都是 `123` -# 安装 -+ `npm install --save hexo-blog-encrypt` +## 安装 -+ 或者 `yarn add hexo-blog-encrypt` (需要安装 [Yarn](https://yarnpkg.com/en/)) +- `npm install --save hexo-blog-encrypt` +- 或者 `yarn add hexo-blog-encrypt` (需要安装 [Yarn](https://yarnpkg.com/en/)) -# 快速开始 -+ 首先, 你需要确保你的文章中含有内容(不能为空,或者只包含空格) -+ 然后在 `_config.yml` 中启用该插件: +## 快速开始 + +- 首先, 你需要确保你的文章中含有内容(不能为空,或者只包含空格) +- 然后在 `_config.yml` 中启用该插件: + +```yaml -``` # Security -## -encrypt: - enable: true +encrypt: # hexo-blog-encrypt + enable: true + ``` -+ 然后在你的文章的头部添加上对应的字段,如 password, abstract, message +- 然后在你的文章的头部添加上对应的字段,如 password, abstract, message + +```markdown -``` --- -title: hello world +title: Hello World date: 2016-03-30 21:18:02 -tags: - - fdsafsdaf -password: Mike -abstract: Welcome to my blog, enter password to read. -message: Welcome to my blog, enter password to read. +password: mikemessi +abstract: Something was encrypted, please enter password to read. +message: Welcome to my blog, please enter password to read. --- + ``` -+ 如果你想对 TOC 也进行加密,则在 article.ejs 中将 TOC 的生成代码修改成如下: +- 如果你想对 TOC 也进行加密,则在 article.ejs 中将 TOC 的生成代码修改成如下: -``` - <% if(post.toc == true){ %> -
style="display:none" <% } %>> - Index - <% if (post.encrypt == true) { %> - <%- toc(post.origin) %> - <% } else { %> - <%- toc(post.content) %> - <% } %> -
- <% } %> - <%- post.content %> -``` +```ejs -+ 然后使用 *hexo clean && hexo g && hexo s*,来查看效果。 +<% if(post.toc == true){ %> +
style="display:none" <% } %>> + Index + <% if (post.encrypt == true) { %> + <%- toc(post.origin) %> + <% } else { %> + <%- toc(post.content) %> + <% } %> +
+<% } %> +<%- post.content %> +``` + +- 然后使用 *hexo clean && hexo g && hexo s*,来查看效果。 -# 具体的使用方法 +## 具体的使用方法 ### 首先,你需要在 _config.yml 中启用该插件 -``` + +```yaml + # Security -## -encrypt: - enable: true -``` +encrypt: # hexo-blog-encrypt + enable: true -### 给文章添加密码: ``` + +### 给文章添加密码 + +```markdown + --- -title: hello world +title: Hello World date: 2016-03-30 21:18:02 -tags: - - fdsfadsfa - - fdsafsdaf -password: Mike -abstract: Welcome to my blog, enter password to read. +password: mikemessi +abstract: Something was encrypted, please enter password to read. message: Welcome to my blog, enter password to read. --- -``` - -+ password: 是该博客加密使用的密码 -+ abstract: 是该博客的摘要,会显示在博客的列表页 -+ message: 这个是博客查看时,密码输入框上面的描述性文字 +``` +- password: 是该博客加密使用的密码 +- abstract: 是该博客的摘要,会显示在博客的列表页 +- message: 这个是博客查看时,密码输入框上面的描述性文字 ### 对 TOC 进行加密 如果你有一篇文章使用了 TOC,你需要修改模板的部分代码。这里用 landscape 作为例子: -+ 你可以在 *hexo/themes/landscape/layout/_partial/article.ejs* 找到 article.ejs。 -+ 然后找到 <% post.content %> 这段代码,通常在30行左右。 -+ 使用如下的代码来替代它: -``` +- 你可以在 *hexo/themes/landscape/layout/_partial/article.ejs* 找到 article.ejs。 +- 然后找到 <% post.content %> 这段代码,通常在30行左右。 +- 使用如下的代码来替代它: + +```ejs + <% if(post.toc == true){ %> -
style="display:none" <% } %>> - Index - <% if (post.encrypt == true) { %> - <%- toc(post.origin, {list_number: true}) %> - <% } else { %> - <%- toc(post.content, {list_number: true}) %> - <% } %> -
+
style="display:none" <% } %>> + Index + <% if (post.encrypt == true) { %> + <%- toc(post.origin, {list_number: true}) %> + <% } else { %> + <%- toc(post.content, {list_number: true}) %> + <% } %> +
<% } %> <%- post.content %> + ``` ### 修改加密模板 -+ 如果你对默认的主题不满意,或者希望修改默认的提示和摘要内容,你可以添加如下配置在 *_config.yml* 中。 +- 如果你对默认的主题不满意,或者希望修改默认的提示和摘要内容,你可以添加如下配置在 *_config.yml* 中。 + +```yaml -``` # Security -## -encrypt: - enable: true - default_abstract: the content has been encrypted, enter the password to read.
- default_message: Please enter the password to read. - default_template: +encrypt: # hexo-blog-encrypt + enable: true + default_abstract: Something was encrypted, please enter password to read.
+ default_message: Welcome to my blog, enter password to read. + default_template: |-
@@ -148,44 +155,44 @@ encrypt: + ``` -+ 可以看见,和上面的配置文件对比来看,多了 **default_template** 和 **default_abstract** 和 **default_message** 配置项。 - + default_abstract : 这个是指在文章列表页,我们看到的加密文章描述。当然这是对所有加密文章生效的。 - + default_message : 这个在文章详情页的密码输入框上方的描述性文字。 - + default_template : 这个是指在文章详情页,我们看到的输入密码阅读的模板,同理,这个也是针对所有文章的 - + 开始的解密部分需要由 div 包裹,而且 div 的 id **必须** 是 'hbe-security',解密后以便于隐藏。 - + 最后的 content 显示 div 的 id **必须** 是 'encrypt-blog',同时为了好看,也希望进行隐藏。 - + 同时,必须要有一个 input 输入框,id **必须**是"pass",用来供用户输入密码。 - + 输入密码之后,务必要有一个触发器,用来调用 'decryptAES' 函数。样例中以 button 来触发。 +- 可以看见,和上面的配置文件对比来看,多了 **default_template** 和 **default_abstract** 和 **default_message** 配置项。 + - default_abstract : 这个是指在文章列表页,我们看到的加密文章描述。当然这是对所有加密文章生效的。 + - default_message : 这个在文章详情页的密码输入框上方的描述性文字。 + - default_template : 这个是指在文章详情页,我们看到的输入密码阅读的模板,同理,这个也是针对所有文章的 + - 开始的解密部分需要由 div 包裹,而且 div 的 id **必须** 是 'hbe-security',解密后以便于隐藏。 + - 最后的 content 显示 div 的 id **必须** 是 'encrypt-blog',同时为了好看,也希望进行隐藏。 + - 同时,必须要有一个 input 输入框,id **必须**是"pass",用来供用户输入密码。 + - 输入密码之后,务必要有一个触发器,用来调用 'decryptAES' 函数。样例中以 button 来触发。 -+ 如果你希望对某一篇特定的文章做特殊处理,这有两种方法可以达到这个效果, 在博客的源文件添加 template 配置: +- 如果你希望对某一篇特定的文章做特殊处理,这有两种方法可以达到这个效果, 在博客的源文件添加 template 配置: + +```markdown -``` --- title: hello world date: 2016-03-30 21:18:02 -tags: - - fdsfadsfa - - fdsafsdaf password: Mike abstract: Welcome to my blog, enter password to read. message: Welcome to my blog, enter password to read. template: - -
-
- - -
-
-
- - - + +
+
+ + +
+
+
+ + + --- + ``` ## 回调 @@ -193,24 +200,28 @@ template: 如果您需要在文章解密之后调用一些代码,您可以参考以下配置: ```yaml + encrypt: enable: true callback: |- initLightGallery() initImageResize() initTocBot() + ``` -> 在`callback` 之后的这个符号`|-`代表多行的yaml值 +> 在`callback` 之后的这个符号`|-`代表多行的yaml值 如果您在其他js文件里面定义了函数,您可以在这里调用它们,或者您也可以在`callback`这里写上您自己的代码逻辑,比如`$('#someId').lightGallery()`,上面的`initXXX()`只是示例,您不应该直接复制上面的配置。 ## TODO + See [TODO](./TODO.md) File ## License -See [LICENSE](./LICENSE) File. +See [LICENSE](./LICENSE) File. ## Thanks + Collaborator - [xiazeyu](https://github.com/xiazeyu) From 69b1710bfd2370de5ac86c139b60b666c5388d90 Mon Sep 17 00:00:00 2001 From: xiazeyu Date: Sun, 11 Aug 2019 15:05:48 +0800 Subject: [PATCH 3/8] optimise dev configs --- .editorconfig | 1 - .eslintrc.js | 199 -------------------------------------------------- 2 files changed, 200 deletions(-) diff --git a/.editorconfig b/.editorconfig index 24f0c2b..f1bca8a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,4 +12,3 @@ max_line_length = null [*.md] trim_trailing_whitespace = false - diff --git a/.eslintrc.js b/.eslintrc.js index 86a9e68..760c7f1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -4,204 +4,5 @@ module.exports = { 'commonjs': true, 'es6': true }, - // 'extends': 'eslint:all', 'extends': 'eslint:recommended', - 'parserOptions': { - 'sourceType': 'script' - }, - 'rules': { - 'indent': [ - 'error', - 2 - ], - 'linebreak-style': [ - 'error', - 'unix', - ], - 'quotes': [ - 'error', - 'single', - ], - 'semi': [ - 'error', - 'always', - ], - 'no-console': 'off', - 'no-extra-parens': [ - 'error', - ], - 'valid-jsdoc': [ - 'warn', - ], - 'array-callback-return': [ - 'error', - ], - 'block-scoped-var': [ - 'warn', - ], - 'consistent-return': [ - 'error', - { - 'treatUndefinedAsUnspecified': true, - } - ], - 'curly': [ - 'warn', - 'all', - ], - 'default-case': [ - 'error', - ], - 'dot-location': [ - 'error', - 'property', - ], - 'dot-notation': [ - 'warn', - ], - 'eqeqeq': [ - 'error', - ], - 'no-else-return': [ - 'error', - ], - 'no-empty-function': [ - 'error', - ], - 'no-magic-numbers': [ - 'warn', - ], - 'no-multi-spaces': [ - 'warn', - ], - 'no-return-assign': [ - 'error', - ], - 'no-sequences': [ - 'warn', - ], - 'no-useless-concat': [ - 'error', - ], - 'no-useless-return': [ - 'warn', - ], - 'strict': [ - 'error', - ], - 'no-catch-shadow': [ - 'error', - ], - 'no-shadow': [ - 'error', - ], - 'no-use-before-define': [ - 'error', - ], - 'global-require': [ - 'error', - ], - 'array-bracket-newline': [ - 'warn', - ], - 'array-bracket-spacing': [ - 'error', - ], - 'block-spacing': [ - 'error', - ], - 'brace-style': [ - 'error', - ], - 'camelcase': [ - 'error', - ], - 'comma-dangle': [ - 'error', - 'always', - ], - 'capitalized-comments': [ - 'warn', - ], - 'comma-spacing': [ - 'warn', - ], - 'comma-style': [ - 'warn', - ], - 'eol-last': [ - 'warn', - ], - 'func-names': [ - 'error', - ], - 'new-parens': [ - 'error', - ], - 'no-lonely-if': [ - 'error', - ], - 'no-mixed-operators': [ - 'error', - ], - 'no-trailing-spaces': [ - 'error', - ], - 'no-unneeded-ternary': [ - 'error', - ], - 'one-var': [ - 'error', - 'never', - ], - 'space-before-function-paren': [ - 'error' - ], - 'space-before-blocks': [ - 'error', - ], - 'space-in-parens': [ - 'error', - ], - 'arrow-body-style': [ - 'error', - ], - 'arrow-parens': [ - 'error', - ], - 'arrow-spacing': [ - 'error', - ], - 'no-var': [ - 'error', - ], - 'prefer-const': [ - 'error', - ], - 'prefer-template': [ - 'error', - ], - 'space-infix-ops': [ - 'error', - ], - 'space-unary-ops': [ - 'error', - ], - 'spaced-comment': [ - 'error', - ], - 'quote-props': [ - 'error', - ], - 'require-jsdoc': [ - 'error', - ], - 'func-style': [ - 'warn', - 'declaration', - ], - 'padded-blocks': [ - 'warn', - ], - }, }; From 54651f260bf7012cc3f8ecb7b4cfb2b0ab5c6e09 Mon Sep 17 00:00:00 2001 From: xiazeyu Date: Sun, 11 Aug 2019 15:29:21 +0800 Subject: [PATCH 4/8] remove jQuery --- ReadMe.md | 2 -- ReadMe.zh.md | 2 -- lib/blog-encrypt.js | 76 ++++++++++++++++++++------------------------- template.html | 1 - 4 files changed, 34 insertions(+), 47 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 2f548af..871e754 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -141,7 +141,6 @@ encrypt: # hexo-blog-encrypt default_abstract: Something was encrypted, please enter password to read.
default_message: Welcome to my blog, enter password to read. default_template: |- -
@@ -177,7 +176,6 @@ password: Mike abstract: Welcome to my blog, enter password to read. message: Welcome to my blog, enter password to read. template: -
diff --git a/ReadMe.zh.md b/ReadMe.zh.md index 5994213..b004cae 100644 --- a/ReadMe.zh.md +++ b/ReadMe.zh.md @@ -142,7 +142,6 @@ encrypt: # hexo-blog-encrypt default_abstract: Something was encrypted, please enter password to read.
default_message: Welcome to my blog, enter password to read. default_template: |- -
@@ -178,7 +177,6 @@ password: Mike abstract: Welcome to my blog, enter password to read. message: Welcome to my blog, enter password to read. template: -
diff --git a/lib/blog-encrypt.js b/lib/blog-encrypt.js index 7ba3355..4fe4a27 100644 --- a/lib/blog-encrypt.js +++ b/lib/blog-encrypt.js @@ -1,3 +1,4 @@ +/* global CryptoJS */ 'use strict'; function decryptAES (password) { @@ -22,31 +23,22 @@ function decryptAES (password) { content = unescape(content); if (content === '') { - throw new Error(noContentError); // ??? + throw new Error(noContentError); } else { document.getElementById('encrypt-blog').style.display = 'inline'; document.getElementById('encrypt-blog').innerHTML = ''; - - // Use jquery to load some js code - try { - - $('#encrypt-blog').html(content); - - // NO Style Change here - {callback} - // NO Style Change here - - } catch(e) { - + try{ + document.getElementById('encrypt-blog').innerHTML = content; + {callback} // TODO + } catch (e){ const errorInfo = '

' - + 'Some errors occurred, check the original file please.' - + 'Detailed exceptions are shown in console.' - + '

'; + + 'Some errors occurred, check the original file please.' + + 'Detailed exceptions are shown in console.' + + '

'; console.error(e); - $('#encrypt-blog').html(errorInfo); - + document.getElementById('encrypt-blog').innerHTML = errorInfo; } document.getElementById('hbe-security').style.display = 'none'; @@ -171,42 +163,42 @@ function GenerateCookieName () { } -// Since you decided to use jQuery. -$(document).ready( - function () { - - - let password = String(getCookie(GenerateCookieName())); - console.log(`Get password from Cookie:${ password}`); +function hbeLoader(){ + let password = String(getCookie(GenerateCookieName())); + console.log(`Get password from Cookie:${password}`); - if (password != '') { + if (password != '') { - if(!decryptAES(password)) { + if (!decryptAES(password)) { - // Delete cookie - setCookie(COOKIE_NAME, password, -5); - - } + // Delete cookie + setCookie(COOKIE_NAME, password, -5); } - console.log('Registering Enter for decrypt.'); - document.getElementById('pass').onkeypress = function (keyPressEvent) { + } - password = String(document.getElementById('pass').value); - if (keyPressEvent.keyCode === 13) { + console.log('Registering Enter for decrypt.'); + document.getElementById('pass').onkeypress = function (keyPressEvent) { - const result = decryptAES(password); + password = String(document.getElementById('pass').value); + if (keyPressEvent.keyCode === 13) { - if (result) { + const result = decryptAES(password); - setCookie(GenerateCookieName(), password, 30); + if (result) { - } + setCookie(GenerateCookieName(), password, 30); } - }; + } + + }; +} - } -); +if (document.readyState !== 'loading') { + hbeLoader(); +} else { + document.addEventListener('DOMContentLoaded', hbeLoader); +} diff --git a/template.html b/template.html index 0780255..6c8bd23 100644 --- a/template.html +++ b/template.html @@ -1,4 +1,3 @@ -
From 1592360ec39237805b3543505c5a3d10db35a918 Mon Sep 17 00:00:00 2001 From: xiazeyu Date: Sun, 11 Aug 2019 15:36:45 +0800 Subject: [PATCH 5/8] lint code --- index.js | 2 +- lib/blog-encrypt.css | 94 ++++++++++++++++++++++---------------------- lib/blog-encrypt.js | 31 +++------------ 3 files changed, 54 insertions(+), 73 deletions(-) diff --git a/index.js b/index.js index fe0b58c..fcdebad 100644 --- a/index.js +++ b/index.js @@ -120,7 +120,7 @@ hexo.extend.generator.register('blog-encrypt', () => [ const Readable = require('stream').Readable; const stream = new Readable(); stream.push(fs.readFileSync(path.resolve(__dirname, 'lib/blog-encrypt.js')) - .replace('{callback}', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : '')); + .replace('{ callback() }', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : '')); stream.push(null); // Indicates the end of the stream return stream; diff --git a/lib/blog-encrypt.css b/lib/blog-encrypt.css index b7bb979..1b3f3fd 100644 --- a/lib/blog-encrypt.css +++ b/lib/blog-encrypt.css @@ -1,72 +1,72 @@ .hbe-input-container { - width: 80%; - max-width: 800px; - position: relative; - display: -webkit-box; - display: -ms-flexbox; - display: flex; - -webkit-box-orient: vertical; - -webkit-box-direction: reverse; - -ms-flex-flow: column-reverse; - flex-flow: column-reverse; - -webkit-box-align: start; - -ms-flex-align: start; - align-items: flex-start; - margin: 100px auto; + width: 80%; + max-width: 800px; + position: relative; + display: -webkit-box; + display: -ms-flexbox; + display: flex; + -webkit-box-orient: vertical; + -webkit-box-direction: reverse; + -ms-flex-flow: column-reverse; + flex-flow: column-reverse; + -webkit-box-align: start; + -ms-flex-align: start; + align-items: flex-start; + margin: 100px auto; } .hbe-input-container input { - -webkit-box-ordinal-group: 11; - order: 10; - -ms-flex-order: 10; - outline: none; - border: none; - width: 100%; - padding: 15px 0; - font-size: 20px; - border-bottom: 1px solid #d5d5d5; - text-indent: 10px; + -webkit-box-ordinal-group: 11; + order: 10; + -ms-flex-order: 10; + outline: none; + border: none; + width: 100%; + padding: 15px 0; + font-size: 20px; + border-bottom: 1px solid #d5d5d5; + text-indent: 10px; } .hbe-input-container input::-moz-placeholder { - opacity: 0; + opacity: 0; } .hbe-input-container input::-webkit-input-placeholder { - opacity: 0; + opacity: 0; } .hbe-input-container input:-ms-input-placeholder { - opacity: 0; + opacity: 0; } .hbe-input-container input, .hbe-input-container label { - transition: all 0.3s; + transition: all 0.3s; } .hbe-input-container label { - -webkit-box-ordinal-group: 101; - -ms-flex-order: 100; - font-size: 18px; - order: 100; - color: #3f4f5b; - -webkit-transform-origin: left bottom; - transform-origin: left bottom; - -webkit-transform: translate(10px, 40px); - transform: translate(0px, 40px); + -webkit-box-ordinal-group: 101; + -ms-flex-order: 100; + font-size: 18px; + order: 100; + color: #3f4f5b; + -webkit-transform-origin: left bottom; + transform-origin: left bottom; + -webkit-transform: translate(10px, 40px); + transform: translate(0px, 40px); } .hbe-input-container .bottom-line { - order: 2; - width: 0; - height: 2px; - background: #658db5; - transition: all 0.3s; + order: 2; + width: 0; + height: 2px; + background: #658db5; + transition: all 0.3s; } .hbe-input-container input:focus { - border-bottom-color: #fff; + border-bottom-color: #fff; } .hbe-input-container input:focus ~ div, .hbe-input-container input:not(:placeholder-shown) ~ div { - width: 100%; + width: 100%; } .hbe-input-container input:focus + label, .hbe-input-container input:not(:placeholder-shown) + label { - color: #52616c; - -webkit-transform: translate(10px) scale(0.9); - transform: translate(10px) scale(0.9); + color: #52616c; + -webkit-transform: translate(10px) scale(0.9); + transform: translate(10px) scale(0.9); } diff --git a/lib/blog-encrypt.js b/lib/blog-encrypt.js index 4fe4a27..11f8224 100644 --- a/lib/blog-encrypt.js +++ b/lib/blog-encrypt.js @@ -1,6 +1,8 @@ -/* global CryptoJS */ +/* global CryptoJS, callback, MathJax */ 'use strict'; +const hbeCookieName = 'HBEPASSWORD'; + function decryptAES (password) { try { @@ -31,7 +33,7 @@ function decryptAES (password) { document.getElementById('encrypt-blog').innerHTML = ''; try{ document.getElementById('encrypt-blog').innerHTML = content; - {callback} // TODO + { callback() } // callback() will be automatically replaced. } catch (e){ const errorInfo = '

' + 'Some errors occurred, check the original file please.' @@ -81,26 +83,6 @@ function decryptAES (password) { } -function htmlDecode (str) { - - let s = ''; - if (str.length == 0) { - - return ''; - - } - - s = str.replace(/>/g, '&'); - s = s.replace(/</g, '<'); - s = s.replace(/>/g, '>'); - s = s.replace(/ /g, ' '); // ??? why not ' ' - s = s.replace(/'/g, '\''); - s = s.replace(/"/g, '"'); - s = s.replace(/
/g, '\n'); - return s; - -} - function decodeBase64 (content) { content = CryptoJS.enc.Base64.parse(content); @@ -158,8 +140,7 @@ function GetUrlRelativePath () { function GenerateCookieName () { - const COOKIE_NAME = 'HBE-PASSWORD'; - return COOKIE_NAME + GetUrlRelativePath(); + return hbeCookieName + GetUrlRelativePath(); } @@ -172,7 +153,7 @@ function hbeLoader(){ if (!decryptAES(password)) { // Delete cookie - setCookie(COOKIE_NAME, password, -5); + setCookie(hbeCookieName, password, -5); } From 148aaffe84850515a55de649f098b40fcc3e8b3c Mon Sep 17 00:00:00 2001 From: xiazeyu Date: Sun, 11 Aug 2019 15:43:52 +0800 Subject: [PATCH 6/8] fix replace bugs --- index.js | 2 +- lib/blog-encrypt.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index fcdebad..3c45837 100644 --- a/index.js +++ b/index.js @@ -120,7 +120,7 @@ hexo.extend.generator.register('blog-encrypt', () => [ const Readable = require('stream').Readable; const stream = new Readable(); stream.push(fs.readFileSync(path.resolve(__dirname, 'lib/blog-encrypt.js')) - .replace('{ callback() }', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : '')); + .replace('callBackReplaceHere', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : '')); stream.push(null); // Indicates the end of the stream return stream; diff --git a/lib/blog-encrypt.js b/lib/blog-encrypt.js index 11f8224..41106c0 100644 --- a/lib/blog-encrypt.js +++ b/lib/blog-encrypt.js @@ -1,4 +1,4 @@ -/* global CryptoJS, callback, MathJax */ +/* global CryptoJS, MathJax */ 'use strict'; const hbeCookieName = 'HBEPASSWORD'; @@ -33,7 +33,7 @@ function decryptAES (password) { document.getElementById('encrypt-blog').innerHTML = ''; try{ document.getElementById('encrypt-blog').innerHTML = content; - { callback() } // callback() will be automatically replaced. + callBackReplaceHere // eslint-disable-line no-undef } catch (e){ const errorInfo = '

' + 'Some errors occurred, check the original file please.' From fd15e287316c4b44f89473fd1ef37acb9962deae Mon Sep 17 00:00:00 2001 From: S4kura0ne Date: Tue, 20 Aug 2019 16:02:55 +0800 Subject: [PATCH 7/8] now scripts can run(polyfill needed?) --- lib/blog-encrypt.js | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/blog-encrypt.js b/lib/blog-encrypt.js index 41106c0..ab2c120 100644 --- a/lib/blog-encrypt.js +++ b/lib/blog-encrypt.js @@ -3,6 +3,32 @@ const hbeCookieName = 'HBEPASSWORD'; +function getExecutableScript(oldElem){ + + let out = document.createElement('script'); + const attList = ['type', 'text', 'src', 'crossorigin', 'defer', 'referrerpolicy']; + attList.forEach((att) => { + if(oldElem[att]){ + out[att] = oldElem[att]; + } + }) + + return out; + +} + +function convertHTMLToElement(content){ + + let out = document.createElement('div'); + out.innerHTML = content; + out.querySelectorAll('script').forEach((elem) => { + elem.replaceWith(getExecutableScript(elem)); + }); + + return out; +} + + function decryptAES (password) { try { @@ -19,10 +45,8 @@ function decryptAES (password) { try { - let content = CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), password); - content = content.toString(CryptoJS.enc.Utf8); - content = decodeBase64(content); - content = unescape(content); + let content = unescape(decodeBase64(CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), password).toString(CryptoJS.enc.Utf8))); + if (content === '') { throw new Error(noContentError); @@ -32,7 +56,7 @@ function decryptAES (password) { document.getElementById('encrypt-blog').style.display = 'inline'; document.getElementById('encrypt-blog').innerHTML = ''; try{ - document.getElementById('encrypt-blog').innerHTML = content; + document.getElementById('encrypt-blog').appendChild(convertHTMLToElement(content)); callBackReplaceHere // eslint-disable-line no-undef } catch (e){ const errorInfo = '

' From 0af6d38b7acd59b2b2c5804f0a8fc18b026c09ff Mon Sep 17 00:00:00 2001 From: S4kura0ne Date: Tue, 20 Aug 2019 16:23:31 +0800 Subject: [PATCH 8/8] use replaceChild instead of replaceWith --- lib/blog-encrypt.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/blog-encrypt.js b/lib/blog-encrypt.js index ab2c120..0603340 100644 --- a/lib/blog-encrypt.js +++ b/lib/blog-encrypt.js @@ -8,9 +8,8 @@ function getExecutableScript(oldElem){ let out = document.createElement('script'); const attList = ['type', 'text', 'src', 'crossorigin', 'defer', 'referrerpolicy']; attList.forEach((att) => { - if(oldElem[att]){ + if(oldElem[att]) out[att] = oldElem[att]; - } }) return out; @@ -22,7 +21,7 @@ function convertHTMLToElement(content){ let out = document.createElement('div'); out.innerHTML = content; out.querySelectorAll('script').forEach((elem) => { - elem.replaceWith(getExecutableScript(elem)); + elem.parentNode.replaceChild(getExecutableScript(elem), elem); }); return out;