|
136 | 136 | console.log('[路径规范化] 处理链接:', href); |
137 | 137 |
|
138 | 138 | // 统一使用basePath处理所有链接,无需区分环境 |
139 | | - if (href.startsWith('/')) { |
| 139 | + // 先检查是否已经包含basePath,避免重复添加 |
| 140 | + if (href.startsWith('/') && !href.startsWith(basePath)) { |
140 | 141 | // 对于以/开头的链接,确保使用正确的basePath |
141 | 142 | const newHref = basePath + href.substring(1); |
142 | 143 | link.setAttribute('href', newHref); |
143 | 144 | 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)) { |
145 | 146 | // 对于直接以目录开头的链接,添加basePath前缀 |
146 | 147 | const newHref = basePath + href; |
147 | 148 | link.setAttribute('href', newHref); |
|
157 | 158 | let src = img.getAttribute('src'); |
158 | 159 | console.log('[路径规范化] 处理图片路径:', src); |
159 | 160 |
|
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 | + } |
167 | 170 | }); |
168 | 171 | } |
169 | 172 |
|
|
0 commit comments