Hexo添加复制代码块功能

Hexo博客中加入代码块复制功能

前言:

本人使用的是Hexo的NexT主题,NexT版本为v5.1.4。事实上,在NexT主题的v6.3版本里已经加入了代码复制这个功能,所以如果你刚开始使用NexT,直接升级主题,并在主题配置文件中打开代码复制的开关就好了。

  1. 添加copy-code.swig

    进入到下面的目录下

    cd themes/next/layout/_third-party/

    然后在此文件夹下创建名为copy-code.swig的文件,在此文件中输入以下内容:

    {% if theme.codeblock.copy_button.enable %}
      <style>
        .copy-btn {
          display: inline-block;
          padding: 6px 12px;
          font-size: 13px;
          font-weight: 700;
          line-height: 20px;
          color: #4D4D4C;
          white-space: nowrap;
          vertical-align: middle;
          cursor: pointer;
          background-color: #F7F7F7;
          background-image: linear-gradient(#F7F7F7, #F7F7F7);
          border: 1px solid #d5d5d5;
          border-radius: 3px;
          user-select: none;
          outline: 0;
        }
    
        .highlight-wrap .copy-btn {
          transition: opacity .3s ease-in-out;
          opacity: 0;
          padding: 2px 6px;
          position: absolute;
          right: 4px;
          top: 8px;
        }
    
        .highlight-wrap:hover .copy-btn,
        .highlight-wrap .copy-btn:focus {
          opacity: 1
        }
    
        .highlight-wrap {
          position: relative;
        }
      </style>
    
      <script>
        $('.highlight').each(function (i, e) {
          var $wrap = $('<div>').addClass('highlight-wrap')
          $(e).after($wrap)
          $wrap.append($('<button>').addClass('copy-btn').append('{{__("post.copy_button")}}').on('click', function (e) {
            var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
              return $(e).text()
            }).toArray().join('\n')
            var ta = document.createElement('textarea')
            document.body.appendChild(ta)
            ta.style.position = 'absolute'
            ta.style.top = '0px'
            ta.style.left = '0px'
            ta.value = code
            ta.select()
            ta.focus()
            var result = document.execCommand('copy')
            document.body.removeChild(ta)
            {% if theme.codeblock.copy_button.show_result %}
              if(result)$(this).text('{{__("post.copy_success")}}')
              else $(this).text('{{__("post.copy_failure")}}')
            {% endif %}
            $(this).blur()
          })).on('mouseleave', function (e) {
            var $b = $(this).find('.copy-btn')
            setTimeout(function () {
              $b.text('{{__("post.copy_button")}}')
            }, 300)
          }).append(e)
        })
      </script>
    {% endif %}
    

    然后返回上一层目录,即layout文件夹下,编辑_layout.swig在文件的最后面加入如下代码:

      {% include '_third-party/scroll-cookie.swig' %}
      {% include '_third-party/exturl.swig' %}
    
      {% include '_third-party/copy-code.swig' %}		# 要加入的代码
    </body>
    </html>
    
  2. 添加按钮上显示的语言

    进入themes/next/languages/目录下:

    zh-Hans.yml中的post下添加:

    copy_button: 复制
    copy_success: 复制成功
    copy_failure: 复制失败
    

    如图:

    在这里插入图片描述

    en.yml中的post下添加:

    copy_button: Copy
    copy_success: Copied
    copy_failure: Copy failed
    

    如图:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sEzbRDcg-1646212447764)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220224103052648.png)]

  3. 在主题配置文件中添加开关

    编辑主题配置文件themes/next/_config.yml),在其中的codeblock中添加copy_button的开关,

    添加的内容为:

    # Add copy button on codeblock
    copy_button:
      enable: true
      # Show text copy result
      show_result: true
    

在这里插入图片描述

需要注意的是nextV5.1.4的版本是没有codeblock选项的,则直接在配置文件中添加即可。一定要顶格写(如上图),否则不会生效。

  1. 退到博客根目录重新生成

    hexo g
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要为 Hexo 博客添加搜索功能,你可以使用第三方搜索引擎,如 Algolia 或 Google Custom Search。这里我们介绍如何使用 Algolia。 Algolia 是一个强大的搜索引擎,提供了易于使用的 API,可以轻松地将搜索功能添加到你的 Hexo 博客中。以下是如何为 Hexo 博客添加 Algolia 搜索的步骤: 1. 注册 Algolia 账户并创建一个应用程序。 2. 安装 Hexo Algolia 插件。 ``` npm install hexo-algolia --save ``` 3. 在博客根目录下创建一个名为 `algolia.json` 的配置文件,并填写以下内容: ``` { "applicationID": "YOUR_APP_ID", "apiKey": "YOUR_API_KEY", "indexName": "YOUR_INDEX_NAME" } ``` 将 `YOUR_APP_ID`、`YOUR_API_KEY` 和 `YOUR_INDEX_NAME` 替换为你在 Algolia 上创建的应用程序的信息。 4. 在你的 Hexo 主题中添加搜索框和搜索结果页面。 在主题的相应文件中添加以下代码: 搜索框: ```html <form class="search-form" method="get" action="/search/"> <input class="search-input" type="text" placeholder="Search" name="query"> <button class="search-submit" type="submit">Search</button> </form> ``` 搜索结果页面: ```html --- title: "Search Results" layout: "search" --- <section class="search-results"> {% for page in page.posts %} <article class="search-result"> <h2 class="search-result-title"><a href="{{ page.url }}">{{ page.title }}</a></h2> <p class="search-result-excerpt">{{ page.excerpt }}</p> </article> {% endfor %} </section> ``` 5. 重新生成站点并上传到你的服务器。 ``` hexo generate ``` 6. 同步你的博客数据到 Algolia 上。 ``` hexo algolia ``` 完成以上步骤后,你的 Hexo 博客就可以使用 Algolia 进行搜索了。当用户在搜索框中输入关键字并提交时,将会跳转到搜索结果页面,显示与关键字匹配的文章列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JiangPengCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值