chore(*): rewrite cdn helper & bug fixes

This commit is contained in:
ppoffice 2019-12-26 21:07:35 -05:00
parent 48fd263ea5
commit c0e2cb1a9b
14 changed files with 104 additions and 88 deletions

View File

@ -6,86 +6,94 @@
* <%- fontcdn(fontName) %>
* <%- iconcdn() %>
*/
const cdn_providers = {
cdnjs: 'https://cdnjs.cloudflare.com/ajax/libs/${ package }/${ version }/${ filename }',
jsdelivr: 'https://cdn.jsdelivr.net/npm/${ package }@${ version }/${ filename }',
unpkg: 'https://unpkg.com/${ package }@${ version }/${ filename }'
const PROVIDERS = {
LIBRARY: {
cdnjs: '[cdnjs]https://cdnjs.cloudflare.com/ajax/libs/${ package }/${ version }/${ filename }',
loli: '[cdnjs]https://cdnjs.loli.net/ajax/libs/${ package }/${ version }/${ filename }',
jsdelivr: 'https://cdn.jsdelivr.net/npm/${ package }@${ version }/${ filename }',
unpkg: 'https://unpkg.com/${ package }@${ version }/${ filename }'
},
FONT: {
google: 'https://fonts.googleapis.com/${ type }?family=${ fontname }',
loli: 'https://fonts.loli.net/${ type }?family=${ fontname }'
},
ICON: {
fontawesome: 'https://use.fontawesome.com/releases/v5.12.0/css/all.css'
}
};
const font_providers = {
google: 'https://fonts.googleapis.com/${ type }?family=${ fontname }'
};
const icon_providers = {
fontawesome: 'https://use.fontawesome.com/releases/v5.4.1/css/all.css'
/**
* Convert npm library path to CDN.js path
*/
const CDNJS_FIXTURES = {
'moment': (ver, fname) => [
'moment.js', ver, fname.startsWith('min/') ? fname.substr(4) : fname
],
'outdatedbrowser': (ver, fname) => [
'outdated-browser', ver, fname.startsWith('outdatedbrowser/') ? fname.substr(16) : fname
],
'highlight.js': (ver, fname) => [
'highlight.js', ver, fname.endsWith('.css') && fname.indexOf('.min.') === -1
? fname.substr(0, fname.length - 4) + '.min.css' : fname
],
'mathjax': (ver, fname) => [
'mathjax', ver, fname.startsWith('unpacked/') ? fname.substr(9) : fname
],
'katex': (ver, fname) => [
'KaTeX', ver, fname
],
'pace-js': (ver, fname) => [
'pace', ver, fname
],
'clipboard': (ver, fname) => [
'clipboard.js', ver, fname
],
// disqusjs is not hosted on CDN.js
'disqusjs': (ver, fname) => []
};
module.exports = function(hexo) {
hexo.extend.helper.register('cdn', function(_package, version, filename) {
let provider = this.config.provider && 'cdn' in this.config.provider ? this.config.provider.cdn : 'jsdelivr';
let { cdn = 'jsdelivr' } = typeof this.config.providers === 'object' ? this.config.providers : {};
if (cdn in PROVIDERS.LIBRARY) {
cdn = PROVIDERS.LIBRARY[cdn];
}
// cdn.js does not follow a GitHub npm style like jsdeliver and unpkg do. Patch it!
if (provider === 'cdnjs' || provider.startsWith('[cdnjs]')) {
if (provider.startsWith('[cdnjs]')) {
provider = provider.substr(7);
if (cdn === 'cdnjs' || cdn.startsWith('[cdnjs]')) {
if (cdn.startsWith('[cdnjs]')) {
cdn = cdn.substr(7);
}
if (filename.startsWith('dist/')) {
filename = filename.substr(5);
}
if (_package === 'moment') {
_package = 'moment.js';
filename = filename.startsWith('min/') ? filename.substr(4) : filename;
}
if (_package === 'outdatedbrowser') {
_package = 'outdated-browser';
filename = filename.startsWith('outdatedbrowser/') ? filename.substr(16) : filename;
}
if (_package === 'highlight.js') {
filename = filename.endsWith('.css') && filename.indexOf('.min.') === -1
? filename.substr(0, filename.length - 4) + '.min.css' : filename;
}
if (_package === 'mathjax') {
filename = filename.startsWith('unpacked/') ? filename.substr(9) : filename;
}
if (_package === 'pace-js') {
_package = 'pace';
}
if (_package === 'clipboard') {
_package = 'clipboard.js';
}
if (_package === 'disqusjs') {
provider = 'jsdelivr';
}
if (_package === 'katex') {
_package = 'KaTeX.js';
if (Object.prototype.hasOwnProperty.call(CDNJS_FIXTURES, _package)) {
[_package, version, filename] = CDNJS_FIXTURES[_package](version, filename);
// package is not hosted on CDN.js
if (!_package) {
cdn = 'jsdelivr';
}
}
}
if (provider !== null && provider in cdn_providers) {
provider = cdn_providers[provider];
}
return provider.replace(/\${\s*package\s*}/gi, _package)
return cdn.replace(/\${\s*package\s*}/gi, _package)
.replace(/\${\s*version\s*}/gi, version)
.replace(/\${\s*filename\s*}/gi, filename);
});
hexo.extend.helper.register('fontcdn', function(fontName, type = 'css') {
let provider = this.config.provider && 'fontcdn' in this.config.provider ? this.config.provider.fontcdn : 'google';
if (provider !== null && provider in font_providers) {
provider = font_providers[provider];
let { fontcdn = 'google' } = typeof this.config.providers === 'object' ? this.config.providers : {};
if (fontcdn in PROVIDERS.FONT) {
fontcdn = PROVIDERS.FONT[fontcdn];
}
return provider.replace(/\${\s*fontname\s*}/gi, fontName)
return fontcdn.replace(/\${\s*fontname\s*}/gi, fontName)
.replace(/\${\s*type\s*}/gi, type);
});
hexo.extend.helper.register('iconcdn', function(provider = null) {
if (provider !== null && provider in icon_providers) {
provider = icon_providers[provider];
} else {
provider = this.config.provider && 'iconcdn' in this.config.provider ? this.config.provider.iconcdn : 'fontawesome';
if (provider !== null && provider in icon_providers) {
provider = icon_providers[provider];
}
hexo.extend.helper.register('iconcdn', function() {
let { iconfont = 'fontawesome' } = typeof this.config.providers === 'object' ? this.config.providers : {};
if (iconfont in PROVIDERS.ICON) {
iconfont = PROVIDERS.ICON[iconfont];
}
return provider;
return iconfont;
});
};

View File

@ -4,31 +4,13 @@
* @example
* <%- is_categories(page) %>
* <%- is_tags(page) %>
* <%- has_thumbnail(post) %>
* <%- get_thumbnail(post) %>
*/
module.exports = function(hexo) {
hexo.extend.helper.register('is_categories', function(page = null) {
return (page === null ? this.page : page).__categories;
return (page === null ? this.page : page).__categories === true;
});
hexo.extend.helper.register('is_tags', function(page = null) {
return (page === null ? this.page : page).__tags;
});
hexo.extend.helper.register('has_thumbnail', function(post) {
const { article } = this.config;
if (typeof post !== 'object') {
return false;
}
if (article && article.thumbnail === false) {
return false;
}
return 'thumbnail' in post && post.thumbnail;
});
hexo.extend.helper.register('get_thumbnail', function(post) {
const { url_for, has_thumbnail } = this.helper;
return url_for(has_thumbnail.call(this, post) ? post.thumbnail : '/img/thumbnail.svg');
return (page === null ? this.page : page).__tags === true;
});
};

View File

@ -0,0 +1,24 @@
/**
* Helper functions for post thumbnail.
*
* @example
* <%- has_thumbnail(post) %>
* <%- get_thumbnail(post) %>
*/
module.exports = function(hexo) {
hexo.extend.helper.register('has_thumbnail', function(post) {
const { article } = this.config;
if (typeof post !== 'object') {
return false;
}
if (article && article.thumbnail === false) {
return false;
}
return 'thumbnail' in post && post.thumbnail;
});
hexo.extend.helper.register('get_thumbnail', function(post) {
const { url_for, has_thumbnail } = this.helper;
return url_for(has_thumbnail.call(this, post) ? post.thumbnail : '/img/thumbnail.svg');
});
};

View File

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/common/comment.json",
"description": "Comment plugin configurations",
"description": "Comment plugin configurations\nhttps://ppoffice.github.io/hexo-theme-icarus/categories/Plugins/Comment/",
"type": "object",
"oneOf": [
{

View File

@ -18,7 +18,7 @@
},
"iconcdn": {
"type": "string",
"description": "Name or URL of the webfont Icon CDN provider",
"description": "Name or URL of the fontawesome icon font CDN provider",
"default": "fontawesome",
"nullable": true
}

View File

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/common/search.json",
"description": "Search plugin configurations",
"description": "Search plugin configurations\nhttps://ppoffice.github.io/hexo-theme-icarus/categories/Plugins/Search/",
"type": "object",
"oneOf": [
{

View File

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/common/share.json",
"description": "Share plugin configurations",
"description": "Share plugin configurations\nhttps://ppoffice.github.io/hexo-theme-icarus/categories/Plugins/Share/",
"type": "object",
"oneOf": [
{

View File

@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/common/widgets.json",
"description": "Sidebar widget configurations",
"description": "Sidebar widget configurations\nhttp://ppoffice.github.io/hexo-theme-icarus/categories/Widgets/",
"type": "array",
"items": {
"type": "object",

View File

@ -157,7 +157,8 @@ class Schema {
}
const defaultValue = new DefaultValue(value, def.description);
if ('oneOf' in def && Array.isArray(def.oneOf) && def.oneOf.length) {
return defaultValue.merge(this.getDefaultValue(def.oneOf[0]));
defaultValue.merge(this.getDefaultValue(def.oneOf[0]));
defaultValue.description = def.description;
}
return defaultValue;
}

View File

@ -19,7 +19,7 @@ module.exports = class extends Component {
{posts.map(post => {
const categories = [];
post.categories.forEach((category, i) => {
categories.push(<a class="has-link-grey" href={category.url}>{category.name}</a>);
categories.push(<a class="has-link-grey" href={url_for(category.path)}>{category.name}</a>);
if (i < post.categories.length - 1) {
categories.push(' / ');
}

View File

@ -34,7 +34,7 @@ class DisqusJs extends Component {
api: '${api}',
admin: '${admin}',
adminLabel: '${adminLabel}',
nesting: ${nesting}%>
nesting: ${nesting}
});`;
return <Fragment>
<link rel="stylesheet" href={cssUrl} />

View File

@ -25,7 +25,7 @@ class Gitalk extends Component {
if (!id || !repo || !owner || !admin || !clientId || !clientSecret) {
return <div class="notification is-danger">
You forgot to set the <code>owner</code>, <code>admin</code>, <code>repo</code>,
<code>clientId</code>, or <code>clientSecret</code> for Gittalk.
<code>client_id</code>, or <code>client_secret</code> for Gitalk.
Please set it in <code>_config.yml</code>.
</div>;
}

View File

@ -43,7 +43,7 @@ module.exports = class extends Component {
{(() => {
const categories = [];
page.categories.forEach((category, i) => {
categories.push(<a class="has-link-grey" href={category.url}>{category.name}</a>);
categories.push(<a class="has-link-grey" href={url_for(category.path)}>{category.name}</a>);
if (i < page.categories.length - 1) {
categories.push(<span>&nbsp;/&nbsp;</span>);
}

View File

@ -121,6 +121,7 @@ require('../include/generator/insight')(hexo);
require('../include/filter/locals')(hexo);
require('../include/helper/cdn')(hexo);
require('../include/helper/page')(hexo);
require('../include/helper/thumbnail')(hexo);
/**
* Remove Hexo filters that could cause OOM