hexo-theme-stellar/scripts/helpers/related_posts.js

55 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2021-02-19 23:33:19 +08:00
/**
* https://github.com/tea3/hexo-related-popular-posts/wiki/More-Settings#customize-html
*/
'use strict';
2021-02-21 03:13:21 +08:00
2021-02-19 23:33:19 +08:00
var util = require('hexo-util');
hexo.extend.helper.register('popular_posts_wrapper', function(args){
2021-02-21 03:13:21 +08:00
const title = args.title;
const json = args.json.json;
const cls = args.json.class;
if (json == undefined || json.length == 0) {
return '';
}
2021-02-19 23:33:19 +08:00
const cfg = hexo.theme.config.article.related_posts;
if (cfg.enable != true) return;
var returnHTML = "";
var div = `
<section class='header'>
2021-02-28 18:19:32 +08:00
<div class='title cap theme'>${title}</div>
2021-02-19 23:33:19 +08:00
</section>
<section class='body'>
`;
2021-03-08 15:26:52 +08:00
const posts = this.site.posts;
const root = this.config.root;
2021-02-19 23:33:19 +08:00
2022-10-23 16:56:27 +08:00
function listItem(obj){
2021-02-19 23:33:19 +08:00
var el = '';
2022-10-23 16:56:27 +08:00
el += '<a class="item" href="' + obj.path + '" title="' + obj.title + '">';
2022-11-19 16:08:50 +08:00
var p = posts.filter(p => root + p.path == obj.path)
2021-03-08 15:26:52 +08:00
if (p && p.length > 0) {
p = p.data[0];
}
2022-10-23 16:56:27 +08:00
el += '<span class="title">' + obj.title + '</span>';
if (obj.excerpt && obj.excerpt.length > 0) {
el += '<span class="excerpt">' + util.truncate(util.stripHTML(obj.excerpt), {length: 120}) + '</span>';
2021-02-19 23:33:19 +08:00
}
el += '</a>';
return el;
}
2022-10-23 16:56:27 +08:00
if (json.length > 0) {
for(var i = 0; i < json.length; i++) {
returnHTML += listItem(json[i]);
}
2021-02-19 23:33:19 +08:00
}
2022-10-23 16:56:27 +08:00
2021-02-21 03:13:21 +08:00
if (returnHTML != "") returnHTML = "<div class=\"" + cls + "\">" + returnHTML + "</div>";
2021-02-19 23:33:19 +08:00
div += returnHTML;
2021-03-06 20:44:17 +08:00
div += '</section>';
2021-02-19 23:33:19 +08:00
return div;
});