Skip to content

Commit

Permalink
index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Tang committed Oct 26, 2016
1 parent 336de1a commit 24d5089
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 43 deletions.
78 changes: 44 additions & 34 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Hexo-Blog-Encrypt
> Now let me introduce my plugin "Hexo-Blog-Encrypt".
##Live Demo
See [http://mikecoder.github.io/](http://mikecoder.github.io/2016/03/30/helloworld/)
See [http://mikecoder.github.io/](http://mikecoder.github.io/), **all passwords are *mikemessi***.

#Install
+ Add '"hexo-blog-encrypt": "1.1.\*"' to your hexo *package.json*.
Expand All @@ -31,22 +31,30 @@ encrypt:

```
---
title: hello world
title: Hello World
date: 2016-03-30 21:18:02
tags:
- fdsfadsfa
- fasdf
- fdsafdasf
- fdasfasdf
- fff
- fsdaf
- qfdsaf
- fdsafsdaf
password: Mike
password: mikemessi
abstract: Welcome to my blog, enter password to read.
message: Welcome to my blog, enter password to read.
---
```

+ If you want to encrypt you TOC of the blog, you should add the following code to your article.ejs:

```
<% if(post.toc == true){ %>
<div id="toc-div" class="toc-article" <% if (post.encrypt == true) { %>style="display:none" <% } %>>
<strong class="toc-title">Index</strong>
<% if (post.encrypt == true) { %>
<%- toc(post.origin, {list_number: true}) %>
<% } else { %>
<%- toc(post.content, {list_number: true}) %>
<% } %>
</div>
<% } %>
<%- post.content %>
```

+ Then use *hexo clean && hexo g && hexo s* to see your blog.

#Detail Usage
Expand All @@ -64,18 +72,9 @@ encrypt:
#### The recomanded one: add password to the source blog file.
```
---
title: hello world
title: Hello World
date: 2016-03-30 21:18:02
tags:
- fdsfadsfa
- fasdf
- fdsafdasf
- fdasfasdf
- fff
- fsdaf
- qfdsaf
- fdsafsdaf
password: Mike
password: mikemessi
abstract: Welcome to my blog, enter password to read.
message: Welcome to my blog, enter password to read.
---
Expand Down Expand Up @@ -106,11 +105,31 @@ encrypt:
---
title: hello world
date: 2016-03-30 21:18:02
tags:
---
```

###For professional usage
### Encrypt TOC

If you has a post with TOC, you should change the code of template. Use the default theme 'landscape' as an example:

+ 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:
```
<% if(post.toc == true){ %>
<div id="toc-div" class="toc-article" <% if (post.encrypt == true) { %>style="display:none" <% } %>>
<strong class="toc-title">Index</strong>
<% if (post.encrypt == true) { %>
<%- toc(post.origin, {list_number: true}) %>
<% } else { %>
<%- toc(post.content, {list_number: true}) %>
<% } %>
</div>
<% } %>
<%- 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.

Expand Down Expand Up @@ -154,15 +173,6 @@ If you want to make the blog special, You can add abstract and template to your
---
title: hello world
date: 2016-03-30 21:18:02
tags:
- fdsfadsfa
- fasdf
- fdsafdasf
- fdasfasdf
- fff
- fsdaf
- qfdsaf
- fdsafsdaf
password: Mike
abstract: Welcome to my blog, enter password to read.
message: Welcome to my blog, enter password to read.
Expand Down
42 changes: 41 additions & 1 deletion ReadMe.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Hexo-Blog-Encrypt
##线上 Demo
你可以查看 [http://mikecoder.github.io/](http://mikecoder.github.io/2016/03/30/helloworld/)

所有的密码都是 "mikemessi"

#安装
+ 在 hexo 根目录的 *package.json* 中添加 '"hexo-blog-encrypt": "1.1.\*"' 依赖。
+ 然后执行 *npm install* 命令。
Expand Down Expand Up @@ -42,6 +44,22 @@ message: Welcome to my blog, enter password to read.
---
```

+ 如果你想对 TOC 也进行加密,则在 article.ejs 中将 TOC 的生成代码修改成如下:

```
<% if(post.toc == true){ %>
<div id="toc-div" class="toc-article" <% if (post.encrypt == true) { %>style="display:none" <% } %>>
<strong class="toc-title">Index</strong>
<% if (post.encrypt == true) { %>
<%- toc(post.origin, {list_number: true}) %>
<% } else { %>
<%- toc(post.content, {list_number: true}) %>
<% } %>
</div>
<% } %>
<%- post.content %>
```

+ 然后使用 *hexo clean && hexo g && hexo s*,来查看效果。


Expand Down Expand Up @@ -105,7 +123,29 @@ tags:
```
+ 这边要注意,标题一定要一致(前后空格无所谓)

###对于进阶使用

### 对 TOC 进行加密

如果你有一篇文章使用了 TOC,你需要修改模板的部分代码。这里用 landscape 作为例子:

+ 你可以在 *hexo/themes/landscape/layout/_partial/article.ejs* 找到 article.ejs。
+ 然后找到 <% post.content %> 这段代码,通常在30行左右。
+ 使用如下的代码来替代它:
```
<% if(post.toc == true){ %>
<div id="toc-div" class="toc-article" <% if (post.encrypt == true) { %>style="display:none" <% } %>>
<strong class="toc-title">Index</strong>
<% if (post.encrypt == true) { %>
<%- toc(post.origin, {list_number: true}) %>
<% } else { %>
<%- toc(post.content, {list_number: true}) %>
<% } %>
</div>
<% } %>
<%- post.content %>
```

### 修改加密模板

+ 如果你对默认的主题不满意,或者希望修改默认的提示和摘要内容,你可以添加如下配置在 *_config.yml* 中。

Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
1.~~Use template to create the blog page.~~
2.~~Complete the password checking.~~
3.~~Add config to the blog source instead of the total config file.~~
4. Encrypt the TOC of the blog.
4. ~~Encrypt the TOC of the blog.~~


##BUG
Expand Down
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ hexo.extend.filter.register("after_post_render", function (data) {
if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) {
return data;
}

if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // no such template
hexo.config.encrypt.default_template = '<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <div id="security"> <div> <div class="input-group"> <input type="text" class="form-control" aria-label="Enter the password." id="pass"/> <div class="input-group-btn"> <button type="button" class="btn btn-default" onclick="decryptAES()">Decrypt</button> </div> </div> </div> </div> <div id="encrypt-blog" style="display:none"> {{content}} </div>';
}
Expand All @@ -42,6 +41,11 @@ hexo.extend.filter.register("after_post_render", function (data) {
if ('password' in data && data.password) {
// use the blog's config first
console.log('encrypt the blog :' + data.title.trim());

// store the origin data
data.origin = data.content;
data.encrypt = true;

if (!('abstract' in data && data.abstract)) {
data.abstract = hexo.config.encrypt.default_abstract;
}
Expand All @@ -57,7 +61,7 @@ hexo.extend.filter.register("after_post_render", function (data) {
data.content = CryptoJS.enc.Base64.stringify(data.content);
data.content = CryptoJS.AES.encrypt(data.content, data.password).toString();
data.content = data.template.replace('{{content}}', data.content);
data.content = '<span class="hexo-blog-encrypt-message">' + data.message + '</span>' + data.content;
data.content = '<span id="encrypt-message">' + data.message + '</span>' + data.content;
data.content = '<script src="' + hexo.config.root + 'mcommon.js"></script>' + data.content;
data.content = '<script src="' + hexo.config.root + 'crypto-js.js"></script>' + data.content;

Expand All @@ -71,6 +75,11 @@ hexo.extend.filter.register("after_post_render", function (data) {
for (var i = 0, len = hexo.config.encrypt.blogs.length; i < len; i++) {
if ('blogs' in hexo.config.encrypt && data.title.trim() == hexo.config.encrypt.blogs[i].title.trim()) {
console.log('encrypt the blog :' + data.title.trim());

// store the origin data
data.origin = data.content;
data.encrypt = true;

if (!hexo.config.encrypt.blogs[i].abstract) {
hexo.config.encrypt.blogs[i].abstract = hexo.config.encrypt.default_abstract;
}
Expand All @@ -86,7 +95,7 @@ hexo.extend.filter.register("after_post_render", function (data) {
data.content = CryptoJS.enc.Base64.stringify(data.content);
data.content = CryptoJS.AES.encrypt(data.content, hexo.config.encrypt.blogs[i].password).toString();
data.content = hexo.config.encrypt.blogs[i].template.replace('{{content}}', data.content);
data.content = '<span class="hexo-blog-encrypt-message">' + hexo.config.encrypt.blogs[i].message + '</span>' + data.content;
data.content = '<span id="encrypt-message">' + hexo.config.encrypt.blogs[i].message + '</span>' + data.content;
data.content = '<script src="' + hexo.config.root + 'mcommon.js"></script>' + data.content;
data.content = '<script src="' + hexo.config.root + 'crypto-js.js"></script>' + data.content;

Expand Down
9 changes: 6 additions & 3 deletions lib/mcommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ function decryptAES() {
if (content == '') {
alert("密码错误!!");
} else {
document.getElementById("encrypt-blog").style.display="inline";
document.getElementById("encrypt-blog").innerHTML = content;
document.getElementById("security").style.display="none";
document.getElementById("encrypt-blog").style.display = "inline";
document.getElementById("encrypt-blog").innerHTML = content;
document.getElementById("encrypt-message").style.display = "none";

document.getElementById("security").style.display = "none";
document.getElementById("toc-div").style.display = "inline";
}
} catch (e) {
alert("密码错误!!");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "1.1.6"
"version": "1.1.7"
}

0 comments on commit 24d5089

Please sign in to comment.