@sugarat/theme 0.1.12 → 0.1.14

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
@@ -155,34 +155,14 @@ declare namespace Theme {
155
155
  friend?: FriendLink[];
156
156
  }
157
157
  interface Config extends DefaultTheme.Config {
158
- blog: BlogConfig;
158
+ blog?: BlogConfig;
159
159
  }
160
160
  interface HomeConfig {
161
161
  handleChangeSlogan?: (oldSlogan: string) => string | Promise<string>;
162
162
  }
163
163
  }
164
164
 
165
- declare function getThemeConfig(cfg?: Partial<Theme.BlogConfig>): {
166
- blog: {
167
- blog?: false | undefined;
168
- pagesData: Theme.PageData[];
169
- srcDir?: string | undefined;
170
- author?: string | undefined;
171
- hotArticle?: Theme.HotArticle | undefined;
172
- home?: Theme.HomeBlog | undefined;
173
- search?: boolean | "pagefind" | undefined;
174
- comment?: false | Theme.GiscusConfig | undefined;
175
- recommend?: Theme.RecommendArticle | undefined;
176
- article?: Theme.ArticleConfig | undefined;
177
- alert?: Theme.Alert | undefined;
178
- popover?: Theme.Popover | undefined;
179
- friend?: Theme.FriendLink[] | undefined;
180
- };
181
- sidebar: {
182
- text: string;
183
- items: never[];
184
- }[];
185
- };
165
+ declare function getThemeConfig(cfg?: Partial<Theme.BlogConfig>): any;
186
166
  declare function getDefaultTitle(content: string): string;
187
167
  declare function clearMatterContent(content: string): string;
188
168
  declare function getFileBirthTime(url: string): string;
package/node.js CHANGED
@@ -71,6 +71,7 @@ function formatDate(d, fmt = "yyyy-MM-dd hh:mm:ss") {
71
71
  }
72
72
 
73
73
  // src/node.ts
74
+ var checkKeys = ["themeConfig"];
74
75
  function getThemeConfig(cfg) {
75
76
  const srcDir = cfg?.srcDir || process.argv.slice(2)?.[1] || ".";
76
77
  const files = import_fast_glob.default.sync(`${srcDir}/**/*.md`, { ignore: ["node_modules"] });
@@ -116,17 +117,76 @@ function getThemeConfig(cfg) {
116
117
  meta
117
118
  };
118
119
  }).filter((v) => v.meta.layout !== "home");
120
+ const extraConfig = {};
121
+ if (cfg?.search === "pagefind") {
122
+ checkKeys.push("head", "vite");
123
+ extraConfig.head = [
124
+ [
125
+ "script",
126
+ {},
127
+ `import('/_pagefind/pagefind.js')
128
+ .then((module) => {
129
+ window.__pagefind__ = module
130
+ })
131
+ .catch(() => {
132
+ console.log('not load /_pagefind/pagefind.js')
133
+ })`
134
+ ]
135
+ ];
136
+ let flag = true;
137
+ let originLog = null;
138
+ extraConfig.vite = {
139
+ plugins: [
140
+ {
141
+ name: "@sugarar/theme-plugin-pagefind",
142
+ buildEnd() {
143
+ const { log } = console;
144
+ if (flag) {
145
+ flag = false;
146
+ originLog = log;
147
+ Object.defineProperty(console, "log", {
148
+ value() {
149
+ if (`${arguments[0]}`.includes("build complete")) {
150
+ console.log = originLog;
151
+ setTimeout(() => {
152
+ originLog();
153
+ originLog("=== pagefind: https://pagefind.app/ ===");
154
+ const command = `npx pagefind --source ${import_path.default.join(
155
+ process.argv.slice(2)?.[1] || ".",
156
+ ".vitepress/dist"
157
+ )}`;
158
+ originLog(command);
159
+ originLog();
160
+ (0, import_child_process.execSync)(command, {
161
+ stdio: "inherit"
162
+ });
163
+ }, 100);
164
+ }
165
+ return log.apply(this, arguments);
166
+ }
167
+ });
168
+ }
169
+ }
170
+ }
171
+ ]
172
+ };
173
+ }
119
174
  return {
120
- blog: {
121
- pagesData: data,
122
- ...cfg
175
+ themeConfig: {
176
+ blog: {
177
+ pagesData: data,
178
+ ...cfg
179
+ },
180
+ ...cfg?.blog !== false ? {
181
+ sidebar: [
182
+ {
183
+ text: "",
184
+ items: []
185
+ }
186
+ ]
187
+ } : void 0
123
188
  },
124
- sidebar: [
125
- {
126
- text: "",
127
- items: []
128
- }
129
- ]
189
+ ...extraConfig
130
190
  };
131
191
  }
132
192
  function getDefaultTitle(content) {
@@ -183,59 +243,16 @@ function getTextSummary(text, count = 100) {
183
243
  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);
184
244
  }
185
245
  function defineConfig(config) {
186
- if (config?.themeConfig?.blog?.search === "pagefind") {
187
- config.head = (config.head || []).concat([
188
- [
189
- "script",
190
- {},
191
- `import('/_pagefind/pagefind.js')
192
- .then((module) => {
193
- window.__pagefind__ = module
194
- })
195
- .catch(() => {
196
- console.log('not load /_pagefind/pagefind.js')
197
- })`
198
- ]
199
- ]);
200
- let flag = true;
201
- let originLog = null;
202
- config.vite = {
203
- ...config.vite,
204
- plugins: [
205
- ...config.vite?.plugins || [],
206
- {
207
- name: "@sugarar/theme-plugin-pagefind",
208
- buildEnd() {
209
- const { log } = console;
210
- if (flag) {
211
- flag = false;
212
- originLog = log;
213
- Object.defineProperty(console, "log", {
214
- value() {
215
- if (`${arguments[0]}`.includes("build complete")) {
216
- console.log = originLog;
217
- setTimeout(() => {
218
- originLog();
219
- originLog("=== pagefind: https://pagefind.app/ ===");
220
- const command = `npx pagefind --source ${import_path.default.join(
221
- process.argv.slice(2)?.[1] || ".",
222
- ".vitepress/dist"
223
- )}`;
224
- originLog(command);
225
- originLog();
226
- (0, import_child_process.execSync)(command, {
227
- stdio: "inherit"
228
- });
229
- }, 100);
230
- }
231
- return log.apply(this, arguments);
232
- }
233
- });
234
- }
235
- }
236
- }
237
- ]
238
- };
246
+ if (config.themeConfig?.themeConfig) {
247
+ config.extends = checkKeys.reduce((pre, key) => {
248
+ pre[key] = config.themeConfig[key];
249
+ delete config.themeConfig[key];
250
+ return pre;
251
+ }, {});
252
+ setTimeout(() => {
253
+ console.warn("==\u2193 \u4E3B\u9898\u914D\u7F6E\u65B9\u5F0F\u8FC7\u671F\uFF0C\u8BF7\u5C3D\u5FEB\u53C2\u7167\u6587\u6863\u66F4\u65B0 \u2193==");
254
+ console.warn("https://theme.sugarat.top/config/global.html");
255
+ }, 1200);
239
256
  }
240
257
  return config;
241
258
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sugarat/theme",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "简约风的 Vitepress 博客主题,sugarat vitepress blog theme",
5
5
  "main": "src/index.ts",
6
6
  "exports": {
@@ -24,7 +24,7 @@
24
24
  <!-- 简介 -->
25
25
  <div class="des">
26
26
  <!-- title -->
27
- <el-link type="info" class="title" :href="v.route">{{
27
+ <el-link type="info" class="title" :href="withBase(v.route)">{{
28
28
  v.meta.title
29
29
  }}</el-link>
30
30
  <!-- 描述信息 -->
@@ -42,6 +42,7 @@
42
42
  <script lang="ts" setup>
43
43
  import { ref, computed } from 'vue'
44
44
  import { ElButton, ElLink } from 'element-plus'
45
+ import { withBase } from 'vitepress'
45
46
  import { useArticles, useBlogConfig } from '../composables/config/blog'
46
47
  import { formatShowDate } from '../utils/index'
47
48
 
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <a class="blog-item" :href="route">
2
+ <a class="blog-item" :href="withBase(route)">
3
3
  <i class="pin" v-if="!!pin"></i>
4
4
  <!-- 左侧信息 -->
5
5
  <div class="info-part">
@@ -22,6 +22,7 @@
22
22
  </template>
23
23
 
24
24
  <script lang="ts" setup>
25
+ import { withBase } from 'vitepress'
25
26
  import { computed } from 'vue'
26
27
  import { formatShowDate } from '../utils/index'
27
28
 
@@ -42,7 +42,7 @@
42
42
  <script lang="ts" setup>
43
43
  import { ref, computed } from 'vue'
44
44
  import { ElButton, ElLink } from 'element-plus'
45
- import { useRoute } from 'vitepress'
45
+ import { useRoute, withBase } from 'vitepress'
46
46
  import { formatShowDate } from '../utils/index'
47
47
  import { useArticles, useBlogConfig } from '../composables/config/blog'
48
48
 
@@ -59,6 +59,7 @@ const route = useRoute()
59
59
  const recommendList = computed(() => {
60
60
  const paths = route.path.split('/')
61
61
  const origin = docs.value
62
+ .map((v) => ({ ...v, route: withBase(v.route) }))
62
63
  // 过滤出公共路由前缀
63
64
  // 限制为同路由前缀
64
65
  .filter(
@@ -33,7 +33,7 @@
33
33
  >
34
34
  <Command.Item
35
35
  v-for="item in showSearchResult"
36
- :data-value="item.route"
36
+ :data-value="withBase(item.route)"
37
37
  :key="item.route"
38
38
  @select="handleSelect"
39
39
  >
@@ -131,7 +131,7 @@
131
131
  import { computed, nextTick, ref, watch } from 'vue'
132
132
  // @ts-ignore
133
133
  import { Command } from 'vue-command-palette'
134
- import { useRoute, useRouter } from 'vitepress'
134
+ import { useRoute, useRouter, withBase } from 'vitepress'
135
135
  import { useMagicKeys } from '@vueuse/core'
136
136
  import { formatDate } from '../utils'
137
137
  import { useArticles, useBlogConfig } from '../composables/config/blog'
@@ -55,7 +55,7 @@ export function useBlogConfig() {
55
55
  }
56
56
 
57
57
  export function useBlogThemeMode() {
58
- return inject(configSymbol)!.value.blog.blog ?? true
58
+ return inject(configSymbol)!.value?.blog?.blog ?? true
59
59
  }
60
60
  export function useHomeConfig() {
61
61
  return inject(homeConfigSymbol)!
@@ -63,7 +63,7 @@ export function useHomeConfig() {
63
63
 
64
64
  export function useGiscusConfig() {
65
65
  const blogConfig = useConfig()
66
- return blogConfig.config.blog.comment
66
+ return blogConfig.config?.blog?.comment
67
67
  }
68
68
 
69
69
  export function useArticles() {
@@ -80,9 +80,9 @@ export function useCurrentArticle() {
80
80
  const blogConfig = useConfig()
81
81
  const route = useRoute()
82
82
 
83
- const docs = computed(() => blogConfig.config.blog.pagesData)
83
+ const docs = computed(() => blogConfig.config?.blog?.pagesData)
84
84
  const currentArticle = computed(() =>
85
- docs.value.find((v) => v.route === route.path.replace(/.html$/, ''))
85
+ docs.value?.find((v) => v.route === route.path.replace(/.html$/, ''))
86
86
  )
87
87
 
88
88
  return currentArticle
@@ -169,7 +169,7 @@ export namespace Theme {
169
169
  }
170
170
 
171
171
  export interface Config extends DefaultTheme.Config {
172
- blog: BlogConfig
172
+ blog?: BlogConfig
173
173
  }
174
174
  export interface HomeConfig {
175
175
  handleChangeSlogan?: (oldSlogan: string) => string | Promise<string>
package/src/node.ts CHANGED
@@ -8,6 +8,8 @@ import type { UserConfig } from 'vitepress'
8
8
  import { formatDate } from './utils/index'
9
9
  import type { Theme } from './composables/config/index'
10
10
 
11
+ const checkKeys = ['themeConfig']
12
+
11
13
  export function getThemeConfig(cfg?: Partial<Theme.BlogConfig>) {
12
14
  const srcDir = cfg?.srcDir || process.argv.slice(2)?.[1] || '.'
13
15
  const files = glob.sync(`${srcDir}/**/*.md`, { ignore: ['node_modules'] })
@@ -82,17 +84,81 @@ export function getThemeConfig(cfg?: Partial<Theme.BlogConfig>) {
82
84
  })
83
85
  .filter((v) => v.meta.layout !== 'home')
84
86
 
87
+ const extraConfig: any = {}
88
+
89
+ if (cfg?.search === 'pagefind') {
90
+ checkKeys.push('head', 'vite')
91
+ extraConfig.head = [
92
+ [
93
+ 'script',
94
+ {},
95
+ `import('/_pagefind/pagefind.js')
96
+ .then((module) => {
97
+ window.__pagefind__ = module
98
+ })
99
+ .catch(() => {
100
+ console.log('not load /_pagefind/pagefind.js')
101
+ })`
102
+ ]
103
+ ]
104
+ let flag = true
105
+ let originLog: any = null
106
+ extraConfig.vite = {
107
+ plugins: [
108
+ {
109
+ name: '@sugarar/theme-plugin-pagefind',
110
+ buildEnd() {
111
+ const { log } = console
112
+ // TODO: hack
113
+ if (flag) {
114
+ flag = false
115
+ originLog = log
116
+ Object.defineProperty(console, 'log', {
117
+ value() {
118
+ if (`${arguments[0]}`.includes('build complete')) {
119
+ console.log = originLog
120
+ setTimeout(() => {
121
+ originLog()
122
+ originLog('=== pagefind: https://pagefind.app/ ===')
123
+ const command = `npx pagefind --source ${path.join(
124
+ process.argv.slice(2)?.[1] || '.',
125
+ '.vitepress/dist'
126
+ )}`
127
+ originLog(command)
128
+ originLog()
129
+ execSync(command, {
130
+ stdio: 'inherit'
131
+ })
132
+ }, 100)
133
+ }
134
+ // @ts-ignore
135
+ return log.apply(this, arguments)
136
+ }
137
+ })
138
+ }
139
+ }
140
+ }
141
+ ]
142
+ }
143
+ }
85
144
  return {
86
- blog: {
87
- pagesData: data as Theme.PageData[],
88
- ...cfg
145
+ themeConfig: {
146
+ blog: {
147
+ pagesData: data as Theme.PageData[],
148
+ ...cfg
149
+ },
150
+ ...(cfg?.blog !== false
151
+ ? {
152
+ sidebar: [
153
+ {
154
+ text: '',
155
+ items: []
156
+ }
157
+ ]
158
+ }
159
+ : undefined)
89
160
  },
90
- sidebar: [
91
- {
92
- text: '',
93
- items: []
94
- }
95
- ]
161
+ ...extraConfig
96
162
  }
97
163
  }
98
164
 
@@ -189,61 +255,22 @@ function getTextSummary(text: string, count = 100) {
189
255
  }
190
256
 
191
257
  export function defineConfig(config: UserConfig<Theme.Config>) {
192
- if (config?.themeConfig?.blog?.search === 'pagefind') {
193
- config.head = (config.head || []).concat([
194
- [
195
- 'script',
196
- {},
197
- `import('/_pagefind/pagefind.js')
198
- .then((module) => {
199
- window.__pagefind__ = module
200
- })
201
- .catch(() => {
202
- console.log('not load /_pagefind/pagefind.js')
203
- })`
204
- ]
205
- ])
206
- let flag = true
207
- let originLog: any = null
208
- config.vite = {
209
- ...config.vite,
210
- plugins: [
211
- ...(config.vite?.plugins || []),
212
- {
213
- name: '@sugarar/theme-plugin-pagefind',
214
- buildEnd() {
215
- const { log } = console
216
- // TODO: hack
217
- if (flag) {
218
- flag = false
219
- originLog = log
220
- Object.defineProperty(console, 'log', {
221
- value() {
222
- if (`${arguments[0]}`.includes('build complete')) {
223
- console.log = originLog
224
- setTimeout(() => {
225
- originLog()
226
- originLog('=== pagefind: https://pagefind.app/ ===')
227
- const command = `npx pagefind --source ${path.join(
228
- process.argv.slice(2)?.[1] || '.',
229
- '.vitepress/dist'
230
- )}`
231
- originLog(command)
232
- originLog()
233
- execSync(command, {
234
- stdio: 'inherit'
235
- })
236
- }, 100)
237
- }
238
- // @ts-ignore
239
- return log.apply(this, arguments)
240
- }
241
- })
242
- }
243
- }
244
- }
245
- ]
246
- }
258
+ // 兼容低版本主题配置
259
+ // @ts-ignore
260
+ if (config.themeConfig?.themeConfig) {
261
+ config.extends = checkKeys.reduce((pre, key) => {
262
+ // @ts-ignore
263
+ pre[key] = config.themeConfig[key]
264
+ // @ts-ignore
265
+ delete config.themeConfig[key]
266
+ return pre
267
+ }, {})
268
+
269
+ // 打印warn信息
270
+ setTimeout(() => {
271
+ console.warn('==↓ 主题配置方式过期,请尽快参照文档更新 ↓==')
272
+ console.warn('https://theme.sugarat.top/config/global.html')
273
+ }, 1200)
247
274
  }
248
275
  return config
249
276
  }