截至目前(2025 年),Hexo生态中主流的、活跃维护的、具备“文章推荐”功能的插件相对较少,且多数方案较为简单(如仅基于标签或分类匹配)。hexo-related-popular-posts也是已经年久失修,而hexo-recommended-posts也是五年以前的插件需要外部服务器提供api,所以我自己做了一个插件来实现这一功能。
项目地址:https://github.com/i-NMB/hexo-article-recommender 欢迎 Star、Issue 与 PR!
特点:
纯本地运行,无网络请求;
融合语义、标题、标签、分类、时效性五大维度;
提供三种策略(balanced / title-focused / content-focused);
输出推荐分数、关系类型、阅读时长等元数据;
内置响应式卡片,支持自动注入或手动调用;
兼容 Hexo 7.0+,无外部依赖。
适用人群:追求智能推荐、隐私安全、开箱即用体验的用户。
效果/安装/主要配置 该插件的效果安装和主要配置请见:Hexo博客文章底部添加推荐相关文章:使用hexo-article-recommender插件实现
如果你不需要自定义,请前往上述链接进行了解和自动配置
Nunjucks(NJK)相关自定义 给出默认模板进行举例(由于篇幅限制,不包含CSS)
步骤1:关闭自动注入 在博客根目录的配置文件中请您关闭自动注入选项
1 2 article_recommender: inject: false
步骤2:添加入口 在之后在对应的post模板中(/themes/you-theme-name/layout/post.njk)以下以themes/your-theme/layout/_partials/recommended-posts.njk为例找到想注入的地方(自己喜欢在哪就添加在哪)添加作为模板入口 ,其中include内容要指向下一步要添加的recommended-posts.njk文件路径
1 2 3 4 5 6 7 8 {% set cfg = config.article_recommender or {} %} {% set _ar_data = { posts: recommended_posts_full(page, cfg.default_count or 3), recommendedTitle: cfg.recommended_title or '推荐文章主题推荐' } %} {% set posts = _ar_data.posts %} {% set recommendedTitle = _ar_data.recommendedTitle %} {% include '_partials/recommended-posts.njk' %}
步骤3:创建模板 由于步骤二中我们写了'_partials/recommended-posts.njk'那么我们在themes/your-theme/layout/_partials/recommended-posts.njk(如果没有请自行创建文件夹和文件,根据自己的博客主题为例)
内容为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 {%- if posts and posts.length > 0 -%} <div class="hexo-article-recommender-section"> <div class="hexo-article-recommender-section-divider"> <div class="hexo-article-recommender-divider-line"></div> <div class="hexo-article-recommender-divider-text"> <span>{{ recommendedTitle or '推荐文章' }}</span> </div> </div> <div class="hexo-article-recommender-posts"> {%- for link in posts -%} {%- set bgColors = [ '#6a89cc, #4a69bd', '#e55039, #eb2f06', '#3dc1d3, #0fb9b1', '#f6b93b, #e55039' ] -%} {%- set avatarColors = ['#4a69bd', '#e55039', '#3dc1d3', '#f6b93b'] -%} {%- set idx = loop.index0 -%} <div class="hexo-article-recommender-post-card" data-bg="{{ bgColors[idx % bgColors.length] }}"> <div class="hexo-article-recommender-site-tag">站内</div> <div class="hexo-article-recommender-relation-tag {{ 'related' if link._recommendType == 'related' else 'fallback' }}"> {{ '相关' if link._recommendType == 'related' else '推荐' }} <span class="hexo-article-recommender-score">({{ link._recommendScore | round(1) }}%)</span> </div> <div class="hexo-article-recommender-card-header"> <div class="hexo-article-recommender-reading-time"> <i class="fas fa-clock"></i> {{ link.minutes or link.reading_time or (link.symbols_count_time and link.symbols_count_time.mins) or '阅读' }}分钟 </div> </div> <div class="hexo-article-recommender-card-content"> <h4 class="hexo-article-recommender-post-title"> <a href="{{ url_for(link.path) }}">{{ link.title }}</a> </h4> <p class="hexo-article-recommender-post-excerpt"> {{ (link.description or (link.excerpt and strip_html(link.excerpt)) or (link.content and strip_html(link.content)) or '点击查看推荐内容。') | truncate(80, false) }} </p> <div class="hexo-article-recommender-author"> <div class="hexo-article-recommender-author-avatar" style="background: {{ avatarColors[idx % avatarColors.length] }};"> {{ idx + 1 }} </div> <div class="hexo-article-recommender-author-info"> <div class="hexo-article-recommender-author-name">{{ link.author or link.author_name or link.author_id or '本站作者' }}</div> <div class="hexo-article-recommender-author-title">{{ link.author_title or '原创文章' }}</div> </div> </div> </div> </div> {%- endfor -%} </div> <h2 class="hexo-article-recommender-section-over">--- over ---</h2> </div> {%- endif -%}
由于样式过多就没加CSS样式了 ,主要是给出一个抛砖引玉的机会 ,给出了内容关联性(link._recommendType用来判断推荐还是相关)、推荐相关的分数((link._recommendScore).toFixed(1))、以及参考阅读时间(link.minutes || link.reading_time || (link.symbols_count_time && link.symbols_count_time.mins))、还有链接地址与标题(url_for(link.path)和link.title)、文章的摘要(link.description)、文章作者(link.author)、作者介绍(link.author_title)等可以直接用来引用的代码。
您可以自行DIY一个自己想要的文章推荐列表。
步骤4:本地验证 使用hexo cl && hexo g && hexo s查看是否生效