Skip to content

Commit 87a224e

Browse files
author
coderzcr
committed
refactor(路径处理): 统一处理链接和图片路径
- 将路径规范化逻辑重构为统一的basePath处理方式 - 新增图片路径处理逻辑,确保所有环境下都能正确加载 - 优化日志输出和函数命名,提高代码可读性
1 parent 969be6e commit 87a224e

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

difficult/difficult.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@
3535

3636
欢迎关注我的公众号“**张有路**”,原创技术文章第一时间推送。
3737

38-
<center>
39-
<img src="../public/oldPicturesFromGitee/qrcode.gif" style="width: 100px;">
40-
</center>
38+
![](../public/oldPicturesFromGitee/qrcode.gif)
39+

easy/easy.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@
2828

2929
欢迎关注我的公众号“**张有路**”,原创技术文章第一时间推送。
3030

31-
<center>
32-
<img src="../public/oldPicturesFromGitee/qrcode.gif" style="width: 100px;">
33-
</center>
31+
![](../public/oldPicturesFromGitee/qrcode.gif)
32+

expert/expert.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@
2424

2525
欢迎关注我的公众号“**张有路**”,原创技术文章第一时间推送。
2626

27-
<center>
28-
<img src="../public/oldPicturesFromGitee/qrcode.gif" style="width: 100px;">
29-
</center>
27+
![](../public/oldPicturesFromGitee/qrcode.gif)
28+

index.html

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@
119119
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/zoom-image.min.js"></script>
120120
<script src="//cdn.jsdelivr.net/npm/docsify-pagination/dist/docsify-pagination.min.js"></script>
121121

122-
<!-- 添加路径规范化脚本,确保GitHub Pages环境下的链接正常工作 -->
122+
<!-- 添加路径规范化脚本,使用统一的basePath处理所有环境 -->
123123
<script>
124124
// 页面加载完成后执行路径规范化
125125
window.addEventListener('load', function() {
126-
console.log('[路径规范化] 页面加载完成,开始处理链接');
126+
console.log('[路径规范化] 页面加载完成,开始处理链接和图片');
127127

128-
// 定期检查并修正页面中的链接
129-
function normalizeLinks() {
128+
// 定期检查并修正页面中的链接和图片
129+
function normalizePaths() {
130130
// 获取所有内部链接
131131
const links = document.querySelectorAll('a[href^="/"], a[href^="newcomer/"], a[href^="easy/"], a[href^="medium/"], a[href^="difficult/"], a[href^="expert/"]');
132132
console.log('[路径规范化] 找到链接数量:', links.length);
@@ -135,28 +135,48 @@
135135
let href = link.getAttribute('href');
136136
console.log('[路径规范化] 处理链接:', href);
137137

138-
// 如果是GitHub Pages环境
139-
if (isGitHubPages) {
140-
// 确保链接包含仓库路径前缀
141-
if (!href.includes(repoPath) && href.startsWith('/')) {
142-
const newHref = repoPath + href.substring(1);
143-
link.setAttribute('href', newHref);
144-
console.log('[路径规范化] 修正链接:', href, '->', newHref);
145-
}
138+
// 统一使用basePath处理所有链接,无需区分环境
139+
if (href.startsWith('/')) {
140+
// 对于以/开头的链接,确保使用正确的basePath
141+
const newHref = basePath + href.substring(1);
142+
link.setAttribute('href', newHref);
143+
console.log('[路径规范化] 修正链接:', href, '->', newHref);
144+
} else if (['newcomer/', 'easy/', 'medium/', 'difficult/', 'expert/'].some(prefix => href.startsWith(prefix))) {
145+
// 对于直接以目录开头的链接,添加basePath前缀
146+
const newHref = basePath + href;
147+
link.setAttribute('href', newHref);
148+
console.log('[路径规范化] 修正相对链接:', href, '->', newHref);
146149
}
147150
});
151+
152+
// 处理图片路径,完全依赖basePath,不区分环境
153+
const images = document.querySelectorAll('img[src^="../public/"]');
154+
console.log('[路径规范化] 找到相对路径图片数量:', images.length);
155+
156+
images.forEach(img => {
157+
let src = img.getAttribute('src');
158+
console.log('[路径规范化] 处理图片路径:', src);
159+
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);
167+
});
148168
}
149169

150170
// 立即执行一次
151-
normalizeLinks();
171+
normalizePaths();
152172

153-
// 定期执行,确保动态内容加载后的链接也被修正
154-
setInterval(normalizeLinks, 2000);
173+
// 定期执行,确保动态内容加载后的路径也被修正
174+
setInterval(normalizePaths, 1000);
155175

156-
// 监听hash变化,确保路径正确
176+
// 监听hash变化,确保页面切换后也能正确处理
157177
window.addEventListener('hashchange', function() {
158178
console.log('[路径规范化] Hash变化:', window.location.hash);
159-
setTimeout(normalizeLinks, 100);
179+
setTimeout(normalizePaths, 100);
160180
});
161181
});
162182
</script>

medium/medium.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,5 @@
7474

7575
欢迎关注我的公众号“**张有路**”,原创技术文章第一时间推送。
7676

77-
<center>
78-
<img src="../public/oldPicturesFromGitee/qrcode.gif" style="width: 100px;">
79-
</center>
77+
![](../public/oldPicturesFromGitee/qrcode.gif)
78+

newcomer/newcomer.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@
3131

3232
欢迎关注我的公众号“**张有路**”,原创技术文章第一时间推送。
3333

34-
<center>
35-
<img src="../public/oldPicturesFromGitee/qrcode.gif" style="width: 100px;">
36-
</center>
34+
![](../public/oldPicturesFromGitee/qrcode.gif)
35+

0 commit comments

Comments
 (0)