直接回答:在 Hexo 根目录建一个 scripts 文件夹,把插件代码放进去即可自动加载,不需要打包成 npm 包;在里面统一给页面补 title 就能解决首页标题不对的问题。
编写一个插件
在node_modules下新建一个文件夹,名字以
hero-开头插件内容
文件夹名为
hexo-title-usename1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18;
// match markdown image and covert to asset_img
hexo.extend.filter.register('before_post_render', function (data) {
if (data.layout!='post') {
return data
}
if (data.title == '' || !data.title) {
const l=data?.slug?.split("/")
data.title = l[l?.length - 1]
}
return data;
});
hexo.extend.filter.register('after_post_render', function(data){
return data;
});在主程序package.json中添加插件包
1
2
3"dependencies": {
"hexo-title-usename":"1.0.0",
}
更简便的方法,新建文件夹scripts将插件内容放这个文件夹下就可以了
关于文章内图片的处理
一、修改hexo原文件 hexo->lib->plugins->processor->post.js文件中的scanAssetDir函数
加入
1 | + const aa = post.asset_dir.split("/") |
二、 修改hex-renderer-marked插件中的image函数中的第一行改写href路径
1 | href = href.slice(1).replace(".assets","") |
三、 在文件顶部引入path
1 | -- href = url_for.call(hexo, href); |
关于文章链接中文的问题
修改主题文件夹下layout-includes>post>post-copyright.pug
1 | let url =decodeURIComponent(page.copyright_url || page.permalink) |
这篇笔记整理自我自己的实践记录,如果做法有出入,或者你踩过别的坑,欢迎到留言板一起聊聊。