截至目前(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插件实现
如果你不需要自定义,请前往上述链接进行了解和自动配置
EJS相关自定义
给出默认模板进行举例(由于篇幅限制,不包含CSS)
步骤1:关闭自动注入
在博客根目录的配置文件中请您关闭自动注入选项
1 2
| article_recommender: inject: false
|
步骤2:添加入口
在之后在对应的post模板中(\themes\you-theme-name\layout\post.ejs)以下以themes/your-theme/layout/_partial/article-recommender.ejs为例找到想注入的地方(自己喜欢在哪就添加在哪)添加作为模板入口,其中include内容要指向下一步要添加的recommended-posts.ejs文件路径
1 2 3 4 5 6 7 8
| <% const cfg = config.article_recommender || {}; const _ar_data = { posts: recommended_posts_full(page, (cfg.default_count) || 3), recommendedTitle: cfg.recommended_title || '推荐文章主题推荐' }; %> <%- partial('_partial/article-recommender', _ar_data) %>
|
步骤3:创建模板
由于步骤二中我们写了'_partial/article-recommender'所以我们在themes/your-theme/layout/_partial/article-recommender.ejs(如果没有请自行创建文件夹和文件)
内容为
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
| <% if (posts.length) { %> <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 %></span> </div> </div> <div class="hexo-article-recommender-posts"> <% posts.forEach((link, idx) => { %> <% const bgColors = ['#6a89cc, #4a69bd', '#e55039, #eb2f06', '#3dc1d3, #0fb9b1', '#f6b93b, #e55039'] %> <% const avatarColors = ['#4a69bd', '#e55039', '#3dc1d3', '#f6b93b'] %> <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 <%= link._recommendType === 'related' ? 'related' : 'fallback' %>"> <%= link._recommendType === 'related' ? '相关' : '推荐' %> <span class="hexo-article-recommender-score">(<%= (link._recommendScore).toFixed(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 || link.reading_time || (link.symbols_count_time && link.symbols_count_time.mins) || '阅读' %>分钟 </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 || (link.excerpt && strip_html(link.excerpt)) || (link.content && strip_html(link.content)) || '点击查看推荐内容。').substring(0, 80) %> </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 || link.author_name || link.author_id || '本站作者' %></div> <div class="hexo-article-recommender-author-title"><%= link.author_title || '原创文章' %></div> </div> </div> </div> </div> <% }) %> </div> <h2 class="hexo-article-recommender-section-over">--- over ---</h2> </div> <% } %>
|
由于样式过多就没加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一个想要的文章推荐列表。