惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Troy Hunt's Blog
Cloudbric
Cloudbric
N
News | PayPal Newsroom
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
H
Hacker News: Front Page
Help Net Security
Help Net Security
S
Secure Thoughts
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
AI
AI
Hacker News: Ask HN
Hacker News: Ask HN
NISL@THU
NISL@THU
Last Week in AI
Last Week in AI
Forbes - Security
Forbes - Security
The GitHub Blog
The GitHub Blog
D
DataBreaches.Net
Scott Helme
Scott Helme
Jina AI
Jina AI
T
Threatpost
W
WeLiveSecurity
P
Palo Alto Networks Blog
F
Fortinet All Blogs
腾讯CDC
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
P
Privacy International News Feed
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
V
Vulnerabilities – Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
IT之家
IT之家
美团技术团队
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
T
Threat Research - Cisco Blogs
博客园 - 司徒正美

梦魇小栈

《羊了个羊》程序员过关攻略 WebRTC 入门指南 将你的博客升级为 PWA 渐进式Web离线应用 什么? 微信没有年度账单? 前端 nodejs 撸起来~ [接口实现] 记一下 pm2 常用配置及命令 (译)NPM vs Yarn 备忘手册 用 MySQL 导入 SQL 文件 解决 ubuntu 服务器中文乱码 面试分享:2018阿里巴巴前端面试总结(题目+答案) crontab 踩坑之绝对路径 SED 命令简明教程 在Hexo博客 NexT主题中部署Wildfire评论系统 使用 JavaScript 实现简单的拖拽 Merry Christmas JAVASCRIPT生成图形验证码 笔记:NPM版本号自增,自动化发布NPM包 不可不知的Mac OS X专用命令行工具(持续更新中) 继多说、网易关停之后该何去何从(网易云跟帖宣布2017年8月1日停止服务) 重新介绍 JavaScript(JS全面系列教程)
Hexo 博客美化代码块
Ihoey 心,若没有栖息的地方,到哪里都是流浪...... · 2018-05-27 · via 梦魇小栈

发表于 | 更新于 | 分类于 Hexo | 评论数: | 阅读次数:

最近有人问我博客的代码块是怎么做的,如下面的代码块,然后好久没有写文章了,趁着周末有时间就水一篇吧~

1
2
3
4
5
6
7
var arr = 'abcdaabc';

var info = arr
.split('')
.reduce((p, k) => (p[k]++ || (p[k] = 1), p), {});

console.log(info); //{ a: 3, b: 2, c: 2, d: 1 }

hexo 会有各种生命周期和事件,平时可能不会用到,但是能很好的利用的话,可以提高不少效率。比如文章多到一定程度之后,每次创建新文章都会被淹没在文件夹里面,在博客根目录下创建一个 scripts 文件夹,放一个 events.js 文件。这样每次通过hexo new post 创建新文章就会自动用 code 打开了~

1
2
3
4
5
6
7
var exec = require('child_process').exec;

// new 后自动打开编辑器
hexo.on('new', function (data) {
console.log('code ' + data.path);
exec('code ' + data.path);
});

又或者是在 hexo deploy 之后想做一些事情的时候也可以用到

1
2
3
4
5
6
7
try {
hexo.on('deployAfter', function () { //当deploy完成后执行备份
doSomething();
});
} catch (e) {
console.log("产生了一个错误<( ̄3 ̄)> !,错误详情为:" + e.toString());
}

代码块也是利用了 hexoapi,是在主题目录下面的 scripts 文件夹,我创建了一个 codeblock.js 文件。监听 after_post_render 事件,(这个事件并不是每次都触发,hexo 会做缓存,在没有缓存的情况下才会执行。)通过事件回调替换文章渲染出来的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var attributes = [
'autocomplete="off"',
'autocorrect="off"',
'autocapitalize="off"',
'spellcheck="false"',
'contenteditable="true"'
]

var attributesStr = attributes.join(' ')

hexo.extend.filter.register('after_post_render', function (data) {
while (/<figure class="highlight ([a-zA-Z]+)">.*?<\/figure>/.test(data.content)) {
data.content = data.content.replace(/<figure class="highlight ([a-zA-Z]+)">.*?<\/figure>/, function () {
var language = RegExp.$1 || 'plain'
var lastMatch = RegExp.lastMatch
lastMatch = lastMatch.replace(/<figure class="highlight /, '<figure class="iseeu highlight /')
return '<div class="highlight-wrap"' + attributesStr + 'data-rel="' + language.toUpperCase() + '">' + lastMatch + '</div>'
})
}
return data
})

然后在 highlight.js 的基础上调整下样式,包裹上一层类 mac Panel 的效果。

blog/themes/next/source/css/_custom 目录下新建一个 .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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
.highlight-wrap[data-rel] {
position: relative;
overflow: hidden;
border-radius: 5px;
box-shadow: 0 10px 30px 0px rgba(0, 0, 0, 0.4);
margin: 35px 0;
::-webkit-scrollbar {
height: 10px;
}

::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
border-radius: 10px;
}

::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
}
&::before {
color: white;
content: attr(data-rel);
height: 38px;
line-height: 38px;
background: #21252b;
color: #fff;
font-size: 16px;
position: absolute;
top: 0;
left: 0;
width: 100%;
font-family: 'Source Sans Pro', sans-serif;
font-weight: bold;
padding: 0px 80px;
text-indent: 15px;
float: left;
}
&::after {
content: ' ';
position: absolute;
-webkit-border-radius: 50%;
border-radius: 50%;
background: #fc625d;
width: 12px;
height: 12px;
top: 0;
left: 20px;
margin-top: 13px;
-webkit-box-shadow: 20px 0px #fdbc40, 40px 0px #35cd4b;
box-shadow: 20px 0px #fdbc40, 40px 0px #35cd4b;
z-index: 3;
}
}

然后在同目录 custom.styl 文件中引入新建的样式文件即可

最后修改主题的代码样式配置文件

1
highlight_theme: night eighties

OK,大功告成~

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2t8yu2xdpn280

满分是10分的话,这篇文章你给几分,您的支持将鼓励我继续创作!