代码

没有请创建blog/scripts/random.js

1
2
3
4
5
6
7
8
9
10
11
hexo.extend.generator.register('random', function (locals) {
const config = hexo.config.random || {}
const posts = []
for (const post of locals.posts.data) {
if (post.random !== false) posts.push(post.path)
}
return {
path: config.path || 'random/index.html',
data: `<html><head><script>var posts=${JSON.stringify(posts)};window.open('/'+posts[Math.floor(Math.random() * posts.length)],"_self")</script></head></html>`
}
})

https://weilining.cf/random/

可选配置

_config.yml添加以下配置:

1
2
random:
path: # 随机链接路径,默认"random/index.html"

如果不想随机跳转到某篇文章,只需在这篇文章Front-matter添加random: false

参考

https://blog.hclonely.com/posts/57bd67ce/