对文章进行加密
前言
- 你可能需要写一些私密的博客, 通过密码验证的方式让人不能随意浏览.
hexo-blog-encrypt
插件就能很好地解决这个问题.
特性
- 一旦你输入了正确的密码, 它将会被存储在本地浏览器的localStorage中. 按个按钮, 密码将会被清空. 若博客中有脚本, 它将会被正确地执行.
- 支持按标签加密.
- 所有的核心功能都是由原生的 API 所提供的. 在 Node.js中, 我们使用 Crypto. 在浏览器中, 我们使用 Web Crypto API.
安装
- 用第一个即可
1 | npm install --save hexo-blog-encrypt |
快速使用
-
将 “password” 字段添加到您文章信息头就像这样:
1
2
3title: 恋爱小屋
date: 2023-05-23 22:43:03
password: hello -
再使用
hexo clean && hexo g && hexo s
在本地预览加密的文章.
高级设置
文章信息头
1 | --- |
_config.yml
1 | # Security |
对博文禁用 Tag 加密
只需要将博文头部的 password
设置为 ""
即可取消 Tag 加密.
1 | --- |
关于 Callback 函数
在部分博客中, 解密后部分元素可能无法正常显示或者表现, 这属于已知问题. 目前的解决办法是通过自行查阅自己的博客中的代码, 了解到在 onload 事件发生时调用了哪些函数, 并将这些函数挑选后写入到博客内容中. 如:
1 | --- |
对 TOC 进行加密
如果你有一篇文章使用了TOC,你需要修改模板的部分代码。这里用landscape 作为例子:
-
你可以在 hexo/themes/landscape/layout/_partial/article.ejs 找到 article.ejs。
-
然后找到 <% post.content %> 这段代码,通常在30行左右。
-
使用如下的代码来替代它:
1
2
3
4
5
6
7
8
9
10
11
12
13<% 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 %>
禁用 Log
If you want to disable the logging, you can add a silent property in _config.yml
and set it to true. 如果你想要禁止使用 Log, 你可以在 _config.yml
中增加一个 silent 属性, 并将其设置为 true.
1 | # Security |
这样就会禁止如 INFO hexo-blog-encrypt: encrypting "{Blog Name}" based on Tag: "EncryptedTag".
的日志.
加密主题
之前, 我们尝试使用 template
关键字来让用户能修改自己的主题. 后来发现真不是一个好主意. 所以我们现在引入了主题: theme
关键字.
你可以简单的使用 theme
在 _config.yml
里或者文章头, 如下:
在文章信息头
1 | --- |
在 _config.yml
1 | # Security |
你可以在线挑选你喜欢的主题,并应用到你的博客中:
结语
hexo-blog-encrypt仓库地址:https://github.com/D0n9X1n/hexo-blog-encrypt/