'use strict' 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 compareFunc = type === 'monthly' ? (yearA, monthA, yearB, monthB) => yearA === yearB && monthA === monthB : (yearA, monthA, yearB, monthB) => yearA === yearB const posts = site.posts.sort('date', order) if (!posts.length) return '' const data = [] posts.forEach(post => { let date = post.date.clone() if (timezone) date = date.tz(timezone) 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 => { let url = `${archiveDir}/${item.year}/` if (type === 'monthly') { url += item.month < 10 ? `0${item.month}/` : `${item.month}/` } return url_for(url) } const len = data.length const limitLength = limit === 0 ? len : Math.min(len, limit) let result = `
${_p('aside.card_archives')} ${len > limitLength ? `` : ''}
' return result }) const toMomentLocale = function (lang) { if (!lang || lang === 'en' || lang === 'default') { return 'en' } return lang.toLowerCase().replace('_', '-') }