@sugarat/theme 0.4.4 → 0.4.6

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.d.ts CHANGED
@@ -338,7 +338,7 @@ declare namespace Theme {
338
338
  /**
339
339
  * 内置一些主题色
340
340
  * @default 'vp-default'
341
- * 也可以自定义颜色,详见 TODO:文档
341
+ * 也可以自定义颜色,详见 https://theme.sugarat.top/config/style.html#%E4%B8%BB%E9%A2%98%E8%89%B2
342
342
  */
343
343
  themeColor?: ThemeColor;
344
344
  pagesData: PageData[];
package/node.js CHANGED
@@ -518,6 +518,9 @@ function themeReloadPlugin() {
518
518
  function getVitePlugins(cfg = {}) {
519
519
  const plugins = [];
520
520
  plugins.push(coverImgTransform());
521
+ if (cfg.themeColor) {
522
+ plugins.push(setThemeScript(cfg.themeColor));
523
+ }
521
524
  plugins.push(themeReloadPlugin());
522
525
  plugins.push(providePageData(cfg));
523
526
  if (cfg && cfg.search !== false) {
@@ -606,6 +609,33 @@ function providePageData(cfg) {
606
609
  }
607
610
  };
608
611
  }
612
+ function setThemeScript(themeColor) {
613
+ let resolveConfig;
614
+ const pluginOps = {
615
+ name: "@sugarat/theme-plugin-theme-color-script",
616
+ enforce: "pre",
617
+ configResolved(config) {
618
+ if (resolveConfig) {
619
+ return;
620
+ }
621
+ resolveConfig = config;
622
+ const vitepressConfig = config.vitepress;
623
+ if (!vitepressConfig) {
624
+ return;
625
+ }
626
+ const selfTransformHead = vitepressConfig.transformHead;
627
+ vitepressConfig.transformHead = async (ctx) => {
628
+ const selfHead = await Promise.resolve(selfTransformHead?.(ctx)) || [];
629
+ return selfHead.concat([
630
+ ["script", { type: "text/javascript" }, `;(function() {
631
+ document.documentElement.setAttribute("theme", "${themeColor}");
632
+ })()`]
633
+ ]);
634
+ };
635
+ }
636
+ };
637
+ return pluginOps;
638
+ }
609
639
 
610
640
  // src/node.ts
611
641
  function getThemeConfig(cfg = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sugarat/theme",
3
- "version": "0.4.4",
3
+ "version": "0.4.6",
4
4
  "description": "简约风的 Vitepress 博客主题,sugarat vitepress blog theme",
5
5
  "author": "sugar",
6
6
  "license": "MIT",
@@ -50,9 +50,9 @@
50
50
  "vitepress-markdown-timeline": "^1.2.1",
51
51
  "vitepress-plugin-mermaid": "2.0.13",
52
52
  "vitepress-plugin-tabs": "0.2.0",
53
- "vitepress-plugin-rss": "0.2.7",
54
- "vitepress-plugin-pagefind": "0.4.2",
55
- "@sugarat/theme-shared": "0.0.1"
53
+ "vitepress-plugin-pagefind": "0.4.3",
54
+ "vitepress-plugin-rss": "0.2.8",
55
+ "@sugarat/theme-shared": "0.0.2"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@element-plus/icons-vue": "^2.3.1",
@@ -371,7 +371,7 @@ export namespace Theme {
371
371
  /**
372
372
  * 内置一些主题色
373
373
  * @default 'vp-default'
374
- * 也可以自定义颜色,详见 TODO:文档
374
+ * 也可以自定义颜色,详见 https://theme.sugarat.top/config/style.html#%E4%B8%BB%E9%A2%98%E8%89%B2
375
375
  */
376
376
  themeColor?: ThemeColor
377
377
  pagesData: PageData[]
@@ -1,7 +1,7 @@
1
1
  import path from 'node:path'
2
2
  import { existsSync, readFileSync } from 'node:fs'
3
3
  import { Buffer } from 'node:buffer'
4
- import type { SiteConfig } from 'vitepress'
4
+ import type { HeadConfig, SiteConfig } from 'vitepress'
5
5
  import {
6
6
  pagefindPlugin
7
7
  } from 'vitepress-plugin-pagefind'
@@ -16,13 +16,13 @@ import { getArticles } from './theme'
16
16
  export function getVitePlugins(cfg: Partial<Theme.BlogConfig> = {}) {
17
17
  const plugins: any[] = []
18
18
 
19
- // const buildEndFn: any[] = []
20
- // Build完后运行的一系列列方法,执行自定义的 buildEnd 钩子
21
- // plugins.push(inlineBuildEndPlugin(buildEndFn))
22
-
23
19
  // 处理cover image的路径(暂只支持自动识别的文章首图)
24
20
  plugins.push(coverImgTransform())
25
21
 
22
+ // 处理自定义主题色
23
+ if (cfg.themeColor) {
24
+ plugins.push(setThemeScript(cfg.themeColor))
25
+ }
26
26
  // 自动重载首页
27
27
  plugins.push(themeReloadPlugin())
28
28
 
@@ -156,3 +156,35 @@ export function providePageData(cfg: Partial<Theme.BlogConfig>) {
156
156
  },
157
157
  } as PluginOption
158
158
  }
159
+
160
+ export function setThemeScript(
161
+ themeColor: Theme.ThemeColor
162
+ ) {
163
+ let resolveConfig: any
164
+ const pluginOps: PluginOption = {
165
+ name: '@sugarat/theme-plugin-theme-color-script',
166
+ enforce: 'pre',
167
+ configResolved(config: any) {
168
+ if (resolveConfig) {
169
+ return
170
+ }
171
+ resolveConfig = config
172
+
173
+ const vitepressConfig: SiteConfig = config.vitepress
174
+ if (!vitepressConfig) {
175
+ return
176
+ }
177
+ // 通过 head 添加额外的脚本注入
178
+ const selfTransformHead = vitepressConfig.transformHead
179
+ vitepressConfig.transformHead = async (ctx) => {
180
+ const selfHead = (await Promise.resolve(selfTransformHead?.(ctx))) || []
181
+ return selfHead.concat([
182
+ ['script', { type: 'text/javascript' }, `;(function() {
183
+ document.documentElement.setAttribute("theme", "${themeColor}");
184
+ })()`]
185
+ ] as HeadConfig[])
186
+ }
187
+ }
188
+ }
189
+ return pluginOps
190
+ }