mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2025-09-15 12:58:48 +08:00
2. Fix: valine lang not work 3. Fix: archives page site name not change when the language change 4. Fix: related posts url error
77 lines
2.4 KiB
JavaScript
77 lines
2.4 KiB
JavaScript
hexo.extend.helper.register('related_posts', function(currentPost, allPosts){
|
|
var relatedPosts = [];
|
|
currentPost.tags.forEach(function (tag) {
|
|
allPosts.forEach(function (post) {
|
|
if (isTagRelated(tag.name, post.tags)) {
|
|
var relatedPost = {
|
|
title: post.title,
|
|
path: post.path,
|
|
cover: post.cover,
|
|
weight: 1
|
|
};
|
|
var index = findItem(relatedPosts, 'path', post.path);
|
|
if (index != -1) {
|
|
relatedPosts[index].weight += 1;
|
|
} else{
|
|
if (currentPost.path != post.path) {
|
|
relatedPosts.push(relatedPost);
|
|
};
|
|
};
|
|
};
|
|
});
|
|
});
|
|
if (relatedPosts.length == 0) {return ''};
|
|
var result = "";
|
|
var limit_num = hexo.theme.config.related_post.limit || 6
|
|
relatedPosts = relatedPosts.sort(compare('weight'));
|
|
for (var i = 0; i < Math.min(relatedPosts.length, limit_num); i++) {
|
|
|
|
var cover = relatedPosts[i].cover|| random_cover()
|
|
result += '<div class="relatedPosts_item"><a href="' + hexo.theme.config.rootConfig.root + relatedPosts[i].path + '" title="' + relatedPosts[i].title +'">';
|
|
result += '<img class="relatedPosts_cover lozad" data-src="' + cover + '">';
|
|
result += '<div class="relatedPosts_title">' + relatedPosts[i].title + '</div>';
|
|
result += '</a></div>'
|
|
};
|
|
return result;
|
|
});
|
|
hexo.extend.helper.register('echo', function(path){
|
|
return path;
|
|
});
|
|
function isTagRelated (tagName, TBDtags) {
|
|
var result = false;
|
|
TBDtags.forEach(function (tag) {
|
|
if (tagName == tag.name) {
|
|
result = true;
|
|
};
|
|
})
|
|
return result;
|
|
}
|
|
function findItem (arrayToSearch, attr, val) {
|
|
for (var i = 0; i < arrayToSearch.length; i++) {
|
|
if (arrayToSearch[i][attr] == val) {
|
|
return i
|
|
};
|
|
};
|
|
return -1;
|
|
}
|
|
function compare (attr) {
|
|
return function (a, b) {
|
|
var val1 = a[attr];
|
|
var val2 = b[attr];
|
|
return val2 - val1;
|
|
}
|
|
}
|
|
|
|
function random_cover() {
|
|
var post_cover;
|
|
var num;
|
|
if (!Array.isArray(hexo.theme.config.default_cover)) {
|
|
post_cover = hexo.theme.config.default_cover
|
|
return post_cover
|
|
} else {
|
|
num = Math.floor(Math.random() * (hexo.theme.config.default_cover.length));
|
|
post_cover = hexo.theme.config.default_cover[num];
|
|
return post_cover
|
|
}
|
|
}
|