很多时候当我们写了一篇博客,但并不想所有人能够访问它。对于WordPress这很容易做到,但是对于hexo,由于是静态网页,并不能做到完全的加密。

在GitHub上发现了有个人做了一个加密的插件,还挺好用,推荐给大家。

安装

在你的hexo根目录的package.json文件夹中添加:

“hexo-blog-encrypt”: “2.0.*”

然后在命令行中输入:

npm install

这样这个插件就安装好了。

找到根目录下的_config.yml文件,添加如下:

1
2
3
4
# Security
##
encrypt:
    enable: true

这样就可以使用插件了。

使用

在你要加密的文章头部写入password,例如:

1
2
3
4
5
6
7
---
title: Hello World
date: 2016-03-30 21:18:02
password: abc123
abstract: Welcome to my blog, enter password to read.
message: Welcome to my blog, enter password to read.
---

这样就可以需要输入密码访问了。

Bugs

  • 对于hexo-blog-encrypt2.0之前的版本,无法触发渲染mathjax的函数,需要进行升级。

  • 如果想对TOC进行加密,以next主题为例,将next/layout/_macro/sidebar.swig的文件替换为:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  `{% macro render(is_post) %}
    <div class="sidebar-toggle">
      <div class="sidebar-toggle-line-wrap">
        <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
        <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
        <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
      </div>
    </div>

    <aside id="sidebar" class="sidebar">
      <div class="sidebar-inner">

        {% set display_toc = is_post and theme.toc.enable %}
        {% if page.encrypt == true %}
          {% if display_toc and toc(page.origin).length > 1 %}
            <ul class="sidebar-nav motion-element">
              <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
                {{ __('sidebar.toc') }}
              </li>
              <li class="sidebar-nav-overview" data-target="site-overview">
                {{ __('sidebar.overview') }}
              </li>
            </ul>
          {% endif %}
        {% else %}
          {% if display_toc and toc(page.content).length > 1 %}
            <ul class="sidebar-nav motion-element">
              <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
                {{ __('sidebar.toc') }}
              </li>
              <li class="sidebar-nav-overview" data-target="site-overview">
                {{ __('sidebar.overview') }}
              </li>
            </ul>
          {% endif %}
        {% endif %}

        <section class="site-overview sidebar-panel{% if not display_toc or toc(page.content).length <= 1 %} sidebar-panel-active{% endif %}">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            <img class="site-author-image" itemprop="image"
                 src="{{ url_for( theme.avatar | default(theme.images + '/avatar.gif') ) }}"
                 alt="{{ theme.author }}" />
            <p class="site-author-name" itemprop="name">{{ theme.author }}</p>
             {% if theme.seo %}
                <p class="site-description motion-element" itemprop="description">{{ theme.signature }}</p>
             {% else %}
                <p class="site-description motion-element" itemprop="description">{{ theme.description }}</p>
            {% endif %}
          </div>
          <nav class="site-state motion-element">
    
            {% if config.archive_dir != '/' %}
              <div class="site-state-item site-state-posts">
                <a href="{{ url_for(theme.menu.archives) }}">
                  <span class="site-state-item-count">{{ site.posts.length }}</span>
                  <span class="site-state-item-name">{{ __('state.posts') }}</span>
                </a>
              </div>
            {% endif %}
    
            {% if site.categories.length > 0 %}
              {% set categoriesPageQuery = site.pages.find({type: 'categories'}, {lean: true}) %}
              {% set hasCategoriesPage = categoriesPageQuery.length > 0 %}
              <div class="site-state-item site-state-categories">
                {% if hasCategoriesPage %}<a href="{{ url_for(categoriesPageQuery[0].path) }}">{% endif %}
                  <span class="site-state-item-count">{{ site.categories.length }}</span>
                  <span class="site-state-item-name">{{ __('state.categories') }}</span>
                {% if hasCategoriesPage %}</a>{% endif %}
              </div>
            {% endif %}
    
            {% if site.tags.length > 0 %}
              {% set tagsPageQuery = site.pages.find({type: 'tags'}, {lean: true}) %}
              {% set hasTagsPage = tagsPageQuery.length > 0 %}
              <div class="site-state-item site-state-tags">
                {% if hasTagsPage %}<a href="{{ url_for(tagsPageQuery[0].path) }}">{% endif %}
                  <span class="site-state-item-count">{{ site.tags.length }}</span>
                  <span class="site-state-item-name">{{ __('state.tags') }}</span>
                {% if hasTagsPage %}</a>{% endif %}
              </div>
            {% endif %}
    
          </nav>
    
          {% if theme.rss %}
            <div class="feed-link motion-element">
              <a href="{{ url_for(theme.rss) }}" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          {% endif %}
    
          <div class="links-of-author motion-element">
            {% if theme.social %}
              {% for name, link in theme.social %}
                <span class="links-of-author-item">
                  <a href="{{ link }}" target="_blank" title="{{ name }}">
                    {% if theme.social_icons.enable %}
                      <i class="fa fa-fw fa-{{ theme.social_icons[name] | default('globe') | lower }}"></i>
                    {% endif %}
                    {{ name }}
                  </a>
                </span>
              {% endfor %}
            {% endif %}
          </div>
    
          {% set cc = {'by': 1, 'by-nc': 1, 'by-nc-nd': 1, 'by-nc-sa': 1, 'by-nd': 1, 'by-sa': 1, 'zero': 1} %}
          {% if theme.creative_commons in cc %}
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/{% if theme.creative_commons === 'zero' %}publicdomain/zero/1.0{% else %}licenses/{{ theme.creative_commons }}/4.0{% endif %}/" class="cc-opacity" target="_blank">
                <img src="{{ url_for(theme.images) }}/cc-{{ theme.creative_commons }}.svg" alt="Creative Commons" />
              </a>
            </div>
          {% endif %}
    
          {# Blogroll #}
          {% if theme.links %}
            <div class="links-of-blogroll motion-element {{ "links-of-blogroll-" + theme.links_layout | default('inline') }}">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-{{ theme.links_icon | default('globe') | lower }}"></i>
                {{ theme.links_title }}
              </div>
              <ul class="links-of-blogroll-list">
                {% for name, link in theme.links %}
                  <li class="links-of-blogroll-item">
                    <a href="{{ link }}" title="{{ name }}" target="_blank">{{ name }}</a>
                  </li>
                {% endfor %}
              </ul>
            </div>
          {% endif %}
    
          {% include '../_custom/sidebar.swig' %}
    
        </section>
        {% if page.encrypt == true %}
          {% if display_toc and toc(page.origin).length > 1 %}
          <!--noindex-->
            <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
              <div class="post-toc">
    
                {% if page.toc_number === undefined %}
                  {% set toc = toc(page.origin, { "class": "nav", list_number: theme.toc.number }) %}
                {% else %}
                  {% set toc = toc(page.origin, { "class": "nav", list_number: page.toc_number }) %}
                {% endif %}
    
                {% if toc.length <= 1 %}
                  <p class="post-toc-empty">{{ __('post.toc_empty') }}</p>
                {% else %}
                  <div class="post-toc-content">{{ toc }}</div>
                {% endif %}
    
              </div>
            </section>
          <!--/noindex-->
          {% endif %}
        {% else %}
          {% if display_toc and toc(page.content).length > 1 %}
          <!--noindex-->
            <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
              <div class="post-toc">
    
                {% if page.toc_number === undefined %}
                  {% set toc = toc(page.content, { "class": "nav", list_number: theme.toc.number }) %}
                {% else %}
                  {% set toc = toc(page.content, { "class": "nav", list_number: page.toc_number }) %}
                {% endif %}
    
                {% if toc.length <= 1 %}
                  <p class="post-toc-empty">{{ __('post.toc_empty') }}</p>
                {% else %}
                  <div class="post-toc-content">{{ toc }}</div>
                {% endif %}
    
              </div>
            </section>
          <!--/noindex-->
          {% endif %}
        {% endif %}
    
        {% if theme.sidebar.b2t %}
          <div class="back-to-top">
            <i class="fa fa-arrow-up"></i>
            {% if theme.sidebar.scrollpercent %}
              <span id="scrollpercent"><span>0</span>%</span>
            {% endif %}
          </div>
        {% endif %}
    
      </div>
    </aside>
  {% endmacro %}