Files
hexo-theme-butterfly/scripts/helpers/aside_archives.js
Jerry f1397da086 chore: release v5.5.3
- 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: 页 -> 頁
2025-12-10 19:16:03 +08:00

114 lines
3.2 KiB
JavaScript

'use strict'
hexo.extend.helper.register('aside_archives', function (options = {}) {
const { config, page, site, url_for: urlFor, _p } = this
const { archive_dir: archiveDir, timezone, language } = config
// Destructure and set default options with object destructuring
const {
type = 'monthly',
format = type === 'monthly' ? 'MMMM YYYY' : 'YYYY',
show_count: showCount = true,
order = -1,
limit,
transform
} = options
// Optimize locale handling
const lang = toMomentLocale(page.lang || page.language || language)
// Early return if no posts
if (!site.posts.length) return ''
const archives = new Map()
site.posts.forEach(post => {
const date = post.date
const year = date.year()
const month = date.month() + 1
const key = type === 'yearly' ? year : `${year}-${month}`
if (archives.has(key)) {
archives.get(key).count++
} else {
archives.set(key, {
year,
month,
count: 1,
date // Store date object for later formatting
})
}
})
const data = Array.from(archives.values()).sort((a, b) => {
if (order === -1) {
return b.year - a.year || b.month - a.month
}
return a.year - b.year || a.month - b.month
})
// Format names after aggregation
data.forEach(item => {
let date = item.date.clone()
if (timezone) date = date.tz(timezone)
if (lang) date = date.locale(lang)
item.name = date.format(format)
delete item.date // Clean up
})
// Create link generator function
const createArchiveLink = item => {
let url = `${archiveDir}/${item.year}/`
if (type === 'monthly') {
url += item.month < 10 ? `0${item.month}/` : `${item.month}/`
}
return urlFor(url)
}
// Limit results efficiently
const limitedData = limit > 0 ? data.slice(0, Math.min(data.length, limit)) : data
// Use template literal for better readability
const archiveHeader = `
<div class="item-headline">
<i class="fas fa-archive"></i>
<span>${_p('aside.card_archives')}</span>
${
data.length > limitedData.length
? `<a class="card-more-btn" href="${urlFor(archiveDir)}/"
title="${_p('aside.more_button')}">
<i class="fas fa-angle-right"></i>
</a>`
: ''
}
</div>
`
// Use map for generating list items, join for performance
const archiveList = `
<ul class="card-archive-list">
${limitedData
.map(
item => `
<li class="card-archive-list-item">
<a class="card-archive-list-link" href="${createArchiveLink(item)}">
<span class="card-archive-list-date">
${transform ? transform(item.name) : item.name}
</span>
${showCount ? `<span class="card-archive-list-count">${item.count}</span>` : ''}
</a>
</li>
`
)
.join('')}
</ul>
`
return archiveHeader + archiveList
})
// Improved locale conversion function
const toMomentLocale = lang => {
if (!lang || ['en', 'default'].includes(lang)) return 'en'
return lang.toLowerCase().replace('_', '-')
}