2020-11-24 10:55:24 +08:00
|
|
|
/* global hexo */
|
|
|
|
|
|
|
|
'use strict'
|
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
const url = require('url')
|
2020-11-24 10:55:24 +08:00
|
|
|
|
2020-03-25 12:27:44 +08:00
|
|
|
hexo.extend.helper.register('isInHomePaging', function (pagePath, route) {
|
2020-10-27 18:11:10 +08:00
|
|
|
if (pagePath.length > 5 && route === '/') {
|
2022-09-30 11:40:30 +08:00
|
|
|
return pagePath.slice(0, 5) === 'page/'
|
2020-10-27 18:11:10 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
return false
|
2020-10-27 18:11:10 +08:00
|
|
|
}
|
2022-09-30 11:40:30 +08:00
|
|
|
})
|
2020-03-27 20:45:27 +08:00
|
|
|
|
|
|
|
hexo.extend.helper.register('createNewArchivePosts', function (posts) {
|
2022-09-30 11:40:30 +08:00
|
|
|
const postList = [],
|
|
|
|
postYearList = []
|
|
|
|
posts.forEach((post) => postYearList.push(post.date.year()))
|
|
|
|
Array.from(new Set(postYearList)).forEach((year) => {
|
2020-10-27 18:11:10 +08:00
|
|
|
postList.push({
|
|
|
|
year: year,
|
|
|
|
postList: []
|
|
|
|
})
|
2022-09-30 11:40:30 +08:00
|
|
|
})
|
2020-10-27 18:11:10 +08:00
|
|
|
postList.sort((a, b) => b.year - a.year)
|
2022-09-30 11:40:30 +08:00
|
|
|
postList.forEach((item) => {
|
|
|
|
posts.forEach((post) => item.year === post.date.year() && item.postList.push(post))
|
|
|
|
})
|
|
|
|
postList.forEach((item) => item.postList.sort((a, b) => b.date.unix() - a.date.unix()))
|
|
|
|
return postList
|
|
|
|
})
|
2020-10-27 18:11:10 +08:00
|
|
|
|
2021-01-28 16:23:47 +08:00
|
|
|
hexo.extend.helper.register('getAuthorLabel', function (postCount, isAuto, labelList) {
|
2022-09-30 11:40:30 +08:00
|
|
|
let level = Math.floor(Math.log2(postCount))
|
|
|
|
level = level < 2 ? 1 : level - 1
|
2021-01-28 16:23:47 +08:00
|
|
|
|
|
|
|
if (isAuto === false && Array.isArray(labelList) && labelList.length > 0) {
|
2022-09-30 11:40:30 +08:00
|
|
|
return level > labelList.length ? labelList[labelList.length - 1] : labelList[level - 1]
|
2021-01-28 16:23:47 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
return `Lv${level}`
|
2021-01-28 16:23:47 +08:00
|
|
|
}
|
2022-09-30 11:40:30 +08:00
|
|
|
})
|
2020-11-18 18:45:45 +08:00
|
|
|
|
|
|
|
hexo.extend.helper.register('getPostUrl', function (rootUrl, path) {
|
|
|
|
if (rootUrl) {
|
2022-09-30 11:40:30 +08:00
|
|
|
let { href } = url.parse(rootUrl)
|
2021-11-17 21:51:35 +08:00
|
|
|
if (href.substr(href.length - 1, 1) !== '/') {
|
2022-09-30 11:40:30 +08:00
|
|
|
href = href + '/'
|
2021-11-17 21:51:35 +08:00
|
|
|
}
|
2022-09-30 11:40:30 +08:00
|
|
|
return href + path
|
2020-11-18 18:45:45 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
return path
|
2020-11-18 18:45:45 +08:00
|
|
|
}
|
2022-09-30 11:40:30 +08:00
|
|
|
})
|
2020-11-24 10:55:24 +08:00
|
|
|
|
2022-09-27 13:43:36 +08:00
|
|
|
const getSourceCdnUrl = (tyle, themeConfig, path) => {
|
2022-09-30 11:40:30 +08:00
|
|
|
const version = require('../../package.json').version
|
|
|
|
const { provider = 'jsdelivr' } = themeConfig?.cdn
|
2022-09-27 13:43:36 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
let urlPrefix = ''
|
2022-09-27 13:43:36 +08:00
|
|
|
switch (provider.toLocaleLowerCase()) {
|
|
|
|
case 'jsdelivr':
|
|
|
|
urlPrefix = '//cdn.jsdelivr.net/npm/hexo-theme-keep'
|
|
|
|
if (tyle === 'js') {
|
2022-09-30 11:40:30 +08:00
|
|
|
return `<script src="${urlPrefix}@${version}/source/${path}"></script>`
|
2022-09-27 13:43:36 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
return `<link rel="stylesheet" href="${urlPrefix}@${version}/source/${path}">`
|
2022-09-27 13:43:36 +08:00
|
|
|
}
|
|
|
|
case 'unpkg':
|
|
|
|
urlPrefix = '//unpkg.com/hexo-theme-keep'
|
|
|
|
if (tyle === 'js') {
|
2022-09-30 11:40:30 +08:00
|
|
|
return `<script src="${urlPrefix}@${version}/source/${path}"></script>`
|
2022-09-27 13:43:36 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
return `<link rel="stylesheet" href="${urlPrefix}@${version}/source/${path}">`
|
2022-09-27 13:43:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:55:24 +08:00
|
|
|
hexo.extend.helper.register('__js', function (path) {
|
2022-09-27 13:43:36 +08:00
|
|
|
const { enable } = this.theme.cdn
|
2022-09-30 11:40:30 +08:00
|
|
|
const _js = hexo.extend.helper.get('js').bind(hexo)
|
2022-09-27 13:43:36 +08:00
|
|
|
const cdnPathHandle = (pa) => {
|
2022-09-30 11:40:30 +08:00
|
|
|
return enable ? getSourceCdnUrl('js', this.theme, pa) : _js(pa)
|
2020-11-24 10:55:24 +08:00
|
|
|
}
|
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
let t = ``
|
2020-11-24 10:55:24 +08:00
|
|
|
|
|
|
|
if (Array.isArray(path)) {
|
|
|
|
for (const p of path) {
|
2022-09-30 11:40:30 +08:00
|
|
|
t += cdnPathHandle(p)
|
2020-11-24 10:55:24 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
t = cdnPathHandle(path)
|
2020-11-24 10:55:24 +08:00
|
|
|
}
|
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
return t
|
|
|
|
})
|
2020-11-24 10:55:24 +08:00
|
|
|
|
2020-11-24 11:10:03 +08:00
|
|
|
hexo.extend.helper.register('__css', function (path) {
|
2022-09-27 13:43:36 +08:00
|
|
|
const { enable } = this.theme.cdn
|
2022-09-30 11:40:30 +08:00
|
|
|
const _css = hexo.extend.helper.get('css').bind(hexo)
|
|
|
|
return enable ? getSourceCdnUrl('css', this.theme, path) : _css(path)
|
|
|
|
})
|