@sugarat/theme 0.2.12 → 0.2.13
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/node.js +39 -0
- package/package.json +2 -1
- package/src/utils/node/vitePlugins.ts +47 -1
package/node.js
CHANGED
|
@@ -442,12 +442,15 @@ function patchVPThemeConfig(cfg, vpThemeConfig = {}) {
|
|
|
442
442
|
var import_node_path3 = __toESM(require("path"));
|
|
443
443
|
var import_node_child_process2 = require("child_process");
|
|
444
444
|
var import_node_process2 = __toESM(require("process"));
|
|
445
|
+
var import_node_fs2 = require("fs");
|
|
446
|
+
var import_node_buffer = require("buffer");
|
|
445
447
|
var import_vitepress_plugin_pagefind = require("vitepress-plugin-pagefind");
|
|
446
448
|
var import_vitepress_plugin_rss = require("vitepress-plugin-rss");
|
|
447
449
|
function getVitePlugins(cfg) {
|
|
448
450
|
const plugins = [];
|
|
449
451
|
const buildEndFn = [];
|
|
450
452
|
plugins.push(inlineBuildEndPlugin(buildEndFn));
|
|
453
|
+
plugins.push(coverImgTransform());
|
|
451
454
|
if (cfg && cfg.search !== false) {
|
|
452
455
|
const ops = cfg.search instanceof Object ? cfg.search : {};
|
|
453
456
|
plugins.push(
|
|
@@ -490,6 +493,42 @@ function inlineBuildEndPlugin(buildEndFn) {
|
|
|
490
493
|
}
|
|
491
494
|
};
|
|
492
495
|
}
|
|
496
|
+
function coverImgTransform() {
|
|
497
|
+
let blogConfig;
|
|
498
|
+
let vitepressConfig;
|
|
499
|
+
let assetsDir;
|
|
500
|
+
return {
|
|
501
|
+
name: "@sugarat/theme-plugin-cover-transform",
|
|
502
|
+
apply: "build",
|
|
503
|
+
enforce: "pre",
|
|
504
|
+
configResolved(config) {
|
|
505
|
+
vitepressConfig = config.vitepress;
|
|
506
|
+
assetsDir = vitepressConfig.assetsDir;
|
|
507
|
+
blogConfig = config.vitepress.site.themeConfig.blog;
|
|
508
|
+
},
|
|
509
|
+
async generateBundle(_, bundle) {
|
|
510
|
+
const assetsMap = Object.entries(bundle).filter(([key]) => {
|
|
511
|
+
return key.startsWith(assetsDir);
|
|
512
|
+
}).map(([_2, value]) => {
|
|
513
|
+
return value;
|
|
514
|
+
});
|
|
515
|
+
for (const page of blogConfig.pagesData) {
|
|
516
|
+
const { cover } = page.meta;
|
|
517
|
+
if (!cover?.startsWith("/")) {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
try {
|
|
521
|
+
const realPath = import_node_path3.default.join(vitepressConfig.root, cover);
|
|
522
|
+
const fileBuffer = (0, import_node_fs2.readFileSync)(realPath);
|
|
523
|
+
const matchAsset = assetsMap.find((v) => import_node_buffer.Buffer.compare(fileBuffer, v.source));
|
|
524
|
+
page.meta.cover = joinPath("/", matchAsset.fileName);
|
|
525
|
+
} catch (e) {
|
|
526
|
+
vitepressConfig.logger.warn(e?.message);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
}
|
|
493
532
|
|
|
494
533
|
// src/node.ts
|
|
495
534
|
function getThemeConfig(cfg) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sugarat/theme",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"description": "简约风的 Vitepress 博客主题,sugarat vitepress blog theme",
|
|
5
5
|
"author": "sugar",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
"@documate/vue": "^0.3.1",
|
|
52
52
|
"@element-plus/icons-vue": "^2.1.0",
|
|
53
53
|
"element-plus": "^2.3.4",
|
|
54
|
+
"javascript-stringify": "^2.1.0",
|
|
54
55
|
"pagefind": "1.0.3",
|
|
55
56
|
"sass": "^1.56.1",
|
|
56
57
|
"typescript": "^4.8.2",
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import { execSync } from 'node:child_process'
|
|
3
3
|
import process from 'node:process'
|
|
4
|
+
import { readFileSync } from 'node:fs'
|
|
5
|
+
import { Buffer } from 'node:buffer'
|
|
4
6
|
import type { SiteConfig } from 'vitepress'
|
|
7
|
+
|
|
5
8
|
import {
|
|
6
9
|
chineseSearchOptimize,
|
|
7
10
|
pagefindPlugin
|
|
8
11
|
} from 'vitepress-plugin-pagefind'
|
|
9
12
|
import { RssPlugin } from 'vitepress-plugin-rss'
|
|
10
13
|
import type { Theme } from '../../composables/config/index'
|
|
14
|
+
import { joinPath } from './index'
|
|
11
15
|
|
|
12
16
|
export function getVitePlugins(cfg?: Partial<Theme.BlogConfig>) {
|
|
13
17
|
const plugins: any[] = []
|
|
@@ -16,7 +20,8 @@ export function getVitePlugins(cfg?: Partial<Theme.BlogConfig>) {
|
|
|
16
20
|
const buildEndFn: any[] = []
|
|
17
21
|
// 执行自定义的 buildEnd 钩子
|
|
18
22
|
plugins.push(inlineBuildEndPlugin(buildEndFn))
|
|
19
|
-
|
|
23
|
+
// 处理cover image的路径(暂只支持自动识别的文章首图)
|
|
24
|
+
plugins.push(coverImgTransform())
|
|
20
25
|
// 内置简化版的pagefind
|
|
21
26
|
if (cfg && cfg.search !== false) {
|
|
22
27
|
const ops = cfg.search instanceof Object ? cfg.search : {}
|
|
@@ -118,3 +123,44 @@ export function inlineBuildEndPlugin(buildEndFn: any[]) {
|
|
|
118
123
|
}
|
|
119
124
|
}
|
|
120
125
|
}
|
|
126
|
+
|
|
127
|
+
// TODO: 支持frontmatter中的相对路径图片自动处理
|
|
128
|
+
export function coverImgTransform() {
|
|
129
|
+
let blogConfig: Theme.BlogConfig
|
|
130
|
+
let vitepressConfig: SiteConfig
|
|
131
|
+
let assetsDir: string
|
|
132
|
+
return {
|
|
133
|
+
name: '@sugarat/theme-plugin-cover-transform',
|
|
134
|
+
apply: 'build',
|
|
135
|
+
enforce: 'pre',
|
|
136
|
+
configResolved(config: any) {
|
|
137
|
+
vitepressConfig = config.vitepress
|
|
138
|
+
assetsDir = vitepressConfig.assetsDir
|
|
139
|
+
blogConfig = config.vitepress.site.themeConfig.blog
|
|
140
|
+
},
|
|
141
|
+
async generateBundle(_: any, bundle: Record<string, any>) {
|
|
142
|
+
const assetsMap = Object.entries(bundle).filter(([key]) => {
|
|
143
|
+
return key.startsWith(assetsDir)
|
|
144
|
+
}).map(([_, value]) => {
|
|
145
|
+
return value
|
|
146
|
+
})
|
|
147
|
+
for (const page of blogConfig.pagesData) {
|
|
148
|
+
const { cover } = page.meta
|
|
149
|
+
// 是否相对路径引用
|
|
150
|
+
if (!cover?.startsWith('/')) {
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
// 寻找构建后的
|
|
155
|
+
const realPath = path.join(vitepressConfig.root, cover)
|
|
156
|
+
const fileBuffer = readFileSync(realPath)
|
|
157
|
+
const matchAsset = assetsMap.find(v => Buffer.compare(fileBuffer, v.source))
|
|
158
|
+
page.meta.cover = joinPath('/', matchAsset.fileName)
|
|
159
|
+
}
|
|
160
|
+
catch (e: any) {
|
|
161
|
+
vitepressConfig.logger.warn(e?.message)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|