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

60 lines
1.6 KiB
JavaScript
Raw 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 = `
<div class="related_posts">
<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'>
`;
function generateHTML(list){
var el = '';
el += '<a class="item" href="' + list.path + '" title="' + list.title + '" rel="bookmark ">';
if (cfg.placeholder_img && cfg.placeholder_img.length > 0) {
el += '<div class="img">'
if (list.img && list.img != "") {
el += '<img src="' + list.img + '" />';
} else {
el += '<img src="' + cfg.placeholder_img + '" />';
}
el += '</div>';
}
el += '<span class="title">' + list.title + '</span>';
if (list.excerpt && list.excerpt.length > 0) {
2021-02-21 03:13:21 +08:00
el += '<span class="excerpt">' + util.truncate(util.stripHTML(list.excerpt), {length: 120}) + '</span>';
2021-02-19 23:33:19 +08:00
}
el += '</a>';
return el;
}
2021-02-21 03:13:21 +08:00
for(var i = 0; i < json.length; i++) {
returnHTML += generateHTML(json[i]);
2021-02-19 23:33:19 +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;
div += '</section></div>';
return div;
});