hexo博客配置-自建标签Bug解决方案

本文所用的架构或思路参考该文:hexo博客配置-主题管理与配置

修复自建tag里写代码块,渲染成undefined的问题

只需添加文件hexo-theme-next/scripts/custom/debugCustom.js如下

debugCustom.jsview raw
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
//ledong修复自建tag里写 ```代码块```,渲染成 undefined 的问题: 
// https://www.oyohyee.com/post/Note/hexo_tag/
/*
@haohuawu
修复 Nunjucks 的 tag 里写 ```代码块```,最终都会渲染成 undefined 的问题
https://github.com/hexojs/hexo/issues/2400
*/
const rEscapeContent = /<escape(?:[^>]*)>([\s\S]*?)<\/escape>/g;
const placeholder = '\uFFFD';
const rPlaceholder = /(?:<|&lt;)\!--\uFFFD(\d+)--(?:>|&gt;)/g;
const cache = [];
function escapeContent(str) {
return '<!--' + placeholder + (cache.push(str) - 1) + '-->';
}
hexo.extend.filter.register('before_post_render', function(data) {
data.content = data.content.replace(rEscapeContent, function(match, content) {
return escapeContent(content);
});
return data;
});
hexo.extend.filter.register('after_post_render', function(data) {
data.content = data.content.replace(rPlaceholder, function() {
return cache[arguments[1]];
});
return data;
});

参考

参考1:Hexo添加代码块折叠

参考2:Hexo自建标签渲染代码段Bug解决方案

Donate comment here
xiaotiandi 微信支付

微信支付

xiaotiandi 支付宝

支付宝

xiaotiandi

公益(commonweal)微信