@MrXiao
2018-12-06T13:54:10.000000Z
字数 1828
阅读 1676
博客
当有大段的代码直接展示在页面时,看起来臃肿且不便于观看,此时将代码折叠式最好的选择。下面是在Hexo的Next主题上添加折叠功能。
Next主题的主要js位于 themes/next/source/js/src/post-details.js
,在下面添加一下代码段:
$(document).ready(function(){
$(document).on('click', '.fold_hider', function(){
$('>.fold', this.parentNode).slideToggle();
$('>:first', this).toggleClass('open');
});
//默认情况下折叠
$("div.fold").css("display","none");
});
在主题scripts下添加一个tags.js,位于themes/next/scripts/tags.js
/*
@haohuawu
修复 Nunjucks 的 tag 里写 ```代码块```,最终都会渲染成 undefined 的问题
https://github.com/hexojs/hexo/issues/2400
*/
const rEscapeContent = /<escape(?:[^>]*)>([\s\S]*?)<\/escape>/g;
const placeholder = '\uFFFD';
const rPlaceholder = /(?:<|<)\!--\uFFFD(\d+)--(?:>|>)/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;
});
再继续添加一个fold.js,位于themes/next/scripts/fold.js
/* global hexo */
// Usage: {% fold ???? %} Something {% endfold %}
function fold (args, content) {
var text = args[0];
if(!text) text = "点击显/隐";
return '<div><div class="fold_hider"><div class="close hider_title">' + text + '</div></div><div class="fold">\n' + hexo.render.renderSync({text: content, engine: 'markdown'}) + '\n</div></div>';
}
hexo.extend.tag.register('fold', fold, {ends: true});
位于themes/next/source/css/_custom/custom.styl
.hider_title{
font-family: "Microsoft Yahei";
cursor: pointer;
color: red;
}
.close:after{
content: "▼";
}
.open:after{
content: "▲";
}
在我们需要折叠的地方前后添加便签,示例用法:
{% fold 点击显/隐内容 %}
something you want to fold, include code block.
{% endfold %}