update theme to butterfly
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
hexo.extend.helper.register('aside_archives', function (options = {}) {
|
||||
const { config, page, site, url_for, _p } = this
|
||||
const { config, page, site, url_for: urlFor, _p } = this
|
||||
const { archive_dir: archiveDir, timezone, language } = config
|
||||
|
||||
// Destructure and set default options with object destructuring
|
||||
@@ -17,56 +17,43 @@ hexo.extend.helper.register('aside_archives', function (options = {}) {
|
||||
// Optimize locale handling
|
||||
const lang = toMomentLocale(page.lang || page.language || language)
|
||||
|
||||
// Memoize comparison function to improve performance
|
||||
const compareFunc =
|
||||
type === 'monthly'
|
||||
? (yearA, monthA, yearB, monthB) => yearA === yearB && monthA === monthB
|
||||
: (yearA, yearB) => yearA === yearB
|
||||
|
||||
// Early return if no posts
|
||||
if (!site.posts.length) return ''
|
||||
|
||||
// Use reduce for more efficient data processing
|
||||
const data = site.posts.sort('date', order).reduce((acc, post) => {
|
||||
let date = post.date.clone()
|
||||
if (timezone) date = date.tz(timezone)
|
||||
|
||||
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 (lang) date = date.locale(lang)
|
||||
|
||||
// Find or create archive entry
|
||||
const lastEntry = acc[acc.length - 1]
|
||||
|
||||
if (type === 'yearly') {
|
||||
const existingYearIndex = acc.findIndex(entry => entry.year === year)
|
||||
if (existingYearIndex !== -1) {
|
||||
acc[existingYearIndex].count++
|
||||
} else {
|
||||
// 否則創建新條目
|
||||
acc.push({
|
||||
name: date.format(format),
|
||||
year,
|
||||
month,
|
||||
count: 1
|
||||
})
|
||||
}
|
||||
if (archives.has(key)) {
|
||||
archives.get(key).count++
|
||||
} else {
|
||||
if (!lastEntry || !compareFunc(lastEntry.year, lastEntry.month, year, month)) {
|
||||
acc.push({
|
||||
name: date.format(format),
|
||||
year,
|
||||
month,
|
||||
count: 1
|
||||
})
|
||||
} else {
|
||||
lastEntry.count++
|
||||
}
|
||||
archives.set(key, {
|
||||
year,
|
||||
month,
|
||||
count: 1,
|
||||
date // Store date object for later formatting
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return acc
|
||||
}, [])
|
||||
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 => {
|
||||
@@ -74,7 +61,7 @@ hexo.extend.helper.register('aside_archives', function (options = {}) {
|
||||
if (type === 'monthly') {
|
||||
url += item.month < 10 ? `0${item.month}/` : `${item.month}/`
|
||||
}
|
||||
return url_for(url)
|
||||
return urlFor(url)
|
||||
}
|
||||
|
||||
// Limit results efficiently
|
||||
@@ -87,7 +74,7 @@ hexo.extend.helper.register('aside_archives', function (options = {}) {
|
||||
<span>${_p('aside.card_archives')}</span>
|
||||
${
|
||||
data.length > limitedData.length
|
||||
? `<a class="card-more-btn" href="${url_for(archiveDir)}/"
|
||||
? `<a class="card-more-btn" href="${urlFor(archiveDir)}/"
|
||||
title="${_p('aside.more_button')}">
|
||||
<i class="fas fa-angle-right"></i>
|
||||
</a>`
|
||||
|
||||
Reference in New Issue
Block a user