/* eslint-disable camelcase */ /** * Butterfly * Related Posts * According the tag */ 'use strict' const { postDesc } = require('../common/postDesc') hexo.extend.helper.register('related_posts', function (currentPost) { const relatedPosts = new Map() const tagsData = currentPost.tags if (!tagsData || !tagsData.length) return '' tagsData.forEach(tag => { const posts = tag.posts posts.forEach(post => { if (currentPost.path === post.path) return if (relatedPosts.has(post.path)) { relatedPosts.get(post.path).weight += 1 } else { const getPostDesc = post.postDesc || postDesc(post, hexo) relatedPosts.set(post.path, { title: post.title, path: post.path, cover: post.cover, cover_type: post.cover_type, weight: 1, updated: post.updated, created: post.date, postDesc: getPostDesc, random: Math.random() }) } }) }) if (relatedPosts.size === 0) { return '' } const hexoConfig = hexo.config const config = hexo.theme.config const limitNum = config.related_post.limit || 6 const dateType = config.related_post.date_type || 'created' const headlineLang = this._p('post.recommend') const relatedPostsList = Array.from(relatedPosts.values()).sort((a, b) => { if (b.weight !== a.weight) { return b.weight - a.weight } return b.random - a.random }) let result = '