hexo-theme-shokax 0.5.0-dev-36bb58f → 0.5.0-dev-f96faf9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/_config.yml CHANGED
@@ -38,8 +38,11 @@ experiments:
38
38
 
39
39
  homeConfig:
40
40
  gradient: false # 使用CSS渐变作为文章封面
41
- # fixedCover 性能比默认的更好,且开启时将启用LCP优化和预加载 TODO
41
+ # fixedCover 性能比默认的更好,且开启时将启用LCP优化和预加载
42
42
  fixedCover: "" # 主页面cover(为空则使用bing随机图片)
43
+ cateCards:
44
+ - slug: # 分类 slug
45
+ cover: # 分类封面
43
46
 
44
47
  # ShokaX 模块化分包引入设置
45
48
  # 请关闭所有不使用的模块以优化主题 js 体积和性能
@@ -1,5 +1,5 @@
1
1
  mixin CardRender(item)
2
- - cover = url_for(theme.statics + item.slug + '/cover.jpg')
2
+ - cover = url_for(theme.statics + item.slug + this.getCoverExt(item.slug))
3
3
  - itemname = item.name
4
4
 
5
5
  section(class="item")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-theme-shokax",
3
- "version": "0.5.0-dev-36bb58f",
3
+ "version": "0.5.0-dev-f96faf9",
4
4
  "description": "a hexo theme based on shoka",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/theme-shoka-x/hexo-theme-shokaX",
@@ -21,12 +21,29 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
22
  mod
23
23
  ));
24
+ var import_promises = require("node:fs/promises");
24
25
  var import_hexo_pagination = __toESM(require("hexo-pagination"));
25
- const fs = require("hexo-fs");
26
+ function getFileExtension(path) {
27
+ const filename = path.split(/[\\/]/).pop() || "";
28
+ const lastDotIndex = filename.lastIndexOf(".");
29
+ return lastDotIndex > 0 ? filename.slice(lastDotIndex + 1) : "";
30
+ }
26
31
  hexo.config.index_generator = Object.assign({
27
32
  per_page: typeof hexo.config.per_page === "undefined" ? 10 : hexo.config.per_page,
28
33
  order_by: "-date"
29
34
  }, hexo.config.index_generator);
35
+ hexo.extend.helper.register("getCoverExt", function(path) {
36
+ if (theme.homeConfig.cateCards.length > 0) {
37
+ const cardMap = /* @__PURE__ */ new Map();
38
+ theme.homeConfig.cateCards.forEach((card) => {
39
+ cardMap.set(card.slug, card.cover);
40
+ });
41
+ if (cardMap.has(path)) {
42
+ const cover = cardMap.get(path);
43
+ return getFileExtension(cover);
44
+ }
45
+ }
46
+ });
30
47
  hexo.extend.generator.register("index", function(locals) {
31
48
  const covers = [];
32
49
  const catlist = [];
@@ -37,6 +54,7 @@ hexo.extend.generator.register("index", function(locals) {
37
54
  const paginationDir = config.pagination_dir || "page";
38
55
  const path = config.index_generator.path || "";
39
56
  const categories = locals.categories;
57
+ const theme2 = hexo.theme.config;
40
58
  const getTopcat = function(cat) {
41
59
  if (cat.parent) {
42
60
  const pCat = categories.findOne({ _id: cat.parent });
@@ -46,48 +64,40 @@ hexo.extend.generator.register("index", function(locals) {
46
64
  }
47
65
  };
48
66
  if (categories && categories.length) {
49
- categories.forEach((cat) => {
67
+ categories.forEach(async (cat) => {
50
68
  const cover = `source/_posts/${cat.slug}`;
51
- if (fs.existsSync(cover + "/cover.avif")) {
52
- covers.push({
53
- path: cat.slug + "/cover.avif",
54
- data: function() {
55
- return fs.createReadStream(cover + "/cover.avif");
56
- }
69
+ if (theme2.homeConfig.cateCards.length > 0) {
70
+ const cardMap = /* @__PURE__ */ new Map();
71
+ theme2.homeConfig.cateCards.forEach((card) => {
72
+ cardMap.set(card.slug, card.cover);
57
73
  });
58
- } else if (fs.existsSync(cover + "/cover.webp")) {
59
- covers.push({
60
- path: cat.slug + "/cover.webp",
61
- data: function() {
62
- return fs.createReadStream(cover + "/cover.webp");
74
+ if (cardMap.has(cat.slug)) {
75
+ const cover2 = cardMap.get(cat.slug);
76
+ const coverData = await (0, import_promises.readFile)(`source/_posts/${cover2}`);
77
+ covers.push({
78
+ path: `${cat.slug}/cover.${getFileExtension(cover2)}`,
79
+ data: coverData
80
+ });
81
+ const topcat = getTopcat(cat);
82
+ if (topcat._id !== cat._id) {
83
+ cat.top = topcat;
63
84
  }
64
- });
65
- } else if (fs.existsSync(cover + "/cover.jpg")) {
66
- covers.push({
67
- path: cat.slug + "/cover.jpg",
68
- data: function() {
69
- return fs.createReadStream(cover + "/cover.jpg");
70
- }
71
- });
72
- const topcat = getTopcat(cat);
73
- if (topcat._id !== cat._id) {
74
- cat.top = topcat;
75
- }
76
- const child = categories.find({ parent: cat._id });
77
- let pl = 6;
78
- if (child.length !== 0) {
79
- cat.child = child.length;
80
- cat.subs = child.sort({ name: 1 }).limit(6).toArray();
81
- pl = Math.max(0, pl - child.length);
82
- if (pl > 0) {
83
- cat.subs.push(...cat.posts.sort({ title: 1 }).filter(function(item, i) {
84
- return item.categories.last()._id === cat._id;
85
- }).limit(pl).toArray());
85
+ const child = categories.find({ parent: cat._id });
86
+ let pl = 6;
87
+ if (child.length !== 0) {
88
+ cat.child = child.length;
89
+ cat.subs = child.sort({ name: 1 }).limit(6).toArray();
90
+ pl = Math.max(0, pl - child.length);
91
+ if (pl > 0) {
92
+ cat.subs.push(...cat.posts.sort({ title: 1 }).filter(function(item, i) {
93
+ return item.categories.last()._id === cat._id;
94
+ }).limit(pl).toArray());
95
+ }
96
+ } else {
97
+ cat.subs = cat.posts.sort({ title: 1 }).limit(6).toArray();
86
98
  }
87
- } else {
88
- cat.subs = cat.posts.sort({ title: 1 }).limit(6).toArray();
99
+ catlist.push(cat);
89
100
  }
90
- catlist.push(cat);
91
101
  }
92
102
  });
93
103
  }
@@ -97,7 +97,7 @@ hexo.extend.generator.register("script", async function(locals) {
97
97
  enterPoint = "node_modules/hexo-theme-shokax/source/js/_app/pjax/siteInit.ts";
98
98
  patchDir = "node_modules/hexo-theme-shokax/source/js/_app/components/cloudflare.ts";
99
99
  }
100
- const result = await (0, import_esbuild.build)({
100
+ await (0, import_esbuild.build)({
101
101
  entryPoints: [enterPoint],
102
102
  bundle: true,
103
103
  outdir: "shokaxTemp",
@@ -136,27 +136,27 @@ hexo.extend.generator.register("script", async function(locals) {
136
136
  }
137
137
  });
138
138
  const res = [];
139
- result.outputFiles.forEach((file) => {
140
- const fileName = file.path.split("/").pop();
141
- if (fileName.endsWith(".js")) {
139
+ (await import_promises.default.readdir("shokaxTemp")).forEach(async (file) => {
140
+ const fileContent = await import_promises.default.readFile("shokaxTemp/" + file);
141
+ if (file.endsWith(".js")) {
142
142
  res.push({
143
- path: theme.js + "/" + fileName,
144
- data: file.text
143
+ path: theme.js + "/" + file,
144
+ data: fileContent
145
145
  });
146
- } else if (fileName.endsWith(".css")) {
146
+ } else if (file.endsWith(".css")) {
147
147
  res.push({
148
- path: theme.css + "/" + fileName,
149
- data: file.text
148
+ path: theme.css + "/" + file,
149
+ data: fileContent
150
150
  });
151
151
  } else {
152
152
  res.push({
153
- path: theme.js + "/" + fileName,
154
- data: file.text
153
+ path: theme.statics + "/" + file,
154
+ data: fileContent
155
155
  });
156
156
  }
157
157
  });
158
158
  if (theme.experiments.cloudflarePatch) {
159
- const patchRes = await (0, import_esbuild.build)({
159
+ await (0, import_esbuild.build)({
160
160
  entryPoints: [patchDir],
161
161
  bundle: true,
162
162
  platform: "browser",
@@ -176,7 +176,7 @@ hexo.extend.generator.register("script", async function(locals) {
176
176
  });
177
177
  res.push({
178
178
  path: theme.js + "/cf-patch.js",
179
- data: patchRes.outputFiles[0].text
179
+ data: await import_promises.default.readFile("cf-patch.js")
180
180
  });
181
181
  }
182
182
  return res;
@@ -23,7 +23,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
  ));
24
24
  var import_hexo_util = require("hexo-util");
25
25
  var import_node_fs = __toESM(require("node:fs"));
26
- const randomServer = parseInt(String(Math.random() * 4), 10) + 1;
27
26
  const randomBG = function(count = 1, image_server = null, image_list = []) {
28
27
  let i;
29
28
  if (image_server) {
@@ -37,13 +36,12 @@ const randomBG = function(count = 1, image_server = null, image_list = []) {
37
36
  return image_server + "?" + Math.floor(Math.random() * 999999);
38
37
  }
39
38
  const parseImage = function(img, size) {
40
- if (img.startsWith("//") || img.startsWith("http")) {
39
+ if (img.startsWith("//") || img.startsWith("https")) {
41
40
  return img;
42
41
  } else if (hexo.theme.config.experiments?.usingRelative) {
43
42
  return img;
44
43
  } else {
45
- console.warn("sinaimg blocked all request from outside website,so don't use this format");
46
- return `https://tva${randomServer}.sinaimg.cn/` + size + "/" + img;
44
+ throw new Error("Image URL is not valid: " + img);
47
45
  }
48
46
  };
49
47
  if (count && count > 1) {
@@ -3,7 +3,7 @@ var import_promises = require("node:fs/promises");
3
3
  hexo.on("generateBefore", async () => {
4
4
  await (0, import_promises.rm)("./shokaxTemp", { force: true, recursive: true });
5
5
  try {
6
- (0, import_promises.unlink)("cf-patch.js");
6
+ await (0, import_promises.unlink)("cf-patch.js");
7
7
  } catch (e) {
8
8
  }
9
9
  });