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:
Jerry
2020-04-06 01:49:47 +08:00
Unverified
parent eb11e4afeb
commit 6c68ce1c4c
16 changed files with 189 additions and 87 deletions

View File

@@ -5,30 +5,23 @@
'use strict'
hexo.extend.helper.register('list_archives', function (options = {}) {
hexo.extend.helper.register('aside_archives', function (options = {}) {
const { config } = this
const archiveDir = config.archive_dir
const { timezone } = config
const lang = this.page.lang || this.page.language || config.language
const lang = toMomentLocale(this.page.lang || this.page.language || config.language)
let { format } = options
const type = options.type || 'monthly'
const { transform } = options
const showCount = Object.prototype.hasOwnProperty.call(options, 'show_count')
? options.show_count
: true
const showCount = Object.prototype.hasOwnProperty.call(options, 'show_count') ? options.show_count : true
const order = options.order || -1
const limit = 8
const compareFunc = type === 'monthly'
? (yearA, monthA, yearB, monthB) => yearA === yearB && monthA === monthB
: (yearA, monthA, yearB, monthB) => yearA === yearB
const limit = options.limit
const moreButton = this._p('aside.more_button')
let result = ''
var moreButton
if (lang === 'zh-CN') {
moreButton = '查看更多'
} else if (lang === 'zh-TW') {
moreButton = '查看更多'
} else {
moreButton = 'More'
}
if (!format) {
format = type === 'monthly' ? 'MMMM YYYY' : 'YYYY'
}
@@ -44,14 +37,14 @@ hexo.extend.helper.register('list_archives', function (options = {}) {
let date = post.date.clone()
if (timezone) date = date.tz(timezone)
if (lang) date = date.locale(lang)
const year = date.year()
const month = date.month() + 1
const name = date.format(format)
const lastData = data[length - 1]
if (!lastData || lastData.name !== name) {
if (!lastData || !compareFunc(lastData.year, lastData.month, year, month)) {
if (lang) date = date.locale(lang)
const name = date.format(format)
length = data.push({
name,
year,
@@ -76,7 +69,10 @@ hexo.extend.helper.register('list_archives', function (options = {}) {
result += '<ul class="archive-list">'
for (let i = 0, len = data.length; i < Math.min(len, limit); i++) {
const len = data.length
const Judge = limit === 0 ? len : Math.min(len, limit)
for (let i = 0; i < Judge; i++) {
const item = data[i]
result += '<li class="archive-list-item">'
@@ -93,14 +89,23 @@ hexo.extend.helper.register('list_archives', function (options = {}) {
result += '</li>'
}
if (data.length > limit) {
if (len > Judge) {
result += '<li class="archive-list-item is-center">'
result +=
'<a class="archive-list-link-more" href="' + '/' + `${archiveDir}" >`
result += moreButton
result += '</a>'
result += '</li>'
result += `<a class="archive-list-link-more" href="${this.url_for(archiveDir)}">${moreButton}</a></li>`
}
result += '</ul>'
return result
})
var toMomentLocale = function (lang) {
if (lang === undefined) {
return undefined
}
// moment.locale('') equals moment.locale('en')
// moment.locale(null) equals moment.locale('en')
if (!lang || lang === 'en' || lang === 'default') {
return 'en'
}
return lang.toLowerCase().replace('_', '-')
}

View 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>`
})