mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-17 04:40:53 +08:00
feat: 更新 plugins.yml 中的依賴版本至最新
feat: 優化 aside_archives ,改進性能和可讀性 feat: 改善 inlineImg 和 timeline 標籤的文檔,優化時間線邏輯 feat: 更新 gallery 標籤以支持額外參數,優化圖片顯示邏輯 improvement: 優化隨機封面過濾器邏輯, 避免連續重複 feat: 最新評論限制顯示 1-10 條之間 fix: artalk 的最新評論顯示待定或者封禁的評論的 bug
This commit is contained in:
@@ -2,38 +2,68 @@
|
||||
|
||||
hexo.extend.helper.register('aside_archives', function (options = {}) {
|
||||
const { config, page, site, url_for, _p } = this
|
||||
const archiveDir = config.archive_dir
|
||||
const { timezone } = config
|
||||
const lang = toMomentLocale(page.lang || page.language || config.language)
|
||||
const type = options.type || 'monthly'
|
||||
const format = options.format || (type === 'monthly' ? 'MMMM YYYY' : 'YYYY')
|
||||
const showCount = Object.prototype.hasOwnProperty.call(options, 'show_count') ? options.show_count : true
|
||||
const order = options.order || -1
|
||||
const limit = options.limit
|
||||
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)
|
||||
|
||||
// Memoize comparison function to improve performance
|
||||
const compareFunc = type === 'monthly'
|
||||
? (yearA, monthA, yearB, monthB) => yearA === yearB && monthA === monthB
|
||||
: (yearA, monthA, yearB, monthB) => yearA === yearB
|
||||
: (yearA, yearB) => yearA === yearB
|
||||
|
||||
const posts = site.posts.sort('date', order)
|
||||
if (!posts.length) return ''
|
||||
// Early return if no posts
|
||||
if (!site.posts.length) return ''
|
||||
|
||||
const data = []
|
||||
posts.forEach(post => {
|
||||
let date = post.date.clone()
|
||||
if (timezone) date = date.tz(timezone)
|
||||
// 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 year = date.year()
|
||||
const month = date.month() + 1
|
||||
const year = date.year()
|
||||
const month = date.month() + 1
|
||||
|
||||
if (!data.length || !compareFunc(data[data.length - 1].year, data[data.length - 1].month, year, month)) {
|
||||
if (lang) date = date.locale(lang)
|
||||
data.push({ name: date.format(format), year, month, count: 1 })
|
||||
} else {
|
||||
data[data.length - 1].count++
|
||||
}
|
||||
})
|
||||
|
||||
const link = item => {
|
||||
// Find or create archive entry
|
||||
const lastEntry = acc[acc.length - 1]
|
||||
if (!lastEntry || !compareFunc(
|
||||
lastEntry.year,
|
||||
lastEntry.month,
|
||||
year,
|
||||
month
|
||||
)) {
|
||||
acc.push({
|
||||
name: date.format(format),
|
||||
year,
|
||||
month,
|
||||
count: 1
|
||||
})
|
||||
} else {
|
||||
lastEntry.count++
|
||||
}
|
||||
|
||||
return acc
|
||||
}, [])
|
||||
|
||||
// Create link generator function
|
||||
const createArchiveLink = item => {
|
||||
let url = `${archiveDir}/${item.year}/`
|
||||
if (type === 'monthly') {
|
||||
url += item.month < 10 ? `0${item.month}/` : `${item.month}/`
|
||||
@@ -41,37 +71,48 @@ hexo.extend.helper.register('aside_archives', function (options = {}) {
|
||||
return url_for(url)
|
||||
}
|
||||
|
||||
const len = data.length
|
||||
const limitLength = limit === 0 ? len : Math.min(len, limit)
|
||||
// Limit results efficiently
|
||||
const limitedData = limit > 0
|
||||
? data.slice(0, Math.min(data.length, limit))
|
||||
: data
|
||||
|
||||
let result = `
|
||||
// Use template literal for better readability
|
||||
const archiveHeader = `
|
||||
<div class="item-headline">
|
||||
<i class="fas fa-archive"></i>
|
||||
<span>${_p('aside.card_archives')}</span>
|
||||
${len > limitLength ? `<a class="card-more-btn" href="${url_for(archiveDir)}/" title="${_p('aside.more_button')}"><i class="fas fa-angle-right"></i></a>` : ''}
|
||||
${data.length > limitedData.length
|
||||
? `<a class="card-more-btn" href="${url_for(archiveDir)}/"
|
||||
title="${_p('aside.more_button')}">
|
||||
<i class="fas fa-angle-right"></i>
|
||||
</a>`
|
||||
: ''}
|
||||
</div>
|
||||
<ul class="card-archive-list">
|
||||
`
|
||||
|
||||
for (let i = 0; i < limitLength; i++) {
|
||||
const item = data[i]
|
||||
result += `
|
||||
<li class="card-archive-list-item">
|
||||
<a class="card-archive-list-link" href="${link(item)}">
|
||||
<span class="card-archive-list-date">${options.transform ? options.transform(item.name) : item.name}</span>
|
||||
${showCount ? `<span class="card-archive-list-count">${item.count}</span>` : ''}
|
||||
</a>
|
||||
</li>
|
||||
`
|
||||
}
|
||||
// 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>
|
||||
`
|
||||
|
||||
result += '</ul>'
|
||||
return result
|
||||
return archiveHeader + archiveList
|
||||
})
|
||||
|
||||
const toMomentLocale = function (lang) {
|
||||
if (!lang || lang === 'en' || lang === 'default') {
|
||||
return 'en'
|
||||
}
|
||||
// Improved locale conversion function
|
||||
const toMomentLocale = lang => {
|
||||
if (!lang || ['en', 'default'].includes(lang)) return 'en'
|
||||
return lang.toLowerCase().replace('_', '-')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user