hexo cards主题一些折腾

头像昵称同时显示 | 名称下今日诗词 | 添加代码块复制功能 | 添加 RSS 订阅功能 | 添加文章加密功能 | hexo cards 主题添加 rel=’me’标签进行mastodon身份验证


1. cards主题头像名称同时显示

打开文件cover.ejs修改 if else 语句,这里判断有头像 avatar 就不显示名称 sitename 了,

文件地址:\themes\cards\layout\_partial\source\cover.ejs

1
2
3
4
5
6
7
8
9
10
<div class="cover__logo">
<% if (theme.cover.avatar) { %>
<img no-lazy alt="author Image" class="cover__avatar" src="<%= url_for(theme.cover.avatar) %>">
<% } %>
<% if (theme.cover.sitename) { %>
<h1 class="cover__title" ><%= theme.cover.sitename %></h1>
<% } else if (config.title) { %>
<h1 class="cover__title" ><%= config.title %></h1>
<% } %>
</div>

注意:
如果参照博客 hexo cards主题添加背景图片 进行了修改,相同路径下的cover_banner.ejs也要对应修改。


2. 名称下今日诗词

打开文件cover.ejs,将此行注释掉<%- markdown(theme.cover.description) %>,并在上方插入<span id="jinrishici-sentence">今日诗词</span>

文件地址:\themes\cards\layout\_partial\source\cover.ejs

1
2
3
4
5
6
<% if (theme.cover.description) { %>
<div class="cover__intro">
<span id="jinrishici-sentence">今日诗词</span>
<!--首页logo下方文字 <%- markdown(theme.cover.description) %> -->
</div>
<% } %>

3. 添加代码块复制功能

创建clipboard_use.js

\themes\cards\source\js 目录下,新建文件clipboard_use.js,添加内容如下:

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
$(".highlight").wrap("<div class='code-wrapper' style='position:relative'></div>");
/*页面载入完成后,创建复制按钮*/
!function (e, t, a) {
/* code */
var initCopyCode = function () {
var copyHtml = '';
copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
copyHtml += ' <i class="fa fa-clipboard"></i><span>复制</span>';
copyHtml += '</button>';
/* $(".highlight .code").before(copyHtml); */

$(".code-wrapper .highlight table").before(copyHtml);
var clipboard = new ClipboardJS('.btn-copy', {
target: function (trigger) {
var tablee = trigger.nextElementSibling;
<!-- return trigger.nextElementSibling; -->
return tablee.firstElementChild.firstElementChild.firstElementChild.nextElementSibling;
}
});
clipboard.on('success', function (e) {
e.trigger.innerHTML = "<i class='fa fa-clipboard'></i><span>复制成功</span>"
setTimeout(function () {
e.trigger.innerHTML = "<i class='fa fa-clipboard'></i><span>复制</span>"
}, 1000)

e.clearSelection();
});
clipboard.on('error', function (e) {
e.trigger.innerHTML = "<i class='fa fa-clipboard'></i><span>复制失败</span>"
setTimeout(function () {
e.trigger.innerHTML = "<i class='fa fa-clipboard'></i><span>复制</span>"
}, 1000)
e.clearSelection();
});
}
initCopyCode();
}(window, document);

为按钮创建样式

\themes\cards\source\css\style\_base\markdown.styl 样式中添加如下代码:

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
/* 代码块复制按钮 */
.btn-copy {
display: inline-block;
cursor: pointer;
background-color: #eee;
background-image: linear-gradient(#fcfcfc, #eee);
border: 1px solid #d5d5d5;
border-radius: 3px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-appearance: none;
font-size: 13px;
font-weight: 700;
line-height: 20px;
color: #333;
-webkit-transition: opacity .3s ease-in-out;
-o-transition: opacity .3s ease-in-out;
transition: opacity .3s ease-in-out;
padding: 2px 6px;
position: absolute;
right: 5px;
top: 5px;
opacity: 0;
}

.btn-copy span {
margin-left: 5px
}

.code-wrapper:hover .btn-copy {
opacity: 1;
}

引入font-awesome

按钮上面添加了一个小图标美化,需要引入font-awesome

\themes\cards\layout\_partial\head\index.ejs中的之前添加如下代码:

1
<link rel="stylesheet" type="text/css" href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css">

最后,引用js

themes\cards\layout\layout.ejs 文件中的 标签前

也就是body标签之间的最后加入如下代码:

1
2
3
4
<!-- 代码块复制功能 -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.js"></script>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="/js/clipboard_use.js"></script>

参考链接

  1. 为Hexo主题增加代码块复制功能 http://ikaiqu.top/posts/f623adca.html

4. 添加 RSS 订阅功能

安装feed插件

npm install hexo-generator-feed --save

配置博客工程目录下的_ config.yml文件,启用插件

1
2
3
4
5
6
7
8
9
10
11
12
13
#Extensions
plugins:
hexo-generator-feed
#Feed Atom
feed:
type: atom
path: atom.xml
limit: 20
hub:
content:
content_limit:
content_limit_delim: ' '

配置cards主题中的_ config.yml文件

1
2
3
4
5
menu: 
- name: 首页
url: /
- name: RSS
url: /atom.xml

插件GitHub地址

https://github.com/hexojs/hexo-generator-feed


5. 添加文章加密功能

安装encrypt插件

npm install --save hexo-blog-encrypt

配置博客工程目录下的_ config.yml文件,启用插件

1
2
3
4
5
6
7
8
9
# Security
encrypt: # hexo-blog-encrypt
abstract: 有东西被加密了, 请输入密码查看.
message: 您好, 这里需要密码.
tags:
- {name: tagName, password: 密码A}
- {name: tagName, password: 密码B}
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
wrong_hash_message: 抱歉, 这个文章不能被校验, 不过您还是能看看解密后的内容.

快速使用

1
2
3
4
5
---
title: Hello World
date: 2016-03-30 21:18:02
password: hello
---

高级设置

文章信息头
1
2
3
4
5
6
7
8
9
10
11
---
title: Hello World
tags:
- 作为日记加密
date: 2016-03-30 21:12:21
password: mikemessi
abstract: 有东西被加密了, 请输入密码查看.
message: 您好, 这里需要密码.
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
wrong_hash_message: 抱歉, 这个文章不能被校验, 不过您还是能看看解密后的内容.
---
对博文禁用 Tag 加密

只需要将博文头部的 password 设置为 “” 即可取消 Tag 加密.

1
2
3
4
5
6
7
8
9
---
title: Callback Test
date: 2019-12-21 11:54:07
tags:
- A Tag should be encrypted
password: ""
---

Use a "" to diable tag encryption.
加密主题效果设置

default, blink, shrink, flip, up, surge, wave, xray

关键字theme,在_config.yml里或者 文章头 , 如下:

  1. 文章信息头
    1
    2
    3
    4
    5
    6
    ---
    title: Hello World
    date: 2016-03-30 21:12:21
    password: mikemessi
    theme: xray
    ---
  2. _config.yml
    1
    2
    3
    4
    5
    # Security
    encrypt: # hexo-blog-encrypt
    abstract: 有东西被加密了, 请输入密码查看.
    message: 您好, 这里需要密码.
    theme: xray

插件GitHub地址

https://github.com/D0n9X1n/hexo-blog-encrypt


6. hexo cards 主题添加 rel=’me’标签进行mastodon身份验证

  1. 在浏览器中F12找到自己想要插入mastodon标签的位置,再在cards中找到对应位置插入<a>标签
  2. 在hexo的config文件中,找到external_link改为如下配置:(添加域名白名单)
    1
    2
    3
    4
    external_link:
    enable: true # Open external links in new tab
    field: site # Apply to the whole site
    exclude: 'mastodon.social'

mastodon官方文档:

  1. 个人资料附加信息
  2. 链接验证

7. …


本文作者:Rozin

本文链接: https://blog.rozin.top/2024/01/22/hexocardssoso/

文章默认使用 CC BY-NC-SA 4.0 DEED 协议进行许可,使用时请注意遵守协议。

评论