hexo-theme-shokax 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,140 +1,109 @@
1
- 'use strict'
2
- /* global hexo */
3
-
4
- const fs = require('hexo-fs')
5
-
1
+ 'use strict';
2
+ import fs from 'hexo-fs';
6
3
  const prepareQuery = (categories, parent) => {
7
- const query = {}
8
-
9
- if (parent) {
10
- query.parent = parent
11
- } else {
12
- query.parent = { $exists: false }
13
- }
14
-
15
- return categories.find(query).sort('name', 1).filter(cat => cat.length)
16
- }
17
-
4
+ const query = {
5
+ parent: undefined
6
+ };
7
+ if (parent) {
8
+ query.parent = parent;
9
+ }
10
+ else {
11
+ query.parent = { $exists: false };
12
+ }
13
+ return categories.find(query).sort('name', 1).filter(cat => cat.length);
14
+ };
18
15
  hexo.extend.helper.register('_list_categories', function (depth = 0) {
19
- // let hexo = this
20
- const categories = this.site.categories
21
-
22
- if (!categories || !categories.length) return ''
23
-
24
- const hierarchicalList = (level, parent) => {
25
- let result = ''
26
-
27
- prepareQuery(categories, parent).forEach((cat, i) => {
28
- let child
29
-
30
- if (level + 1 < depth) {
31
- child = hierarchicalList(level + 1, cat._id)
32
- }
33
-
34
- const catname = `<a itemprop="url" href="${this.url_for(cat.path)}">${cat.name}</a><small>( ${cat.length} )</small>`
35
-
36
- switch (level) {
37
- case 0:
38
- result += `<div><h2 class="item header">${catname}</h2>`
39
- break
40
-
41
- case 1:
42
- result += `<h3 class="item section">${catname}</h3>`
43
- break
44
-
45
- case 2:
46
- result += `<div class="item normal"><div class="title">${catname}</div></div>`
47
- break
48
- }
49
-
50
- if (child) {
51
- result += `${child}`
52
- }
53
-
54
- if (level === 0) {
55
- result += '</div>'
56
- }
57
- })
58
-
59
- return result
60
- }
61
-
62
- return hierarchicalList(0)
63
- })
64
-
16
+ const categories = this.site.categories;
17
+ if (!categories || !categories.length)
18
+ return '';
19
+ const hierarchicalList = (level, parent) => {
20
+ let result = '';
21
+ prepareQuery(categories, parent).forEach((cat, i) => {
22
+ let child;
23
+ if (level + 1 < depth) {
24
+ child = hierarchicalList(level + 1, cat._id);
25
+ }
26
+ const catname = `<a itemprop="url" href="${this.url_for(cat.path)}">${cat.name}</a><small>( ${cat.length} )</small>`;
27
+ switch (level) {
28
+ case 0:
29
+ result += `<div><h2 class="item header">${catname}</h2>`;
30
+ break;
31
+ case 1:
32
+ result += `<h3 class="item section">${catname}</h3>`;
33
+ break;
34
+ case 2:
35
+ result += `<div class="item normal"><div class="title">${catname}</div></div>`;
36
+ break;
37
+ }
38
+ if (child) {
39
+ result += `${child}`;
40
+ }
41
+ if (level === 0) {
42
+ result += '</div>';
43
+ }
44
+ });
45
+ return result;
46
+ };
47
+ return hierarchicalList(0);
48
+ });
65
49
  hexo.extend.helper.register('_categories', function () {
66
- // let hexo = this
67
- const categories = this.site.categories
68
- if (!categories || !categories.length) return ''
69
-
70
- const pangu = this.theme.pangu
71
- ? require('pangu')
72
- : {
73
- spacing: data => {
74
- return data
50
+ const categories = this.site.categories;
51
+ if (!categories || !categories.length)
52
+ return '';
53
+ const pangu = this.theme.pangu
54
+ ? require('pangu')
55
+ : {
56
+ spacing: data => {
57
+ return data;
58
+ }
59
+ };
60
+ const result = {};
61
+ categories.forEach((cat, i) => {
62
+ const child = prepareQuery(categories, cat._id);
63
+ const cover = 'source/_posts' + cat.path.replace(this.config.category_dir, '') + 'cover.jpg';
64
+ if (fs.existsSync(cover)) {
65
+ const className = cat.slug.split('/');
66
+ className.pop();
67
+ cat.class = className.join(' ');
68
+ cat.name = pangu.spacing(cat.name);
69
+ if (child.length !== 0) {
70
+ cat.child = child;
71
+ }
72
+ result[cat._id] = cat;
75
73
  }
76
- }
77
-
78
- const result = {}
79
-
80
- categories.forEach((cat, i) => {
81
- const child = prepareQuery(categories, cat._id)
82
- const cover = 'source/_posts' + cat.path.replace(this.config.category_dir, '') + 'cover.jpg'
83
-
84
- if (fs.existsSync(cover)) {
85
- const className = cat.slug.split('/')
86
- className.pop()
87
- cat.class = className.join(' ')
88
- cat.name = pangu.spacing(cat.name)
89
-
90
- if (child.length !== 0) {
91
- cat.child = child
92
- }
93
-
94
- result[cat._id] = cat
95
- }
96
- })
97
-
98
- return result
99
- })
100
-
74
+ });
75
+ return result;
76
+ });
101
77
  hexo.extend.helper.register('_category_prev', function (name) {
102
- // let hexo = this
103
- const categories = this.site.categories
104
- if (!categories || !categories.length) return ''
105
-
106
- let result = ''
107
-
108
- categories.find({ name }).forEach((current) => {
109
- if (current.parent) {
110
- categories.find({ _id: current.parent }).forEach((cat, i) => {
111
- result += `<a href="${this.url_for(cat.path)}">${cat.name}</a>`
112
- })
113
- }
114
- })
115
-
116
- return result
117
- })
118
-
78
+ const categories = this.site.categories;
79
+ if (!categories || !categories.length)
80
+ return '';
81
+ let result = '';
82
+ categories.find({ name }).forEach((current) => {
83
+ if (current.parent) {
84
+ categories.find({ _id: current.parent }).forEach((cat, i) => {
85
+ result += `<a href="${this.url_for(cat.path)}">${cat.name}</a>`;
86
+ });
87
+ }
88
+ });
89
+ return result;
90
+ });
119
91
  hexo.extend.helper.register('_category_posts', function (page) {
120
- // let hexo = this
121
- const categories = this.site.categories
122
- if (!categories || !categories.length || !page.categories || !page.categories.length) return ''
123
-
124
- let result = ''
125
- const cat = page.categories.toArray()
126
-
127
- categories.find({ _id: cat[cat.length - 1]._id }).forEach((category) => {
128
- if (category.posts) {
129
- category.posts.sort('date', 1).forEach((post) => {
130
- let current = ''
131
- if (post.path === page.path) {
132
- current = ' class="active"'
92
+ const categories = this.site.categories;
93
+ if (!categories || !categories.length || !page.categories || !page.categories.length)
94
+ return '';
95
+ let result = '';
96
+ const cat = page.categories.toArray();
97
+ categories.find({ _id: cat[cat.length - 1]._id }).forEach((category) => {
98
+ if (category.posts) {
99
+ category.posts.sort('date', 1).forEach((post) => {
100
+ let current = '';
101
+ if (post.path === page.path) {
102
+ current = ' class="active"';
103
+ }
104
+ result += `<li ${current}><a href="${this.url_for(post.path)}" rel="bookmark" title="${post.title}">${post.title}</a></li>`;
105
+ });
133
106
  }
134
- result += `<li ${current}><a href="${this.url_for(post.path)}" rel="bookmark" title="${post.title}">${post.title}</a></li>`
135
- })
136
- }
137
- })
138
-
139
- return result
140
- })
107
+ });
108
+ return result;
109
+ });
@@ -1,76 +1,62 @@
1
- 'use strict'
2
- /* global hexo */
3
- /*!
4
- hexo-symbols-count-time by theme-next
5
- under GNU Lesser General Public License v3.0
6
- https://github.com/theme-next/hexo-symbols-count-time/blob/master/LICENSE
7
- */
8
-
9
- const { stripHTML } = require('hexo-util')
10
-
1
+ 'use strict';
2
+ import { stripHTML } from 'hexo-util';
11
3
  const config = hexo.config.symbols_count_time = Object.assign({
12
- symbols: true,
13
- time: true,
14
- total_symbols: true,
15
- total_time: true,
16
- exclude_codeblock: false,
17
- awl: 4,
18
- wpm: 275,
19
- suffix: 'mins.'
20
- }, hexo.config.symbols_count_time)
21
-
22
- function getSymbols (post) {
23
- return post?._content?.length ?? post?.content?.length ?? post.length
4
+ symbols: true,
5
+ time: true,
6
+ total_symbols: true,
7
+ total_time: true,
8
+ exclude_codeblock: false,
9
+ awl: 4,
10
+ wpm: 275,
11
+ suffix: 'mins.'
12
+ }, hexo.config.symbols_count_time);
13
+ function getSymbols(post) {
14
+ return post?._content?.length ?? post?.content?.length ?? post.length;
24
15
  }
25
-
26
- function getSymbolsTotal (site) {
27
- let symbolsResultCount = 0
28
- site.posts.forEach((post) => {
29
- symbolsResultCount += getSymbols(post)
30
- })
31
- return symbolsResultCount
16
+ function getSymbolsTotal(site) {
17
+ let symbolsResultCount = 0;
18
+ site.posts.forEach((post) => {
19
+ symbolsResultCount += getSymbols(post);
20
+ });
21
+ return symbolsResultCount;
32
22
  }
33
-
34
- function getFormatTime (minutes, suffix) {
35
- const fHours = Math.floor(minutes / 60)
36
- let fMinutes = Math.floor(minutes - (fHours * 60))
37
- if (fMinutes < 1) {
38
- fMinutes = 1 // 0 => 1
39
- }
40
- return fHours < 1
41
- ? fMinutes + ' ' + suffix // < 59 => 59 mins.
42
- : fHours + ':' + ('00' + fMinutes).slice(-2) // = 61 => 1:01
23
+ function getFormatTime(minutes, suffix) {
24
+ const fHours = Math.floor(minutes / 60);
25
+ let fMinutes = Math.floor(minutes - (fHours * 60));
26
+ if (fMinutes < 1) {
27
+ fMinutes = 1;
28
+ }
29
+ return fHours < 1
30
+ ? fMinutes + ' ' + suffix
31
+ : fHours + ':' + ('00' + fMinutes).slice(-2);
43
32
  }
44
-
45
33
  hexo.extend.helper.register('symbolsCount', function (post) {
46
- let symbolsResult = getSymbols(post)
47
- if (symbolsResult > 9999) {
48
- symbolsResult = Math.round(symbolsResult / 1000) + 'k' // > 9999 => 11k
49
- } else if (symbolsResult > 999) {
50
- symbolsResult = Math.round(symbolsResult / 100) / 10 + 'k' // > 999 => 1.1k
51
- } // < 999 => 111
52
- return symbolsResult
53
- })
54
-
34
+ let symbolsResult = getSymbols(post);
35
+ if (symbolsResult > 9999) {
36
+ symbolsResult = Math.round(symbolsResult / 1000) + 'k';
37
+ }
38
+ else if (symbolsResult > 999) {
39
+ symbolsResult = Math.round(symbolsResult / 100) / 10 + 'k';
40
+ }
41
+ return symbolsResult;
42
+ });
55
43
  hexo.extend.helper.register('symbolsTime', function (post, awl = config.awl, wpm = config.wpm, suffix = config.suffix) {
56
- const minutes = Math.round(getSymbols(post) / (awl * wpm))
57
- return getFormatTime(minutes, suffix)
58
- })
59
-
44
+ const minutes = Math.round(getSymbols(post) / (awl * wpm));
45
+ return getFormatTime(minutes, suffix);
46
+ });
60
47
  hexo.extend.helper.register('symbolsCountTotal', function (site) {
61
- const symbolsResultTotal = getSymbolsTotal(site)
62
- return symbolsResultTotal < 1000000
63
- ? Math.round(symbolsResultTotal / 1000) + 'k' // < 999k => 111k
64
- : Math.round(symbolsResultTotal / 100000) / 10 + 'm' // > 999k => 1.1m
65
- })
66
-
48
+ const symbolsResultTotal = getSymbolsTotal(site);
49
+ return symbolsResultTotal < 1000000
50
+ ? Math.round(symbolsResultTotal / 1000) + 'k'
51
+ : Math.round(symbolsResultTotal / 100000) / 10 + 'm';
52
+ });
67
53
  hexo.extend.helper.register('symbolsTimeTotal', function (site, awl = config.awl, wpm = config.wpm, suffix = config.suffix) {
68
- const minutes = Math.round(getSymbolsTotal(site) / (awl * wpm))
69
- return getFormatTime(minutes, suffix)
70
- })
71
-
54
+ const minutes = Math.round(getSymbolsTotal(site) / (awl * wpm));
55
+ return getFormatTime(minutes, suffix);
56
+ });
72
57
  hexo.extend.filter.register('after_post_render', (data) => {
73
- let { content } = data
74
- if (config.exclude_codeblock) content = content.replace(/<pre>.*?<\/pre>/g, '')
75
- data.length = stripHTML(content).replace(/\r?\n|\r/g, '').replace(/\s+/g, '').length
76
- }, 0)
58
+ let { content } = data;
59
+ if (config.exclude_codeblock)
60
+ content = content.replace(/<pre>.*?<\/pre>/g, '');
61
+ data.length = stripHTML(content).replace(/\r?\n|\r/g, '').replace(/\s+/g, '').length;
62
+ }, 0);
@@ -1,49 +1,45 @@
1
- /* global hexo */
2
-
3
1
  hexo.on('generateBefore', () => {
4
- // 加载`theme_injects`过滤器
5
- require('./lib/injects')(hexo)
6
- })
7
-
2
+ require('./lib/injects')(hexo);
3
+ });
8
4
  hexo.on('generateAfter', () => {
9
- // 检查版本更新
10
- const https = require('https')
11
- const path = require('path')
12
- const { version } = require(path.normalize('../../package.json'))
13
- https.get('https://api.github.com/repos/zkz098/hexo-theme-shokaX/releases/latest', {
14
- headers: {
15
- 'User-Agent': 'Theme ShokaX Client'
16
- }
17
- }, (res) => {
18
- let result = ''
19
- res.on('data', (data) => {
20
- result += data
21
- })
22
- res.on('end', () => {
23
- try {
24
- const latest = JSON.parse(result).tag_name.replace('v', '').split('.')
25
- const current = version.split('.')
26
- let isOutdated = false
27
- for (let i = 0; i < Math.max(latest.length, current.length); i++) {
28
- if (!current[i] || latest[i] > current[i]) {
29
- isOutdated = true
30
- break
31
- }
32
- if (latest[i] < current[i]) {
33
- break
34
- }
5
+ const https = require('https');
6
+ const path = require('path');
7
+ const { version } = require(path.normalize('../../package.json'));
8
+ https.get('https://api.github.com/repos/theme-shoka-x/hexo-theme-shokaX/releases/latest', {
9
+ headers: {
10
+ 'User-Agent': 'Theme ShokaX Client'
35
11
  }
36
- if (isOutdated) {
37
- hexo.log.warn(`Your theme ShokaX is outdated. Current version: v${current.join('.')}, latest version: v${latest.join('.')}`)
38
- hexo.log.warn('Visit https://github.com/zkz098/hexo-theme-shokaX/releases for more information.')
39
- }
40
- } catch (err) {
41
- hexo.log.error('Failed to detect version info. Error message:')
42
- hexo.log.error(err)
43
- }
44
- })
45
- }).on('error', err => {
46
- hexo.log.error('Failed to detect version info. Error message:')
47
- hexo.log.error(err)
48
- })
49
- })
12
+ }, (res) => {
13
+ let result = '';
14
+ res.on('data', (data) => {
15
+ result += data;
16
+ });
17
+ res.on('end', () => {
18
+ try {
19
+ const latest = JSON.parse(result).tag_name.replace('v', '').split('.');
20
+ const current = version.split('.');
21
+ let isOutdated = false;
22
+ for (let i = 0; i < Math.max(latest.length, current.length); i++) {
23
+ if (!current[i] || latest[i] > current[i]) {
24
+ isOutdated = true;
25
+ break;
26
+ }
27
+ if (latest[i] < current[i]) {
28
+ break;
29
+ }
30
+ }
31
+ if (isOutdated) {
32
+ hexo.log.warn(`Your theme ShokaX is outdated. Current version: v${current.join('.')}, latest version: v${latest.join('.')}`);
33
+ hexo.log.warn('Visit https://github.com/theme-shoka-x/hexo-theme-shokaX/releases for more information.');
34
+ }
35
+ }
36
+ catch (err) {
37
+ hexo.log.error('Failed to detect version info. Error message:');
38
+ hexo.log.error(err);
39
+ }
40
+ });
41
+ }).on('error', err => {
42
+ hexo.log.error('Failed to detect version info. Error message:');
43
+ hexo.log.error(err);
44
+ });
45
+ });
@@ -1,20 +1,19 @@
1
- 'use strict'
2
-
3
- module.exports = {
4
- views: [
5
- 'head',
6
- 'sidebar',
7
- 'rightNav',
8
- 'postMeta',
9
- 'postBodyEnd',
10
- 'footer',
11
- 'bodyEnd',
12
- 'comment',
13
- 'status'
14
- ],
15
- styles: [
16
- 'variable',
17
- 'mixin',
18
- 'style'
19
- ]
20
- }
1
+ 'use strict';
2
+ export default {
3
+ views: [
4
+ 'head',
5
+ 'sidebar',
6
+ 'rightNav',
7
+ 'postMeta',
8
+ 'postBodyEnd',
9
+ 'footer',
10
+ 'bodyEnd',
11
+ 'comment',
12
+ 'status'
13
+ ],
14
+ styles: [
15
+ 'variable',
16
+ 'mixin',
17
+ 'style'
18
+ ]
19
+ };
@@ -1,89 +1,66 @@
1
- 'use strict'
2
-
3
- // 插件部分参考自theme-next
4
- const fs = require('fs')
5
- const path = require('path')
6
- const points = require('./injects-point')
7
- const defaultExtname = '.pug'
8
-
9
- // Defining stylus types
1
+ 'use strict';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import points from './injects-point';
5
+ const defaultExtname = '.pug';
10
6
  class StylusInject {
11
- constructor (base_dir) {
12
- this.base_dir = base_dir
13
- this.files = []
14
- }
15
-
16
- push (file) {
17
- // Get absolute path base on hexo dir
18
- this.files.push(path.resolve(this.base_dir, file))
19
- }
7
+ constructor(base_dir) {
8
+ this.base_dir = base_dir;
9
+ this.files = [];
10
+ }
11
+ push(file) {
12
+ this.files.push(path.resolve(this.base_dir, file));
13
+ }
20
14
  }
21
-
22
- // Defining view types
23
15
  class ViewInject {
24
- constructor (base_dir) {
25
- this.base_dir = base_dir
26
- this.raws = []
27
- }
28
-
29
- raw (name, raw, ...args) {
30
- // Set default extname
31
- if (path.extname(name) === '') {
32
- name += defaultExtname
16
+ constructor(base_dir) {
17
+ this.base_dir = base_dir;
18
+ this.raws = [];
33
19
  }
34
- this.raws.push({ name, raw, args })
35
- }
36
-
37
- file (name, file, ...args) {
38
- // Set default extname from file's extname
39
- if (path.extname(name) === '') {
40
- name += path.extname(file)
20
+ raw(name, raw, ...args) {
21
+ if (path.extname(name) === '') {
22
+ name += defaultExtname;
23
+ }
24
+ this.raws.push({ name, raw, args });
25
+ }
26
+ file(name, file, ...args) {
27
+ if (path.extname(name) === '') {
28
+ name += path.extname(file);
29
+ }
30
+ this.raw(name, fs.readFileSync(path.resolve(this.base_dir, file), 'utf8'), ...args);
41
31
  }
42
- // Get absolute path base on hexo dir
43
- this.raw(name, fs.readFileSync(path.resolve(this.base_dir, file), 'utf8'), ...args)
44
- }
45
32
  }
46
-
47
- // Init injects
48
- function initInject (base_dir) {
49
- const injects = {}
50
- points.styles.forEach(item => {
51
- injects[item] = new StylusInject(base_dir)
52
- })
53
- points.views.forEach(item => {
54
- injects[item] = new ViewInject(base_dir)
55
- })
56
- return injects
33
+ function initInject(base_dir) {
34
+ const injects = {};
35
+ points.styles.forEach(item => {
36
+ injects[item] = new StylusInject(base_dir);
37
+ });
38
+ points.views.forEach(item => {
39
+ injects[item] = new ViewInject(base_dir);
40
+ });
41
+ return injects;
57
42
  }
58
-
59
43
  module.exports = (hexo) => {
60
- // Exec theme_inject filter
61
- const injects = initInject(hexo.base_dir)
62
- hexo.execFilterSync('theme_inject', injects)
63
- hexo.theme.config.injects = {}
64
-
65
- // Inject stylus
66
- points.styles.forEach(type => {
67
- hexo.theme.config.injects[type] = injects[type].files
68
- })
69
-
70
- // Inject views
71
- points.views.forEach(type => {
72
- const configs = Object.create(null)
73
- hexo.theme.config.injects[type] = []
74
- // Add or override view.
75
- injects[type].raws.forEach((injectObj, index) => {
76
- const name = `inject/${type}/${injectObj.name}`
77
- hexo.theme.setView(name, injectObj.raw)
78
- configs[name] = {
79
- layout: name,
80
- locals: injectObj.args[0],
81
- options: injectObj.args[1],
82
- order: injectObj.args[2] || index
83
- }
84
- })
85
- // Views sort.
86
- hexo.theme.config.injects[type] = Object.values(configs)
87
- .sort((x, y) => x.order - y.order)
88
- })
89
- }
44
+ const injects = initInject(hexo.base_dir);
45
+ hexo.execFilterSync('theme_inject', injects);
46
+ hexo.theme.config.injects = {};
47
+ points.styles.forEach(type => {
48
+ hexo.theme.config.injects[type] = injects[type].files;
49
+ });
50
+ points.views.forEach(type => {
51
+ const configs = Object.create(null);
52
+ hexo.theme.config.injects[type] = [];
53
+ injects[type].raws.forEach((injectObj, index) => {
54
+ const name = `inject/${type}/${injectObj.name}`;
55
+ hexo.theme.setView(name, injectObj.raw);
56
+ configs[name] = {
57
+ layout: name,
58
+ locals: injectObj.args[0],
59
+ options: injectObj.args[1],
60
+ order: injectObj.args[2] || index
61
+ };
62
+ });
63
+ hexo.theme.config.injects[type] = Object.values(configs)
64
+ .sort((x, y) => x.order - y.order);
65
+ });
66
+ };