hexo博文加密

介绍一种给博客文章加密的方法。

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

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

安装

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

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

然后在命令行中输入:

npm install

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

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

# Security
##
encrypt:
    enable: true

这样就可以使用插件了。

使用

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

---
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的文件替换为:

    `{% 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 %}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值