|
119 | 119 | <script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/zoom-image.min.js"></script> |
120 | 120 | <script src="//cdn.jsdelivr.net/npm/docsify-pagination/dist/docsify-pagination.min.js"></script> |
121 | 121 |
|
122 | | - <!-- 添加路径规范化脚本,确保GitHub Pages环境下的链接正常工作 --> |
| 122 | + <!-- 添加路径规范化脚本,使用统一的basePath处理所有环境 --> |
123 | 123 | <script> |
124 | 124 | // 页面加载完成后执行路径规范化 |
125 | 125 | window.addEventListener('load', function() { |
126 | | - console.log('[路径规范化] 页面加载完成,开始处理链接'); |
| 126 | + console.log('[路径规范化] 页面加载完成,开始处理链接和图片'); |
127 | 127 |
|
128 | | - // 定期检查并修正页面中的链接 |
129 | | - function normalizeLinks() { |
| 128 | + // 定期检查并修正页面中的链接和图片 |
| 129 | + function normalizePaths() { |
130 | 130 | // 获取所有内部链接 |
131 | 131 | const links = document.querySelectorAll('a[href^="/"], a[href^="newcomer/"], a[href^="easy/"], a[href^="medium/"], a[href^="difficult/"], a[href^="expert/"]'); |
132 | 132 | console.log('[路径规范化] 找到链接数量:', links.length); |
|
135 | 135 | let href = link.getAttribute('href'); |
136 | 136 | console.log('[路径规范化] 处理链接:', href); |
137 | 137 |
|
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); |
146 | 149 | } |
147 | 150 | }); |
| 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 | + }); |
148 | 168 | } |
149 | 169 |
|
150 | 170 | // 立即执行一次 |
151 | | - normalizeLinks(); |
| 171 | + normalizePaths(); |
152 | 172 |
|
153 | | - // 定期执行,确保动态内容加载后的链接也被修正 |
154 | | - setInterval(normalizeLinks, 2000); |
| 173 | + // 定期执行,确保动态内容加载后的路径也被修正 |
| 174 | + setInterval(normalizePaths, 1000); |
155 | 175 |
|
156 | | - // 监听hash变化,确保路径正确 |
| 176 | + // 监听hash变化,确保页面切换后也能正确处理 |
157 | 177 | window.addEventListener('hashchange', function() { |
158 | 178 | console.log('[路径规范化] Hash变化:', window.location.hash); |
159 | | - setTimeout(normalizeLinks, 100); |
| 179 | + setTimeout(normalizePaths, 100); |
160 | 180 | }); |
161 | 181 | }); |
162 | 182 | </script> |
|
0 commit comments