2018-10-16 05:28:42 +00:00
|
|
|
/**
|
|
|
|
* CDN static file resolvers.
|
2018-10-19 04:53:09 +00:00
|
|
|
*
|
2018-10-16 05:28:42 +00:00
|
|
|
* @example
|
|
|
|
* <%- cdn(package, version, filename) %>
|
|
|
|
* <%- 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 font_providers = {
|
2018-10-27 16:16:23 +00:00
|
|
|
google: 'https://fonts.googleapis.com/${ type }?family=${ fontname }'
|
2018-10-16 05:28:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const icon_providers = {
|
2018-10-27 16:16:23 +00:00
|
|
|
fontawesome: 'https://use.fontawesome.com/releases/v5.4.1/css/all.css'
|
2018-10-16 05:28:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function (hexo) {
|
2018-10-27 16:16:23 +00:00
|
|
|
hexo.extend.helper.register('cdn', function (_package, version, filename) {
|
2018-10-19 04:53:09 +00:00
|
|
|
let provider = hexo.extend.helper.get('get_config').bind(this)('providers.cdn');
|
2018-10-27 16:16:23 +00:00
|
|
|
// 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 (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;
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 04:53:09 +00:00
|
|
|
if (provider !== null && cdn_providers.hasOwnProperty(provider)) {
|
2018-10-16 05:28:42 +00:00
|
|
|
provider = cdn_providers[provider];
|
|
|
|
}
|
2018-10-27 16:16:23 +00:00
|
|
|
return provider.replace(/\${\s*package\s*}/gi, _package)
|
2018-10-16 05:28:42 +00:00
|
|
|
.replace(/\${\s*version\s*}/gi, version)
|
|
|
|
.replace(/\${\s*filename\s*}/gi, filename);
|
|
|
|
});
|
|
|
|
|
2018-10-27 16:16:23 +00:00
|
|
|
hexo.extend.helper.register('fontcdn', function (fontName, type = 'css') {
|
2018-10-19 04:53:09 +00:00
|
|
|
let provider = hexo.extend.helper.get('get_config').bind(this)('providers.fontcdn');
|
|
|
|
if (provider !== null && font_providers.hasOwnProperty(provider)) {
|
2018-10-16 05:28:42 +00:00
|
|
|
provider = font_providers[provider];
|
|
|
|
}
|
2018-10-27 16:16:23 +00:00
|
|
|
return provider.replace(/\${\s*fontname\s*}/gi, fontName)
|
|
|
|
.replace(/\${\s*type\s*}/gi, type);
|
2018-10-16 05:28:42 +00:00
|
|
|
});
|
|
|
|
|
2018-10-19 04:53:09 +00:00
|
|
|
hexo.extend.helper.register('iconcdn', function (provider = null) {
|
|
|
|
if (provider !== null && icon_providers.hasOwnProperty(provider)) {
|
2018-10-16 05:28:42 +00:00
|
|
|
provider = icon_providers[provider];
|
2018-10-19 04:53:09 +00:00
|
|
|
} else {
|
|
|
|
provider = hexo.extend.helper.get('get_config').bind(this)('providers.iconcdn');
|
|
|
|
if (provider !== null && icon_providers.hasOwnProperty(provider)) {
|
|
|
|
provider = icon_providers[provider];
|
|
|
|
}
|
2018-10-16 05:28:42 +00:00
|
|
|
}
|
|
|
|
return provider;
|
|
|
|
});
|
|
|
|
}
|