fix: 更新 package.json 和 plugins.yml 中的版本號

fix: 修正 truncateContent 函數, 加密文章不显示自动擷取內容
feat: 增加首頁樣式以支持單詞換行
fix: 修正 truncateContent 函數以正確處理自動擷取內容
fix: 修复 card_archives 计数 bug
fix: 修正分頁順序邏輯
This commit is contained in:
Jerry
2025-05-09 17:52:01 +08:00
parent ca030589fb
commit cf059bd533
7 changed files with 73 additions and 63 deletions

View File

@@ -3,13 +3,14 @@
const { stripHTML, truncate } = require('hexo-util')
// Truncates the given content to a specified length, removing HTML tags and replacing newlines with spaces.
const truncateContent = (content, length) => {
return truncate(stripHTML(content), { length, separator: ' ' }).replace(/\n/g, ' ')
const truncateContent = (content, length, encrypt = false) => {
if (!content || encrypt) return ''
return truncate(stripHTML(content).replace(/\n/g, ' '), { length })
}
// Generates a post description based on the provided data and theme configuration.
const postDesc = (data, hexo) => {
const { description, content, postDesc } = data
const { description, content, postDesc, encrypt } = data
if (postDesc) return postDesc
@@ -23,10 +24,10 @@ const postDesc = (data, hexo) => {
result = description
break
case 2:
result = description || truncateContent(content, length)
result = description || truncateContent(content, length, encrypt)
break
default:
result = truncateContent(content, length)
result = truncateContent(content, length, encrypt)
}
data.postDesc = result

View File

@@ -2,11 +2,7 @@
hexo.extend.helper.register('aside_archives', function (options = {}) {
const { config, page, site, url_for, _p } = this
const {
archive_dir: archiveDir,
timezone,
language
} = config
const { archive_dir: archiveDir, timezone, language } = config
// Destructure and set default options with object destructuring
const {
@@ -22,33 +18,42 @@ hexo.extend.helper.register('aside_archives', function (options = {}) {
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
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 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 (lang) date = date.locale(lang)
if (lang) date = date.locale(lang)
// Find or create archive entry
const lastEntry = acc[acc.length - 1]
if (!lastEntry || !compareFunc(
lastEntry.year,
lastEntry.month,
year,
month
)) {
// 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
})
}
} else {
if (!lastEntry || !compareFunc(lastEntry.year, lastEntry.month, year, month)) {
acc.push({
name: date.format(format),
year,
@@ -58,9 +63,10 @@ hexo.extend.helper.register('aside_archives', function (options = {}) {
} else {
lastEntry.count++
}
}
return acc
}, [])
return acc
}, [])
// Create link generator function
const createArchiveLink = item => {
@@ -72,39 +78,41 @@ hexo.extend.helper.register('aside_archives', function (options = {}) {
}
// Limit results efficiently
const limitedData = limit > 0
? data.slice(0, Math.min(data.length, limit))
: data
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="${url_for(archiveDir)}/"
${
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>
`
// Use map for generating list items, join for performance
const archiveList = `
<ul class="card-archive-list">
${limitedData.map(item => `
${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>`
: ''}
${showCount ? `<span class="card-archive-list-count">${item.count}</span>` : ''}
</a>
</li>
`).join('')}
`
)
.join('')}
</ul>
`