mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-19 18:42:34 +00:00
Custom CDN options (#428)
This commit is contained in:
parent
6092e9ec9b
commit
3dca98b7cf
10
_config.yml
10
_config.yml
@ -868,16 +868,20 @@ canvas_ribbon:
|
|||||||
|
|
||||||
vendors:
|
vendors:
|
||||||
# The CDN provider of NexT internal scripts.
|
# The CDN provider of NexT internal scripts.
|
||||||
# Available values: local | jsdelivr | unpkg | cdnjs
|
# Available values: local | jsdelivr | unpkg | cdnjs | custom
|
||||||
# Warning: If you are using the latest master branch of NexT, please set `internal: local`
|
# Warning: If you are using the latest master branch of NexT, please set `internal: local`
|
||||||
internal: local
|
internal: local
|
||||||
# The default CDN provider of third-party plugins.
|
# The default CDN provider of third-party plugins.
|
||||||
# Available values: local | jsdelivr | unpkg | cdnjs
|
# Available values: local | jsdelivr | unpkg | cdnjs | custom
|
||||||
# Dependencies for `plugins: local`: https://github.com/next-theme/plugins
|
# Dependencies for `plugins: local`: https://github.com/next-theme/plugins
|
||||||
plugins: jsdelivr
|
plugins: jsdelivr
|
||||||
|
# Custom CDN URL
|
||||||
|
# For example:
|
||||||
|
# custom_cdn_url: https://cdn.jsdelivr.net/npm/${npm_name}@${version}/${minified}
|
||||||
|
# custom_cdn_url: https://cdnjs.cloudflare.com/ajax/libs/${cdnjs_name}/${version}/${cdnjs_file}
|
||||||
|
custom_cdn_url:
|
||||||
|
|
||||||
# Assets
|
# Assets
|
||||||
# Accelerate delivery of static files using a CDN
|
# Accelerate delivery of static files using a CDN
|
||||||
css: css
|
css: css
|
||||||
js: js
|
|
||||||
images: images
|
images: images
|
||||||
|
|||||||
@ -13,10 +13,6 @@ function resolve(name, file = '') {
|
|||||||
return `${dir}/${file}`;
|
return `${dir}/${file}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parse(line, attr) {
|
|
||||||
return line.split(attr)[1].replace(';', '').trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
function highlightTheme(name) {
|
function highlightTheme(name) {
|
||||||
const file = resolve('highlight.js', `styles/${name}.css`);
|
const file = resolve('highlight.js', `styles/${name}.css`);
|
||||||
const css = fs.readFileSync(file).toString();
|
const css = fs.readFileSync(file).toString();
|
||||||
@ -27,6 +23,7 @@ function highlightTheme(name) {
|
|||||||
rule += content;
|
rule += content;
|
||||||
return match;
|
return match;
|
||||||
});
|
});
|
||||||
|
const parse = (line, attr) => line.split(attr)[1].replace(';', '').trim();
|
||||||
rule.split('\n').forEach(line => {
|
rule.split('\n').forEach(line => {
|
||||||
if (line.includes('background:')) background = parse(line, 'background:');
|
if (line.includes('background:')) background = parse(line, 'background:');
|
||||||
else if (line.includes('background-color:')) background = parse(line, 'background-color:');
|
else if (line.includes('background-color:')) background = parse(line, 'background-color:');
|
||||||
@ -39,6 +36,29 @@ function highlightTheme(name) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getVendors({ name, alias, version, file, minified, local, custom }) {
|
||||||
|
// Make it possible to set `cdnjs_name` and `cdnjs_file` in `custom_cdn_url`
|
||||||
|
const npm_name = name;
|
||||||
|
const cdnjs_name = alias || name;
|
||||||
|
const npm_file = file;
|
||||||
|
const cdnjs_file = minified.replace(/^(dist|lib|source\/js|)\/(browser\/|)/, '');
|
||||||
|
const value = {
|
||||||
|
npm_name,
|
||||||
|
cdnjs_name,
|
||||||
|
version,
|
||||||
|
npm_file,
|
||||||
|
minified,
|
||||||
|
cdnjs_file
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
local,
|
||||||
|
jsdelivr: `https://cdn.jsdelivr.net/npm/${npm_name}@${version}/${minified}`,
|
||||||
|
unpkg : `https://unpkg.com/${npm_name}@${version}/${npm_file}`,
|
||||||
|
cdnjs : `https://cdnjs.cloudflare.com/ajax/libs/${cdnjs_name}/${version}/${cdnjs_file}`,
|
||||||
|
custom : (custom || '').replace(/\$\{(.+?)\}/g, (match, $1) => value[$1])
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const points = {
|
const points = {
|
||||||
views: [
|
views: [
|
||||||
'head',
|
'head',
|
||||||
@ -61,5 +81,6 @@ const points = {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
resolve,
|
resolve,
|
||||||
highlightTheme,
|
highlightTheme,
|
||||||
|
getVendors,
|
||||||
points
|
points
|
||||||
};
|
};
|
||||||
|
|||||||
@ -4,6 +4,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const { url_for } = require('hexo-util');
|
const { url_for } = require('hexo-util');
|
||||||
|
const { getVendors } = require('./utils');
|
||||||
|
|
||||||
let internal;
|
let internal;
|
||||||
try {
|
try {
|
||||||
@ -33,13 +34,13 @@ module.exports = hexo => {
|
|||||||
if (key === 'pace_css') {
|
if (key === 'pace_css') {
|
||||||
value.file = `${value.dir}/${pace.color}/pace-theme-${pace.theme}.css`;
|
value.file = `${value.dir}/${pace.color}/pace-theme-${pace.theme}.css`;
|
||||||
}
|
}
|
||||||
const { name, version, file, alias, unavailable } = value;
|
const { name, file, unavailable } = value;
|
||||||
const links = {
|
const links = getVendors({
|
||||||
|
...value,
|
||||||
|
minified: file,
|
||||||
local : url_for.call(hexo, `lib/${name}/${file}`),
|
local : url_for.call(hexo, `lib/${name}/${file}`),
|
||||||
jsdelivr: `https://cdn.jsdelivr.net/npm/${name}@${version}/${file}`,
|
custom : vendors.custom_cdn_url
|
||||||
unpkg : `https://unpkg.com/${name}@${version}/${file}`,
|
});
|
||||||
cdnjs : `https://cdnjs.cloudflare.com/ajax/libs/${alias || name}/${version}/${file.replace(/^(dist|lib|)\/(browser\/|)/, '')}`
|
|
||||||
};
|
|
||||||
let { plugins = 'jsdelivr' } = vendors;
|
let { plugins = 'jsdelivr' } = vendors;
|
||||||
if (plugins === 'cdnjs' && unavailable && unavailable.includes('cdnjs')) plugins = 'jsdelivr';
|
if (plugins === 'cdnjs' && unavailable && unavailable.includes('cdnjs')) plugins = 'jsdelivr';
|
||||||
if (plugins === 'local' && typeof internal === 'undefined') plugins = 'jsdelivr';
|
if (plugins === 'local' && typeof internal === 'undefined') plugins = 'jsdelivr';
|
||||||
|
|||||||
@ -3,8 +3,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
const { parse } = require('url');
|
||||||
const nextFont = require('./font');
|
const nextFont = require('./font');
|
||||||
const nextUrl = require('./next-url');
|
const nextUrl = require('./next-url');
|
||||||
|
const { getVendors } = require('../events/lib/utils');
|
||||||
|
|
||||||
hexo.extend.helper.register('next_font', nextFont);
|
hexo.extend.helper.register('next_font', nextFont);
|
||||||
hexo.extend.helper.register('next_url', nextUrl);
|
hexo.extend.helper.register('next_url', nextUrl);
|
||||||
@ -17,14 +19,15 @@ hexo.extend.helper.register('next_inject', function(point) {
|
|||||||
|
|
||||||
hexo.extend.helper.register('next_js', function(file, pjax = false) {
|
hexo.extend.helper.register('next_js', function(file, pjax = false) {
|
||||||
const { next_version } = this;
|
const { next_version } = this;
|
||||||
const { internal } = this.theme.vendors;
|
const { internal, custom_cdn_url } = this.theme.vendors;
|
||||||
const minified_file = file.endsWith('.js') && !file.endsWith('.min.js') ? file.slice(0, -3) + '.min.js' : file;
|
const links = getVendors({
|
||||||
const links = {
|
name : 'hexo-theme-next',
|
||||||
local : this.url_for(`${this.theme.js}/${file}`),
|
version : next_version,
|
||||||
jsdelivr: `https://cdn.jsdelivr.net/npm/hexo-theme-next@${next_version}/source/js/${minified_file}`,
|
file : 'source/js/' + file,
|
||||||
unpkg : `https://unpkg.com/hexo-theme-next@${next_version}/source/js/${file}`,
|
minified: 'source/js/' + file.replace(/\.js$/, '.min.js'),
|
||||||
cdnjs : `https://cdnjs.cloudflare.com/ajax/libs/hexo-theme-next/${next_version}/${minified_file}`
|
local : this.url_for(`js/${file}`),
|
||||||
};
|
custom : custom_cdn_url
|
||||||
|
});
|
||||||
const src = links[internal] || links.local;
|
const src = links[internal] || links.local;
|
||||||
return `<script ${pjax ? 'data-pjax ' : ''}src="${src}"></script>`;
|
return `<script ${pjax ? 'data-pjax ' : ''}src="${src}"></script>`;
|
||||||
});
|
});
|
||||||
@ -48,23 +51,22 @@ hexo.extend.helper.register('next_data', function(name, ...data) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
hexo.extend.helper.register('next_pre', function() {
|
hexo.extend.helper.register('next_pre', function() {
|
||||||
const { preconnect } = this.theme;
|
if (!this.theme.preconnect) return '';
|
||||||
if (!preconnect) return '';
|
|
||||||
const { enable, host } = this.theme.font;
|
const { enable, host } = this.theme.font;
|
||||||
const { internal, plugins } = this.theme.vendors;
|
const { internal, plugins, custom_cdn_url } = this.theme.vendors;
|
||||||
const links = {
|
const links = {
|
||||||
local : '',
|
local : '',
|
||||||
jsdelivr: 'https://cdn.jsdelivr.net',
|
jsdelivr: 'https://cdn.jsdelivr.net',
|
||||||
unpkg : 'https://unpkg.com',
|
unpkg : 'https://unpkg.com',
|
||||||
cdnjs : 'https://cdnjs.cloudflare.com'
|
cdnjs : 'https://cdnjs.cloudflare.com',
|
||||||
|
custom : parse(custom_cdn_url || '').hostname
|
||||||
};
|
};
|
||||||
const h = enable ? host || 'https://fonts.googleapis.com' : '';
|
const h = enable ? host || 'https://fonts.googleapis.com' : '';
|
||||||
const i = links[internal];
|
const i = links[internal];
|
||||||
const p = links[plugins];
|
const p = links[plugins];
|
||||||
const results = [...new Set([h, i, p].filter(origin => origin))].map(
|
return [...new Set([h, i, p].filter(origin => origin))].map(
|
||||||
origin => `<link rel="preconnect" href="${origin}" crossorigin>`
|
origin => `<link rel="preconnect" href="${origin}" crossorigin>`
|
||||||
);
|
).join('\n');
|
||||||
return results.join('\n');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
hexo.extend.helper.register('post_gallery', function(photos) {
|
hexo.extend.helper.register('post_gallery', function(photos) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user