Skip to content

Commit 733c9f0

Browse files
author
coderzcr
committed
fix: 修复路径规范化中重复添加basePath的问题
避免在链接和图片路径处理中重复添加basePath前缀,通过检查是否已包含basePath来防止重复处理
1 parent 87a224e commit 733c9f0

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

index.html

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@
136136
console.log('[路径规范化] 处理链接:', href);
137137

138138
// 统一使用basePath处理所有链接,无需区分环境
139-
if (href.startsWith('/')) {
139+
// 先检查是否已经包含basePath,避免重复添加
140+
if (href.startsWith('/') && !href.startsWith(basePath)) {
140141
// 对于以/开头的链接,确保使用正确的basePath
141142
const newHref = basePath + href.substring(1);
142143
link.setAttribute('href', newHref);
143144
console.log('[路径规范化] 修正链接:', href, '->', newHref);
144-
} else if (['newcomer/', 'easy/', 'medium/', 'difficult/', 'expert/'].some(prefix => href.startsWith(prefix))) {
145+
} else if (['newcomer/', 'easy/', 'medium/', 'difficult/', 'expert/'].some(prefix => href.startsWith(prefix)) && !href.startsWith(basePath)) {
145146
// 对于直接以目录开头的链接,添加basePath前缀
146147
const newHref = basePath + href;
147148
link.setAttribute('href', newHref);
@@ -157,13 +158,15 @@
157158
let src = img.getAttribute('src');
158159
console.log('[路径规范化] 处理图片路径:', src);
159160

160-
// 统一处理:提取public/后的部分,添加到basePath后面
161-
// 这样在任何环境下都能正确工作,因为basePath已经根据环境设置好了
162-
const imgPath = src.substring(11); // 移除 '../public/' 前缀
163-
const normalizedSrc = basePath + 'public/' + imgPath;
164-
165-
img.setAttribute('src', normalizedSrc);
166-
console.log('[路径规范化] 修正图片路径:', src, '->', normalizedSrc);
161+
// 先检查是否已经处理过,避免重复添加
162+
if (src.startsWith('../public/') && !src.startsWith(basePath)) {
163+
// 统一处理:提取public/后的部分,添加到basePath后面
164+
const imgPath = src.substring(11); // 移除 '../public/' 前缀
165+
const normalizedSrc = basePath + 'public/' + imgPath;
166+
167+
img.setAttribute('src', normalizedSrc);
168+
console.log('[路径规范化] 修正图片路径:', src, '->', normalizedSrc);
169+
}
167170
});
168171
}
169172

0 commit comments

Comments
 (0)