mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-08 12:07:06 +08:00
- Bump version from 5.5.3-b2 to 5.5.3 in package.json - Update third-party dependencies: * algolia_search: 5.43.0 -> 5.46.0 * docsearch: 4.3.1 -> 4.3.2 * fancybox: 6.1.4 -> 6.1.7 * katex: 0.16.25 -> 0.16.27 * mermaid: 11.12.1 -> 11.12.2 * waline: 3.7.1 -> 3.8.0 perf: optimize JavaScript performance - Add defer attribute to script tags in pjax and prismjs - Improve DOM content loading timing in pjax - Optimize utilities with better throttle implementation - Cache header positions for TOC performance - Optimize related posts generation with Maps - Improve archive helpers performance fix: improve error handling and UI fixes - Replace process.exit with proper error throwing - Fix tooltip positioning with boundary checks - Add btn-effects to readmode exit button - Fix element height calculation for hidden elements - Improve image filters in dark mode (brightness .88, contrast .95) style: code improvements and consistency - Refactor random cover generation with generator pattern - Optimize data processing in helpers - Clean up unused functions and improve code structure - Fix Chinese translation: 页 -> 頁
92 lines
3.0 KiB
JavaScript
92 lines
3.0 KiB
JavaScript
/* 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 = '<div class="relatedPosts">'
|
|
result += `<div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>${headlineLang}</span></div>`
|
|
result += '<div class="relatedPosts-list">'
|
|
|
|
for (let i = 0; i < Math.min(relatedPostsList.length, limitNum); i++) {
|
|
let { cover, title, path, cover_type, created, updated, postDesc } = relatedPostsList[i]
|
|
const { escape_html, url_for, date } = this
|
|
cover = cover || 'var(--default-bg-color)'
|
|
title = escape_html(title)
|
|
const className = postDesc ? 'pagination-related' : 'pagination-related no-desc'
|
|
result += `<a class="${className}" href="${url_for(path)}" title="${title}">`
|
|
if (cover_type === 'img') {
|
|
result += `<img class="cover" src="${url_for(cover)}" alt="cover">`
|
|
} else {
|
|
result += `<div class="cover" style="background: ${cover}"></div>`
|
|
}
|
|
if (dateType === 'created') {
|
|
result += `<div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> ${date(created, hexoConfig.date_format)}</div>`
|
|
} else {
|
|
result += `<div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="fas fa-history fa-fw"></i> ${date(updated, hexoConfig.date_format)}</div>`
|
|
}
|
|
result += `<div class="info-item-2">${title}</div></div>`
|
|
|
|
if (postDesc) {
|
|
result += `<div class="info-2"><div class="info-item-1">${postDesc}</div></div>`
|
|
}
|
|
result += '</div></a>'
|
|
}
|
|
|
|
result += '</div></div>'
|
|
return result
|
|
})
|