2025-2-26-fixed
This commit is contained in:
25
themes/fluid/scripts/helpers/date.js
Normal file
25
themes/fluid/scripts/helpers/date.js
Normal file
@ -0,0 +1,25 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
const moment = require('moment');
|
||||
const { isMoment } = moment;
|
||||
|
||||
hexo.extend.helper.register('compare_date', function(date1, date2) {
|
||||
if (!date1) {
|
||||
return -1;
|
||||
}
|
||||
if (!date2) {
|
||||
return 1;
|
||||
}
|
||||
const m1 = isMoment(date1) ? date1 : moment(date1);
|
||||
const m2 = isMoment(date2) ? date2 : moment(date2);
|
||||
const diff = m1.diff(m2);
|
||||
if (diff < 0) {
|
||||
return -1;
|
||||
} else if (diff > 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
9
themes/fluid/scripts/helpers/engine.js
Normal file
9
themes/fluid/scripts/helpers/engine.js
Normal file
@ -0,0 +1,9 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
hexo.extend.helper.register('inject_point', function(point) {
|
||||
return this.theme.injects[point]
|
||||
.map(item => this.partial(item.layout, item.locals, item.options))
|
||||
.join('');
|
||||
});
|
||||
40
themes/fluid/scripts/helpers/export-config.js
Normal file
40
themes/fluid/scripts/helpers/export-config.js
Normal file
@ -0,0 +1,40 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
const url = require('url');
|
||||
const urlJoin = require('../utils/url-join');
|
||||
|
||||
/**
|
||||
* Export theme config to js
|
||||
*/
|
||||
hexo.extend.helper.register('export_config', function () {
|
||||
let { config, theme, fluid_version } = this;
|
||||
const exportConfig = {
|
||||
hostname: url.parse(config.url).hostname || config.url,
|
||||
root: config.root,
|
||||
version: fluid_version,
|
||||
typing: theme.fun_features.typing,
|
||||
anchorjs: theme.fun_features.anchorjs,
|
||||
progressbar: theme.fun_features.progressbar,
|
||||
code_language: theme.code.language,
|
||||
copy_btn: theme.code.copy_btn,
|
||||
image_caption: theme.post.image_caption,
|
||||
image_zoom: theme.post.image_zoom,
|
||||
toc: theme.post.toc,
|
||||
lazyload: theme.lazyload,
|
||||
web_analytics: theme.web_analytics,
|
||||
search_path: urlJoin(config.root, theme.search.path),
|
||||
include_content_in_search: theme.search.content,
|
||||
};
|
||||
return `<script id="fluid-configs">
|
||||
var Fluid = window.Fluid || {};
|
||||
Fluid.ctx = Object.assign({}, Fluid.ctx)
|
||||
var CONFIG = ${JSON.stringify(exportConfig)};
|
||||
|
||||
if (CONFIG.web_analytics.follow_dnt) {
|
||||
var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
|
||||
Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
|
||||
}
|
||||
</script>`;
|
||||
});
|
||||
24
themes/fluid/scripts/helpers/import.js
Normal file
24
themes/fluid/scripts/helpers/import.js
Normal file
@ -0,0 +1,24 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
hexo.extend.helper.register('import_js', function(base, relative, ex = '') {
|
||||
if (!Array.isArray(this.page.script_snippets)) {
|
||||
this.page.script_snippets = [];
|
||||
}
|
||||
this.page.script_snippets.push(this.js_ex(base, relative, ex));
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('import_script', function(snippet) {
|
||||
if (!Array.isArray(this.page.script_snippets)) {
|
||||
this.page.script_snippets = [];
|
||||
}
|
||||
this.page.script_snippets.push(snippet);
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('import_css', function(base, relative, ex = '') {
|
||||
if (!Array.isArray(this.page.css_snippets)) {
|
||||
this.page.css_snippets = [];
|
||||
}
|
||||
this.page.css_snippets.push(this.css_ex(base, relative, ex));
|
||||
});
|
||||
7
themes/fluid/scripts/helpers/injects.js
Normal file
7
themes/fluid/scripts/helpers/injects.js
Normal file
@ -0,0 +1,7 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
hexo.extend.helper.register('point_injected', function(type) {
|
||||
return hexo.theme.config.injects[type] && hexo.theme.config.injects[type].length > 0;
|
||||
});
|
||||
25
themes/fluid/scripts/helpers/page.js
Normal file
25
themes/fluid/scripts/helpers/page.js
Normal file
@ -0,0 +1,25 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
hexo.extend.helper.register('prev_post', function prev_post(post) {
|
||||
const prev = post.prev;
|
||||
if (!prev) {
|
||||
return null;
|
||||
}
|
||||
if (prev.hide) {
|
||||
return prev_post(prev);
|
||||
}
|
||||
return prev;
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('next_post', function next_post(post) {
|
||||
const next = post.next;
|
||||
if (!next) {
|
||||
return null;
|
||||
}
|
||||
if (next.hide) {
|
||||
return next_post(next);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
50
themes/fluid/scripts/helpers/scope.js
Normal file
50
themes/fluid/scripts/helpers/scope.js
Normal file
@ -0,0 +1,50 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
const pageInScope = (page, scope) => {
|
||||
switch (scope) {
|
||||
case 'home':
|
||||
return Boolean(page.__index);
|
||||
case 'post':
|
||||
case 'posts':
|
||||
return Boolean(page.__post);
|
||||
case 'archives':
|
||||
case 'archive':
|
||||
return Boolean(page.archive);
|
||||
case 'categories':
|
||||
case 'category':
|
||||
return page.layout === 'categories' || page.layout === 'category';
|
||||
case 'tags':
|
||||
case 'tag':
|
||||
return page.layout === 'tags' || page.layout === 'tag';
|
||||
case 'about':
|
||||
return page.layout === 'about';
|
||||
case 'links':
|
||||
case 'link':
|
||||
return page.layout === 'links';
|
||||
case '404':
|
||||
return page.layout === '404';
|
||||
case 'page':
|
||||
case 'custom':
|
||||
return Boolean(page.__page);
|
||||
}
|
||||
};
|
||||
|
||||
hexo.extend.helper.register('in_scope', function(scope) {
|
||||
if (!scope || scope.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Array.isArray(scope)) {
|
||||
for (const each of scope) {
|
||||
if (pageInScope(this.page, each)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return pageInScope(this.page, scope);
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
17
themes/fluid/scripts/helpers/url.js
Normal file
17
themes/fluid/scripts/helpers/url.js
Normal file
@ -0,0 +1,17 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
const urlJoin = require('../utils/url-join');
|
||||
|
||||
hexo.extend.helper.register('css_ex', function(base, relative, ex = '') {
|
||||
return `<link ${ex} rel="stylesheet" href="${this.url_for(urlJoin(base, relative))}" />`;
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('js_ex', function(base, relative, ex = '') {
|
||||
return `<script ${ex} src="${this.url_for(urlJoin(base, relative))}" ></script>`;
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('url_join', function(base, relative) {
|
||||
return this.url_for(urlJoin(base, relative));
|
||||
});
|
||||
29
themes/fluid/scripts/helpers/utils.js
Normal file
29
themes/fluid/scripts/helpers/utils.js
Normal file
@ -0,0 +1,29 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
const md5 = require('../utils/crypto');
|
||||
const { decodeURL } = require('hexo-util');
|
||||
const compareVersions = require('../../scripts/utils/compare-versions');
|
||||
|
||||
hexo.extend.helper.register('md5', function(string) {
|
||||
return md5(string);
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('require_version', function(current, require) {
|
||||
const verRe = current.match(/[@/](\d{1,2})\.?(\d{0,2})\.?(\d{0,2})/);
|
||||
if (verRe && verRe.length >= 4) {
|
||||
const ver = `${verRe[1]}.${verRe[2] || 'x'}.${verRe[3] || 'x'}`;
|
||||
return compareVersions(ver, require) >= 0;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('deduplicate', function(arr) {
|
||||
if (!Array.isArray(arr)) {
|
||||
return [];
|
||||
}
|
||||
return [...new Set(arr)];
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('decode_url', decodeURL);
|
||||
42
themes/fluid/scripts/helpers/wordcount.js
Normal file
42
themes/fluid/scripts/helpers/wordcount.js
Normal file
@ -0,0 +1,42 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
const { stripHTML } = require('hexo-util');
|
||||
|
||||
const getWordCount = (post) => {
|
||||
// post.origin is the original post content of hexo-blog-encrypt
|
||||
const content = stripHTML(post.origin || post.content).replace(/\r?\n|\r/g, '').replace(/\s+/g, '');
|
||||
|
||||
if (!post.wordcount) {
|
||||
const zhCount = (content.match(/[\u4E00-\u9FA5]/g) || []).length;
|
||||
const enCount = (content.replace(/[\u4E00-\u9FA5]/g, '').match(/[a-zA-Z0-9_\u0392-\u03c9\u0400-\u04FF]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af\u0400-\u04FF]+|[\u00E4\u00C4\u00E5\u00C5\u00F6\u00D6]+|\w+/g) || []).length;
|
||||
post.wordcount = zhCount + enCount
|
||||
}
|
||||
return post.wordcount;
|
||||
};
|
||||
|
||||
const symbolsCount = (count) => {
|
||||
if (count > 9999) {
|
||||
count = Math.round(count / 1000) + 'k'; // > 9999 => 11k
|
||||
} else if (count > 999) {
|
||||
count = (Math.round(count / 100) / 10) + 'k'; // > 999 => 1.1k
|
||||
} // < 999 => 111
|
||||
return count;
|
||||
};
|
||||
|
||||
hexo.extend.helper.register('min2read', (post, { awl, wpm }) => {
|
||||
return Math.floor(getWordCount(post) / ((awl || 2) * (wpm || 60))) + 1;
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('wordcount', (post) => {
|
||||
return symbolsCount(getWordCount(post));
|
||||
});
|
||||
|
||||
hexo.extend.helper.register('wordtotal', (site) => {
|
||||
let count = 0;
|
||||
site.posts.forEach(post => {
|
||||
count += getWordCount(post);
|
||||
});
|
||||
return symbolsCount(count);
|
||||
});
|
||||
Reference in New Issue
Block a user