问题描述
我现在正在尝试在旧的hexo博客上安装hexo-theme-next
的8.0.2版本。
在旧版本的hexo-theme-next
中,我只能将.twig文件更改为具有自定义的索引页,但是对于8.0.2,似乎只能将新的部分插入到特定位置。
我想要的是像这样更改(旧版本index.twig中的代码的一部分):
{% block class %}index posts-expand{% endblock %}
对此:
{% block class %}
{%- if config.content.mode == normala %}
index posts-expand
{%- elif config.content.mode == montage %}
index posts-montage
{%- endif %}
{% endblock %}
解决方法
最后,我从主题下一步社区得到了答案。
如果您想做我上面提到的事情,唯一的方法是使用js String.replace方法(当然还有正则表达式规则);
下面是删除默认的“ power-by”字符串的示例:
let removeRedundantString = (str) => {
str = str.replace(/(<div class="powered-by">).*(<\/div>)/s,``);
str = str.replace(/(<span class="with-love">).*(<span class="author")/s,`$2`);
return str
}
hexo.extend.filter.register('after_render:html',(str,data) => {
//here data is an object that have information about your blog within
// you can try console.log data to see is inside it.
if (data.config.theme_config.no_hexo_credit) {
str = removeRedundantString(str);
}
return str;
})
,您应该将这些代码另存为Hexo根目录/脚本中的文件,就像
/scripts/xxx.js
(如果没有,您应该自己创建/ scripts文件夹,并且js文件没有命名规则,只需将它们放在该文件夹中,十六进制渲染器将在编译时将其插入到html中)