I want to add a random article picker to my blog as another way to discover past posts, aside from categories and tags on the right side and making it easier on mobile.
I couldn’t find a simple enough solution that suits my needs after brief Googling. The options I found were either to randomize at build time, which means the article doesn’t change with each click or refresh, or to generate a JSON index, which I had trouble with probably because my lack of understanding of Hugo.
I thought such a simple feature should be achievable with just JS + HTML, so I decided to write my own. Performance shouldn’t be an issue for a blog with a few tens to thousands of articles. Here’s a solution I came up with for reference.
我想给自己的博客加一个随机文章入口,作为页面右边的分类和 tag 之外的另一个发掘文章入口,移动端也更容易。在网上一搜,Google 第一页没有看到太显眼的简单适合我博客的答案:要么是最简单的 build time randomize,不能每次点击/刷新都出新文章;要么要生成 json 索引,对 hugo 语言和 config 不是很熟悉的我折腾了一下没搞出来,遂放弃。
想说这么简单个功能不至于不能纯拿 JS + html 写吧,博客少则几十多则几千的随机数性能应该也不成问题,随自己折腾出来个方案供大家参考。
大概是 2020 年底,在墙内社交平台辗转近二十年之后,我又重回了自建独立博客这条路的时候开始在长毛象上提 make blogging great again/博客伟大复兴这个有些自嘲意味的口号(毕竟无论中文还是英文语境里这俩句式都有不怎么招待见的政治意味)。这些年多多少少也试图安利一些友人写博客,最近换到静态博客也零散写了两篇建站倒腾,不过都收效甚微。毕竟,像小学语文老师反复强调的,光讲苹果真好吃苹果有益健康,不提具体什么口感怎么个有益健康法的是烂作文。