chore(*): rewrite cdn helper & bug fixes

pull/588/head
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) %> * <%- fontcdn(fontName) %>
* <%- iconcdn() %> * <%- iconcdn() %>
*/ */
const cdn_providers = {
cdnjs: 'https://cdnjs.cloudflare.com/ajax/libs/${ package }/${ version }/${ filename }', const PROVIDERS = {
jsdelivr: 'https://cdn.jsdelivr.net/npm/${ package }@${ version }/${ filename }', LIBRARY: {
unpkg: 'https://unpkg.com/${ package }@${ version }/${ filename }' 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 }' * Convert npm library path to CDN.js path
}; */
const CDNJS_FIXTURES = {
const icon_providers = { 'moment': (ver, fname) => [
fontawesome: 'https://use.fontawesome.com/releases/v5.4.1/css/all.css' '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) { module.exports = function(hexo) {
hexo.extend.helper.register('cdn', function(_package, version, filename) { 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! // cdn.js does not follow a GitHub npm style like jsdeliver and unpkg do. Patch it!
if (provider === 'cdnjs' || provider.startsWith('[cdnjs]')) { if (cdn === 'cdnjs' || cdn.startsWith('[cdnjs]')) {
if (provider.startsWith('[cdnjs]')) { if (cdn.startsWith('[cdnjs]')) {
provider = provider.substr(7); cdn = cdn.substr(7);
} }
if (filename.startsWith('dist/')) { if (filename.startsWith('dist/')) {
filename = filename.substr(5); filename = filename.substr(5);
} }
if (_package === 'moment') { if (Object.prototype.hasOwnProperty.call(CDNJS_FIXTURES, _package)) {
_package = 'moment.js'; [_package, version, filename] = CDNJS_FIXTURES[_package](version, filename);
filename = filename.startsWith('min/') ? filename.substr(4) : filename; // package is not hosted on CDN.js
} if (!_package) {
if (_package === 'outdatedbrowser') { cdn = 'jsdelivr';
_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 (provider !== null && provider in cdn_providers) { return cdn.replace(/\${\s*package\s*}/gi, _package)
provider = cdn_providers[provider];
}
return provider.replace(/\${\s*package\s*}/gi, _package)
.replace(/\${\s*version\s*}/gi, version) .replace(/\${\s*version\s*}/gi, version)
.replace(/\${\s*filename\s*}/gi, filename); .replace(/\${\s*filename\s*}/gi, filename);
}); });
hexo.extend.helper.register('fontcdn', function(fontName, type = 'css') { hexo.extend.helper.register('fontcdn', function(fontName, type = 'css') {
let provider = this.config.provider && 'fontcdn' in this.config.provider ? this.config.provider.fontcdn : 'google'; let { fontcdn = 'google' } = typeof this.config.providers === 'object' ? this.config.providers : {};
if (provider !== null && provider in font_providers) { if (fontcdn in PROVIDERS.FONT) {
provider = font_providers[provider]; 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); .replace(/\${\s*type\s*}/gi, type);
}); });
hexo.extend.helper.register('iconcdn', function(provider = null) { hexo.extend.helper.register('iconcdn', function() {
if (provider !== null && provider in icon_providers) { let { iconfont = 'fontawesome' } = typeof this.config.providers === 'object' ? this.config.providers : {};
provider = icon_providers[provider]; if (iconfont in PROVIDERS.ICON) {
} else { iconfont = PROVIDERS.ICON[iconfont];
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];
}
} }
return provider; return iconfont;
}); });
}; };

View File

@ -4,31 +4,13 @@
* @example * @example
* <%- is_categories(page) %> * <%- is_categories(page) %>
* <%- is_tags(page) %> * <%- is_tags(page) %>
* <%- has_thumbnail(post) %>
* <%- get_thumbnail(post) %>
*/ */
module.exports = function(hexo) { module.exports = function(hexo) {
hexo.extend.helper.register('is_categories', function(page = null) { 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) { hexo.extend.helper.register('is_tags', function(page = null) {
return (page === null ? this.page : page).__tags; return (page === null ? this.page : page).__tags === true;
});
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

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

View File

@ -18,7 +18,7 @@
}, },
"iconcdn": { "iconcdn": {
"type": "string", "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", "default": "fontawesome",
"nullable": true "nullable": true
} }

View File

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

View File

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

View File

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

View File

@ -157,7 +157,8 @@ class Schema {
} }
const defaultValue = new DefaultValue(value, def.description); const defaultValue = new DefaultValue(value, def.description);
if ('oneOf' in def && Array.isArray(def.oneOf) && def.oneOf.length) { 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; return defaultValue;
} }

View File

@ -19,7 +19,7 @@ module.exports = class extends Component {
{posts.map(post => { {posts.map(post => {
const categories = []; const categories = [];
post.categories.forEach((category, i) => { 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) { if (i < post.categories.length - 1) {
categories.push(' / '); categories.push(' / ');
} }

View File

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

View File

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

View File

@ -43,7 +43,7 @@ module.exports = class extends Component {
{(() => { {(() => {
const categories = []; const categories = [];
page.categories.forEach((category, i) => { 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) { if (i < page.categories.length - 1) {
categories.push(<span>&nbsp;/&nbsp;</span>); categories.push(<span>&nbsp;/&nbsp;</span>);
} }

View File

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