feat: added cdn provider setting in _config.yml (#158)
This commit is contained in:
parent
2612ba3b26
commit
427fa6d466
|
@ -28,7 +28,7 @@ style:
|
||||||
# Favicon (You can use local image or image external link)
|
# Favicon (You can use local image or image external link)
|
||||||
favicon: /images/logo.svg
|
favicon: /images/logo.svg
|
||||||
|
|
||||||
# Article image align position, value: left | center
|
# Article image align position, values: left | center
|
||||||
article_img_align: left
|
article_img_align: left
|
||||||
|
|
||||||
# Left side width
|
# Left side width
|
||||||
|
@ -132,7 +132,7 @@ post:
|
||||||
author_label:
|
author_label:
|
||||||
enable: true
|
enable: true
|
||||||
auto: true # if true, show Lv1, Lv2, Lv3... , If false, show custom label
|
auto: true # if true, show Lv1, Lv2, Lv3... , If false, show custom label
|
||||||
# label array item can be one or more
|
# Label array item can be one or more
|
||||||
custom_label_list: ["Trainee", "Engineer", "Architect", "CTO", "BOSS"]
|
custom_label_list: ["Trainee", "Engineer", "Architect", "CTO", "BOSS"]
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
|
@ -233,6 +233,7 @@ lazyload:
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
cdn:
|
cdn:
|
||||||
enable: false
|
enable: false
|
||||||
|
provider: jsdelivr # values: jsdelivr | unpkg
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# PJAX
|
# PJAX
|
||||||
|
|
|
@ -54,12 +54,34 @@ hexo.extend.helper.register('getPostUrl', function (rootUrl, path) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getSourceCdnUrl = (tyle, themeConfig, path) => {
|
||||||
|
const { version = '3.4.5', cdn = {} } = themeConfig
|
||||||
|
const { provider = 'jsdelivr' } = cdn
|
||||||
|
|
||||||
|
let urlPrefix = ''
|
||||||
|
switch (provider.toLocaleLowerCase()) {
|
||||||
|
case 'jsdelivr':
|
||||||
|
urlPrefix = '//cdn.jsdelivr.net/npm/hexo-theme-keep'
|
||||||
|
if (tyle === 'js') {
|
||||||
|
return `<script src="${urlPrefix}@${version}/source/${path}"></script>`;
|
||||||
|
} else {
|
||||||
|
return `<link rel="stylesheet" href="${urlPrefix}@${version}/source/${path}">`;
|
||||||
|
}
|
||||||
|
case 'unpkg':
|
||||||
|
urlPrefix = '//unpkg.com/hexo-theme-keep'
|
||||||
|
if (tyle === 'js') {
|
||||||
|
return `<script src="${urlPrefix}@${version}/source/${path}"></script>`;
|
||||||
|
} else {
|
||||||
|
return `<link rel="stylesheet" href="${urlPrefix}@${version}/source/${path}">`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hexo.extend.helper.register('__js', function (path) {
|
hexo.extend.helper.register('__js', function (path) {
|
||||||
|
const { enable } = this.theme.cdn
|
||||||
const _js = hexo.extend.helper.get('js').bind(hexo);
|
const _js = hexo.extend.helper.get('js').bind(hexo);
|
||||||
const cdnPathHandle = (path_2) => {
|
const cdnPathHandle = (pa) => {
|
||||||
return this.theme.cdn.enable
|
return enable ? getSourceCdnUrl('js', this.theme, pa) : _js(pa);
|
||||||
? `<script src="//cdn.jsdelivr.net/npm/hexo-theme-keep@${this.theme.version}/source/${path_2}"></script>`
|
|
||||||
: _js(path_2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let t = ``;
|
let t = ``;
|
||||||
|
@ -76,10 +98,7 @@ hexo.extend.helper.register('__js', function (path) {
|
||||||
});
|
});
|
||||||
|
|
||||||
hexo.extend.helper.register('__css', function (path) {
|
hexo.extend.helper.register('__css', function (path) {
|
||||||
|
const { enable } = this.theme.cdn
|
||||||
const _css = hexo.extend.helper.get('css').bind(hexo);
|
const _css = hexo.extend.helper.get('css').bind(hexo);
|
||||||
if (this.theme.cdn.enable) {
|
return enable ? getSourceCdnUrl('css', this.theme, path) : _css(path);
|
||||||
return `<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/hexo-theme-keep@${this.theme.version}/source/${path}">`;
|
|
||||||
} else {
|
|
||||||
return _css(path);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue