Skip to content

Commit ddb936f

Browse files
authored
fix(@142vip/vuepress): 修复importCode配置替换文件路径异常 (#539)
1 parent ec626d8 commit ddb936f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

packages/vuepress/src/core/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import type { NavbarOptions, SidebarOptions } from 'vuepress-theme-hope'
33
import { navbar, sidebar } from 'vuepress-theme-hope'
44
import { getVipViteBundler } from './bundler'
55

6+
/**
7+
* 用户配置
8+
*/
9+
export type VipVuepressUserConfig = UserConfig
10+
611
/**
712
* 定义 vuepress 配置
813
* @param config 配置
914
*/
10-
export function defineVipVuepressConfig(config: UserConfig): UserConfig {
15+
export function defineVipVuepressConfig(config: VipVuepressUserConfig): VipVuepressUserConfig {
1116
// 默认vite编译
1217
if (config.bundler == null) {
1318
config.bundler = getVipViteBundler()

packages/vuepress/src/core/theme.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ThemeOptions } from 'vuepress-theme-hope'
22
import path from 'node:path'
3-
import { fileURLToPath } from 'node:url'
43
import { hopeTheme } from 'vuepress-theme-hope'
54
import { i18n } from './i18n'
65
import { baseThemePluginOptions } from './theme-plugins'
@@ -143,19 +142,17 @@ export function getVipHopeTheme(userConfig: ThemeOptions) {
143142
* https://vuejs.press/zh/guide/markdown.html#%E5%AF%BC%E5%85%A5%E4%BB%A3%E7%A0%81%E5%9D%97
144143
* - 例如:引入的路径中包含@code,会替换为当前目录下的code目录
145144
* @param pathArray 路径规则,旧路径,新路径
145+
* @param cwd 文件目录,默认当前目录
146146
* @returns 新路径
147147
*/
148-
export function handleImportCodePath(pathArray: Array<[oldPath: string, newPath: string]>) {
148+
export function handleImportCodePath(pathArray: Array<[oldPath: string, newPath: string]>, cwd?: string) {
149149
return (str: string): string => {
150150
// 当前目录名
151-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
152-
153151
for (const [oldPath, newPath] of pathArray) {
154152
if (str.includes(oldPath)) {
155-
return str.replace(`/^${oldPath}/`, path.resolve(__dirname, newPath))
153+
return str.replace(oldPath, path.resolve(cwd ?? process.cwd(), newPath))
156154
}
157155
}
158-
159156
// 原路径
160157
return str
161158
}

0 commit comments

Comments
 (0)