@sugarat/theme 0.1.19 → 0.1.20

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 CHANGED
@@ -120,40 +120,44 @@ function getThemeConfig(cfg) {
120
120
  const extraConfig = {};
121
121
  if (cfg?.search === "pagefind" || cfg?.search instanceof Object && cfg.search.mode === "pagefind") {
122
122
  checkKeys.push("vite");
123
- let flag = true;
124
- let originLog = null;
123
+ let resolveConfig;
125
124
  extraConfig.vite = {
126
125
  plugins: [
127
126
  {
128
127
  name: "@sugarar/theme-plugin-pagefind",
129
128
  enforce: "pre",
130
- buildEnd() {
131
- const { log } = console;
132
- if (flag) {
133
- flag = false;
134
- originLog = log;
135
- Object.defineProperty(console, "log", {
136
- value() {
137
- if (`${arguments[0]}`.includes("build complete")) {
138
- console.log = originLog;
139
- setTimeout(() => {
140
- originLog();
141
- originLog("=== pagefind: https://pagefind.app/ ===");
142
- const command = `npx pagefind --source ${import_path.default.join(
143
- process.argv.slice(2)?.[1] || ".",
144
- ".vitepress/dist"
145
- )}`;
146
- originLog(command);
147
- originLog();
148
- (0, import_child_process.execSync)(command, {
149
- stdio: "inherit"
150
- });
151
- }, 100);
152
- }
153
- return log.apply(this, arguments);
154
- }
155
- });
129
+ configResolved(config) {
130
+ if (resolveConfig) {
131
+ return;
132
+ }
133
+ resolveConfig = config;
134
+ const vitepressConfig = config.vitepress;
135
+ if (!vitepressConfig) {
136
+ return;
156
137
  }
138
+ const selfBuildEnd = vitepressConfig.buildEnd;
139
+ vitepressConfig.buildEnd = (siteConfig) => {
140
+ selfBuildEnd?.(siteConfig);
141
+ const ignore = [
142
+ "div.aside",
143
+ "a.header-anchor"
144
+ ];
145
+ const { log } = console;
146
+ log();
147
+ log("=== pagefind: https://pagefind.app/ ===");
148
+ let command = `npx pagefind --source ${import_path.default.join(
149
+ process.argv.slice(2)?.[1] || ".",
150
+ ".vitepress/dist"
151
+ )}`;
152
+ if (ignore.length) {
153
+ command += ` --exclude-selectors "${ignore.join(", ")}"`;
154
+ }
155
+ log(command);
156
+ log();
157
+ (0, import_child_process.execSync)(command, {
158
+ stdio: "inherit"
159
+ });
160
+ };
157
161
  },
158
162
  transform(code, id) {
159
163
  if (id.endsWith("theme-default/Layout.vue")) {
@@ -214,7 +218,7 @@ function clearMatterContent(content) {
214
218
  function getFileBirthTime(url) {
215
219
  let date = new Date();
216
220
  try {
217
- const infoStr = (0, import_child_process.execSync)(`git log -1 --pretty="%ci" ${url}`).toString("utf-8").trim();
221
+ const infoStr = (0, import_child_process.spawnSync)("git", ["log", "-1", '--pretty="%ci"', url]).stdout?.toString().replace(/["']/g, "").trim();
218
222
  if (infoStr) {
219
223
  date = new Date(infoStr);
220
224
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sugarat/theme",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "description": "简约风的 Vitepress 博客主题,sugarat vitepress blog theme",
5
5
  "main": "src/index.ts",
6
6
  "exports": {
@@ -132,7 +132,7 @@ import { computed, nextTick, ref, watch, onBeforeMount, onMounted } from 'vue'
132
132
  import { Command } from 'vue-command-palette'
133
133
  import { useRoute, useRouter, withBase } from 'vitepress'
134
134
  import { useMagicKeys, useWindowSize } from '@vueuse/core'
135
- import { formatDate } from '../utils'
135
+ import { chineseSearchOptimize, formatDate } from '../utils'
136
136
  import { useArticles, useBlogConfig } from '../composables/config/blog'
137
137
  import { Theme } from '../composables/config'
138
138
  import LogoPagefind from './LogoPagefind.vue'
@@ -158,7 +158,7 @@ const openSearch = computed(() =>
158
158
  )
159
159
 
160
160
  const addInlineScript = () => {
161
- const scriptText = `import('/_pagefind/pagefind.js')
161
+ const scriptText = `import('${withBase('/_pagefind/pagefind.js')}')
162
162
  .then((module) => {
163
163
  window.__pagefind__ = module
164
164
  })
@@ -247,14 +247,16 @@ watch(
247
247
  } else {
248
248
  // @ts-ignore
249
249
  await window?.__pagefind__
250
- ?.search?.(searchWords.value)
250
+ ?.search?.(chineseSearchOptimize(searchWords.value))
251
251
  .then(async (search: any) => {
252
252
  const result = await Promise.all(
253
253
  search.results.map((v: any) => v.data())
254
254
  )
255
255
  searchResult.value = []
256
256
  docs.value.forEach((v) => {
257
- const match = result.find((r) => r.url.startsWith(v.route))
257
+ const match = result.find((r) =>
258
+ r.url.startsWith(withBase(v.route))
259
+ )
258
260
  if (match) {
259
261
  searchResult.value.push({
260
262
  ...v,
package/src/node.ts CHANGED
@@ -2,9 +2,9 @@
2
2
  import glob from 'fast-glob'
3
3
  import matter from 'gray-matter'
4
4
  import fs from 'fs'
5
- import { execSync, spawn } from 'child_process'
5
+ import { execSync, spawn, spawnSync } from 'child_process'
6
6
  import path from 'path'
7
- import type { UserConfig } from 'vitepress'
7
+ import type { SiteConfig, UserConfig } from 'vitepress'
8
8
  import { formatDate } from './utils/index'
9
9
  import type { Theme } from './composables/config/index'
10
10
 
@@ -91,40 +91,52 @@ export function getThemeConfig(cfg?: Partial<Theme.BlogConfig>) {
91
91
  (cfg?.search instanceof Object && cfg.search.mode === 'pagefind')
92
92
  ) {
93
93
  checkKeys.push('vite')
94
- let flag = true
95
- let originLog: any = null
94
+
95
+ let resolveConfig: any
96
96
  extraConfig.vite = {
97
97
  plugins: [
98
98
  {
99
99
  name: '@sugarar/theme-plugin-pagefind',
100
100
  enforce: 'pre',
101
- buildEnd() {
102
- const { log } = console
103
- // TODO: hack
104
- if (flag) {
105
- flag = false
106
- originLog = log
107
- Object.defineProperty(console, 'log', {
108
- value() {
109
- if (`${arguments[0]}`.includes('build complete')) {
110
- console.log = originLog
111
- setTimeout(() => {
112
- originLog()
113
- originLog('=== pagefind: https://pagefind.app/ ===')
114
- const command = `npx pagefind --source ${path.join(
115
- process.argv.slice(2)?.[1] || '.',
116
- '.vitepress/dist'
117
- )}`
118
- originLog(command)
119
- originLog()
120
- execSync(command, {
121
- stdio: 'inherit'
122
- })
123
- }, 100)
124
- }
125
- // @ts-ignore
126
- return log.apply(this, arguments)
127
- }
101
+ configResolved(config: any) {
102
+ if (resolveConfig) {
103
+ return
104
+ }
105
+ resolveConfig = config
106
+
107
+ const vitepressConfig: SiteConfig = config.vitepress
108
+ if (!vitepressConfig) {
109
+ return
110
+ }
111
+
112
+ // 添加 自定义 vitepress 的钩子
113
+ const selfBuildEnd = vitepressConfig.buildEnd
114
+ vitepressConfig.buildEnd = (siteConfig: any) => {
115
+ // 调用自己的
116
+ selfBuildEnd?.(siteConfig)
117
+ // 调用pagefind
118
+ const ignore: string[] = [
119
+ // 侧边栏内容
120
+ 'div.aside',
121
+ // 标题锚点
122
+ 'a.header-anchor'
123
+ ]
124
+ const { log } = console
125
+ log()
126
+ log('=== pagefind: https://pagefind.app/ ===')
127
+ let command = `npx pagefind --source ${path.join(
128
+ process.argv.slice(2)?.[1] || '.',
129
+ '.vitepress/dist'
130
+ )}`
131
+
132
+ if (ignore.length) {
133
+ command += ` --exclude-selectors "${ignore.join(', ')}"`
134
+ }
135
+
136
+ log(command)
137
+ log()
138
+ execSync(command, {
139
+ stdio: 'inherit'
128
140
  })
129
141
  }
130
142
  },
@@ -207,8 +219,9 @@ export function getFileBirthTime(url: string) {
207
219
 
208
220
  try {
209
221
  // 参考 vitepress 中的 getGitTimestamp 实现
210
- const infoStr = execSync(`git log -1 --pretty="%ci" ${url}`)
211
- .toString('utf-8')
222
+ const infoStr = spawnSync('git', ['log', '-1', '--pretty="%ci"', url])
223
+ .stdout?.toString()
224
+ .replace(/["']/g, '')
212
225
  .trim()
213
226
  if (infoStr) {
214
227
  date = new Date(infoStr)
@@ -82,3 +82,10 @@ export default function countWord(data: string) {
82
82
  }
83
83
  return count
84
84
  }
85
+
86
+ export function chineseSearchOptimize(input: string) {
87
+ return input
88
+ .replace(/[\u4e00-\u9fa5]/g, ' $& ')
89
+ .replace(/\s+/g, ' ')
90
+ .trim()
91
+ }