@sugarat/theme 0.1.35 → 0.1.36-beta.0

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
@@ -110,7 +110,8 @@ declare namespace Theme {
110
110
  interface HomeBlog {
111
111
  name?: string;
112
112
  motto?: string;
113
- inspiring?: string;
113
+ inspiring?: string | string[];
114
+ inspiringTimeout?: number;
114
115
  pageSize?: number;
115
116
  }
116
117
  interface ArticleConfig {
@@ -248,6 +249,10 @@ declare namespace Theme {
248
249
  blog?: BlogConfig;
249
250
  }
250
251
  interface HomeConfig {
252
+ /**
253
+ * @deprecated
254
+ * 此方法已经废弃,这个定义将在未来某一刻被移除,请为 inspiring 配置数租来实现相同的效果
255
+ */
251
256
  handleChangeSlogan?: (oldSlogan: string) => string | Promise<string>;
252
257
  }
253
258
  }
@@ -257,6 +262,7 @@ declare function getDefaultTitle(content: string): string;
257
262
  declare function clearMatterContent(content: string): string;
258
263
  declare function getFileBirthTime(url: string): string;
259
264
  declare function getGitTimestamp(file: string): Promise<unknown>;
260
- declare function defineConfig(config: UserConfig<Theme.Config>): UserConfig<any>;
265
+ declare function assignMermaid(config: any): void;
266
+ declare function defineConfig(config: UserConfig<Theme.Config>): any;
261
267
 
262
- export { clearMatterContent, defineConfig, getDefaultTitle, getFileBirthTime, getGitTimestamp, getThemeConfig };
268
+ export { assignMermaid, clearMatterContent, defineConfig, getDefaultTitle, getFileBirthTime, getGitTimestamp, getThemeConfig };
package/node.js CHANGED
@@ -26,6 +26,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
26
26
  // src/node.ts
27
27
  var node_exports = {};
28
28
  __export(node_exports, {
29
+ assignMermaid: () => assignMermaid,
29
30
  clearMatterContent: () => clearMatterContent,
30
31
  defineConfig: () => defineConfig,
31
32
  getDefaultTitle: () => getDefaultTitle,
@@ -346,23 +347,22 @@ function getThemeConfig(cfg) {
346
347
  ]
347
348
  };
348
349
  }
350
+ const markdownPlugin = [];
349
351
  if (cfg?.tabs) {
350
- extraConfig.markdown = {
351
- config(md) {
352
- tabsPlugin(md);
353
- }
354
- };
352
+ markdownPlugin.push(tabsPlugin);
355
353
  }
356
354
  if (cfg) {
357
355
  cfg.mermaid = cfg?.mermaid ?? false;
358
356
  }
359
357
  if (cfg?.mermaid !== false) {
360
- extraConfig.vite = {
361
- ...extraConfig.vite,
362
- resolve: {
363
- alias: {
364
- mermaid: "mermaid/dist/mermaid.esm.mjs"
365
- }
358
+ markdownPlugin.push(import_vitepress_plugin_mermaid.MermaidMarkdown);
359
+ }
360
+ if (markdownPlugin.length) {
361
+ extraConfig.markdown = {
362
+ config(...rest) {
363
+ markdownPlugin.forEach((plugin) => {
364
+ plugin?.(...rest);
365
+ });
366
366
  }
367
367
  };
368
368
  }
@@ -437,6 +437,39 @@ function getGitTimestamp(file) {
437
437
  function getTextSummary(text, count = 100) {
438
438
  return clearMatterContent(text).match(/^# ([\s\S]+)/m)?.[1]?.replace(/#/g, "")?.replace(/!\[.*?\]\(.*?\)/g, "")?.replace(/\[(.*?)\]\(.*?\)/g, "$1")?.replace(/\*\*(.*?)\*\*/g, "$1")?.split("\n")?.filter((v) => !!v)?.slice(1)?.join("\n")?.replace(/>(.*)/, "")?.slice(0, count);
439
439
  }
440
+ function assignMermaid(config) {
441
+ if (!config.mermaid)
442
+ return;
443
+ if (!config.vite)
444
+ config.vite = {};
445
+ if (!config.vite.plugins)
446
+ config.vite.plugins = [];
447
+ config.vite.plugins.push((0, import_vitepress_plugin_mermaid.MermaidPlugin)(config.mermaid));
448
+ if (!config.vite.optimizeDeps)
449
+ config.vite.optimizeDeps = {};
450
+ config.vite.optimizeDeps = {
451
+ ...config.vite.optimizeDeps,
452
+ include: [
453
+ "@braintree/sanitize-url",
454
+ "dayjs",
455
+ "debug",
456
+ "cytoscape-cose-bilkent",
457
+ "cytoscape"
458
+ ]
459
+ };
460
+ if (!config.vite.resolve)
461
+ config.vite.resolve = {};
462
+ if (!config.vite.resolve.alias)
463
+ config.vite.resolve.alias = {};
464
+ config.vite.resolve.alias = {
465
+ ...config.vite.resolve.alias,
466
+ "dayjs/plugin/advancedFormat.js": "dayjs/esm/plugin/advancedFormat",
467
+ "dayjs/plugin/customParseFormat.js": "dayjs/esm/plugin/customParseFormat",
468
+ "dayjs/plugin/isoWeek.js": "dayjs/esm/plugin/isoWeek",
469
+ "cytoscape/dist/cytoscape.umd.js": "cytoscape/dist/cytoscape.esm.js",
470
+ mermaid: "mermaid/dist/mermaid.esm.mjs"
471
+ };
472
+ }
440
473
  function defineConfig(config) {
441
474
  if (config.themeConfig?.themeConfig) {
442
475
  config.extends = checkKeys.reduce((pre, key) => {
@@ -450,10 +483,11 @@ function defineConfig(config) {
450
483
  }, 1200);
451
484
  }
452
485
  const extendThemeConfig = config.extends?.themeConfig?.blog;
453
- const resultConfig = extendThemeConfig.mermaid === false ? config : (0, import_vitepress_plugin_mermaid.withMermaid)({
486
+ const resultConfig = extendThemeConfig.mermaid === false ? config : {
454
487
  ...config,
455
488
  mermaid: extendThemeConfig.mermaid === true ? {} : extendThemeConfig.mermaid
456
- });
489
+ };
490
+ assignMermaid(resultConfig);
457
491
  if (!resultConfig.markdown)
458
492
  resultConfig.markdown = {};
459
493
  if (config.extends?.markdown?.config) {
@@ -468,6 +502,7 @@ function defineConfig(config) {
468
502
  }
469
503
  // Annotate the CommonJS export names for ESM import in node:
470
504
  0 && (module.exports = {
505
+ assignMermaid,
471
506
  clearMatterContent,
472
507
  defineConfig,
473
508
  getDefaultTitle,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sugarat/theme",
3
- "version": "0.1.35",
3
+ "version": "0.1.36-beta.0",
4
4
  "description": "简约风的 Vitepress 博客主题,sugarat vitepress blog theme",
5
5
  "main": "src/index.ts",
6
6
  "exports": {
@@ -10,9 +10,9 @@
10
10
  </div>
11
11
  </template>
12
12
  <script setup lang="ts">
13
- import { computed, ref } from 'vue'
13
+ import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
14
14
  import { useData } from 'vitepress'
15
- import { useHomeConfig, useBlogConfig } from '../composables/config/blog'
15
+ import { useBlogConfig } from '../composables/config/blog'
16
16
 
17
17
  const { site, frontmatter } = useData()
18
18
  const { home } = useBlogConfig()
@@ -21,34 +21,61 @@ const name = computed(
21
21
  () => (frontmatter.value.blog?.name ?? site.value.title) || home?.name || ''
22
22
  )
23
23
  const motto = computed(() => frontmatter.value.blog?.motto || home?.motto || '')
24
- const initInspiring = ref<string>(
25
- frontmatter.value.blog?.inspiring || home?.inspiring || ''
24
+
25
+ const inspiring = ref('')
26
+ const inspiringList = computed<string[]>(() => {
27
+ return [
28
+ ...new Set(
29
+ [frontmatter.value.blog?.inspiring, home?.inspiring]
30
+ .flat()
31
+ .filter((v) => !!v)
32
+ )
33
+ ]
34
+ })
35
+ const inspiringIndex = ref<number>(-1)
36
+ const inspiringTimeout = computed<number>(
37
+ () => frontmatter.value.blog?.inspiringTimeout || home?.inspiringTimeout || 0
26
38
  )
27
- const inspiring = computed({
28
- get() {
29
- return initInspiring.value
30
- },
31
- set(newValue) {
32
- initInspiring.value = newValue
39
+
40
+ watch(inspiringTimeout, () => {
41
+ startTimer()
42
+ })
43
+ const timer = ref<any>(0)
44
+ const startTimer = () => {
45
+ if (timer.value) {
46
+ clearTimeout(timer.value)
47
+ }
48
+ if (inspiringTimeout.value > 0) {
49
+ timer.value = setTimeout(() => {
50
+ changeSlogan()
51
+ }, inspiringTimeout.value)
33
52
  }
53
+ }
54
+ onMounted(() => {
55
+ changeSlogan()
34
56
  })
35
57
 
36
- const homeConfig = useHomeConfig()
58
+ onUnmounted(() => {
59
+ if (timer.value) {
60
+ clearTimeout(timer.value)
61
+ }
62
+ })
37
63
 
38
64
  const changeSlogan = async () => {
39
- if (typeof homeConfig?.handleChangeSlogan !== 'function') {
40
- return
41
- }
42
- const newSlogan = await homeConfig.handleChangeSlogan(inspiring.value)
43
- if (typeof newSlogan !== 'string' || !newSlogan.trim()) {
44
- return
45
- }
65
+ // 顺手启动定时器
66
+ startTimer()
67
+
68
+ if (inspiringList.value.length < 1) return
69
+
70
+ inspiringIndex.value = (inspiringIndex.value + 1) % inspiringList.value.length
71
+ const newValue = inspiringList.value[inspiringIndex.value]
72
+ if (newValue === inspiring.value) return
46
73
 
47
74
  // 重新渲染数据,同时触发动画
48
75
  inspiring.value = ''
49
- setTimeout(async () => {
50
- inspiring.value = newSlogan
51
- })
76
+ setTimeout(() => {
77
+ inspiring.value = newValue
78
+ }, 100)
52
79
  }
53
80
  </script>
54
81
  <style lang="scss" scoped>
@@ -116,7 +116,8 @@ export namespace Theme {
116
116
  export interface HomeBlog {
117
117
  name?: string
118
118
  motto?: string
119
- inspiring?: string
119
+ inspiring?: string | string[]
120
+ inspiringTimeout?: number
120
121
  pageSize?: number
121
122
  }
122
123
 
@@ -269,6 +270,10 @@ export namespace Theme {
269
270
  blog?: BlogConfig
270
271
  }
271
272
  export interface HomeConfig {
273
+ /**
274
+ * @deprecated
275
+ * 此方法已经废弃,这个定义将在未来某一刻被移除,请为 inspiring 配置数租来实现相同的效果
276
+ */
272
277
  handleChangeSlogan?: (oldSlogan: string) => string | Promise<string>
273
278
  }
274
279
  }
package/src/node.ts CHANGED
@@ -6,7 +6,7 @@ import { execSync, spawn, spawnSync } from 'child_process'
6
6
  import path from 'path'
7
7
  import type { SiteConfig, UserConfig } from 'vitepress'
8
8
  import { tabsMarkdownPlugin } from 'vitepress-plugin-tabs'
9
- import { withMermaid } from 'vitepress-plugin-mermaid'
9
+ import { MermaidMarkdown, MermaidPlugin } from 'vitepress-plugin-mermaid'
10
10
  import { formatDate } from './utils/index'
11
11
  import type { Theme } from './composables/config/index'
12
12
 
@@ -170,25 +170,35 @@ export function getThemeConfig(cfg?: Partial<Theme.BlogConfig>) {
170
170
  ]
171
171
  }
172
172
  }
173
+ const markdownPlugin: any[] = []
174
+ // tabs支持
173
175
  if (cfg?.tabs) {
174
- extraConfig.markdown = {
175
- config(md: any) {
176
- tabsMarkdownPlugin(md)
177
- }
178
- }
176
+ markdownPlugin.push(tabsMarkdownPlugin)
179
177
  }
178
+
179
+ // 流程图支持
180
180
  if (cfg) {
181
181
  cfg.mermaid = cfg?.mermaid ?? false
182
182
  }
183
-
184
- // 流程图支持
185
183
  if (cfg?.mermaid !== false) {
186
- extraConfig.vite = {
187
- ...extraConfig.vite,
188
- resolve: {
189
- alias: {
190
- mermaid: 'mermaid/dist/mermaid.esm.mjs'
191
- }
184
+ markdownPlugin.push(MermaidMarkdown)
185
+ // extraConfig.vite = {
186
+ // ...extraConfig.vite,
187
+ // resolve: {
188
+ // alias: {
189
+ // mermaid: 'mermaid/dist/mermaid.esm.mjs'
190
+ // }
191
+ // }
192
+ // }
193
+ }
194
+
195
+ // 注册markdown插件
196
+ if (markdownPlugin.length) {
197
+ extraConfig.markdown = {
198
+ config(...rest: any[]) {
199
+ markdownPlugin.forEach((plugin) => {
200
+ plugin?.(...rest)
201
+ })
192
202
  }
193
203
  }
194
204
  }
@@ -306,7 +316,37 @@ function getTextSummary(text: string, count = 100) {
306
316
  )
307
317
  }
308
318
 
309
- export function defineConfig(config: UserConfig<Theme.Config>) {
319
+ export function assignMermaid(config: any) {
320
+ if (!config.mermaid) return
321
+
322
+ if (!config.vite) config.vite = {}
323
+ if (!config.vite.plugins) config.vite.plugins = []
324
+ config.vite.plugins.push(MermaidPlugin(config.mermaid))
325
+ if (!config.vite.optimizeDeps) config.vite.optimizeDeps = {}
326
+ config.vite.optimizeDeps = {
327
+ ...config.vite.optimizeDeps,
328
+ include: [
329
+ '@braintree/sanitize-url',
330
+ 'dayjs',
331
+ 'debug',
332
+ 'cytoscape-cose-bilkent',
333
+ 'cytoscape'
334
+ ]
335
+ }
336
+ if (!config.vite.resolve) config.vite.resolve = {}
337
+ if (!config.vite.resolve.alias) config.vite.resolve.alias = {}
338
+
339
+ config.vite.resolve.alias = {
340
+ ...config.vite.resolve.alias,
341
+ 'dayjs/plugin/advancedFormat.js': 'dayjs/esm/plugin/advancedFormat',
342
+ 'dayjs/plugin/customParseFormat.js': 'dayjs/esm/plugin/customParseFormat',
343
+ 'dayjs/plugin/isoWeek.js': 'dayjs/esm/plugin/isoWeek',
344
+ 'cytoscape/dist/cytoscape.umd.js': 'cytoscape/dist/cytoscape.esm.js',
345
+ mermaid: 'mermaid/dist/mermaid.esm.mjs'
346
+ }
347
+ }
348
+
349
+ export function defineConfig(config: UserConfig<Theme.Config>): any {
310
350
  // 兼容低版本主题配置
311
351
  // @ts-ignore
312
352
  if (config.themeConfig?.themeConfig) {
@@ -332,11 +372,12 @@ export function defineConfig(config: UserConfig<Theme.Config>) {
332
372
  const resultConfig =
333
373
  extendThemeConfig.mermaid === false
334
374
  ? config
335
- : withMermaid({
375
+ : {
336
376
  ...config,
337
377
  mermaid:
338
378
  extendThemeConfig.mermaid === true ? {} : extendThemeConfig.mermaid
339
- })
379
+ }
380
+ assignMermaid(resultConfig)
340
381
 
341
382
  // 处理markdown插件
342
383
  if (!resultConfig.markdown) resultConfig.markdown = {}