mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-12 14:07:06 +08:00
✨ feat: aside widget 增加部分參數可配置
✨ feat: aside widget的Categories,Tags,post,Archives可配置limit ✨ feat: 整合公告欄配置到aside去 🐛 fix: 修復當flink.yml內容為空時,報錯的bug 💄 fix: 修復darkmode下 aside 查看更多文字顏色太淺的bug
This commit is contained in:
87
scripts/helpers/aside_categories.js
Normal file
87
scripts/helpers/aside_categories.js
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Butterfly
|
||||
* for aside categories
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
hexo.extend.helper.register('aside_categories', function (categories, options) {
|
||||
if (!options && (!categories || !Object.prototype.hasOwnProperty.call(categories, 'length'))) {
|
||||
options = categories
|
||||
categories = this.site.categories
|
||||
}
|
||||
|
||||
if (!categories || !categories.length) return ''
|
||||
options = options || {}
|
||||
const { config } = this
|
||||
const showCount = Object.prototype.hasOwnProperty.call(options, 'show_count') ? options.show_count : true
|
||||
const depth = options.depth ? parseInt(options.depth, 10) : 0
|
||||
const orderby = options.orderby || 'name'
|
||||
const order = options.order || 1
|
||||
const tagDir = this.url_for(config.tag_dir)
|
||||
const limit = options.limit === 0 ? categories.length : options.limit
|
||||
const buttonLabel = this._p('aside.more_button')
|
||||
const prepareQuery = parent => {
|
||||
const query = {}
|
||||
if (parent) {
|
||||
query.parent = parent
|
||||
} else {
|
||||
query.parent = {
|
||||
$exists: false
|
||||
}
|
||||
}
|
||||
return categories.find(query).sort(orderby, order).filter(cat => cat.length)
|
||||
}
|
||||
|
||||
const hierarchicalList = (t, level, parent) => {
|
||||
let result = ''
|
||||
if (t > 0) {
|
||||
prepareQuery(parent).forEach((cat, i) => {
|
||||
if (t > 0) {
|
||||
t = t - 1
|
||||
let child
|
||||
if (!depth || level + 1 < depth) {
|
||||
var childList = hierarchicalList(t, level + 1, cat._id)
|
||||
child = childList[0]
|
||||
t = childList[1]
|
||||
}
|
||||
|
||||
result += '<li class="aside-category-list-item">'
|
||||
|
||||
result += `<a class="aside-category-list-link" href="${this.url_for(cat.path)}">`
|
||||
result += `<span class="aside-category-list-name">${cat.name}</span>`
|
||||
|
||||
if (showCount) {
|
||||
result += `<span class="aside-category-list-count">${cat.length}</span>`
|
||||
}
|
||||
|
||||
result += '</a>'
|
||||
result += '</li>'
|
||||
|
||||
if (child) {
|
||||
result += `<ul class="aside-category-list child">${child}</ul>`
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return [result, t]
|
||||
}
|
||||
|
||||
const list = hierarchicalList(limit, 0)
|
||||
|
||||
var moreButton = function () {
|
||||
var moreHtml = ''
|
||||
if (categories.length <= limit) return ''
|
||||
moreHtml += '<li class="aside-category-list-item is-center">'
|
||||
moreHtml += `<a class="aside-category-list-item-more" href="${tagDir}" >`
|
||||
moreHtml += buttonLabel
|
||||
moreHtml += '</a></li>'
|
||||
return moreHtml
|
||||
}
|
||||
|
||||
return `<ul class="aside-category-list">
|
||||
${list[0]}
|
||||
${moreButton()}
|
||||
</ul>`
|
||||
})
|
||||
Reference in New Issue
Block a user