|
40 | 40 | <script> |
41 | 41 | // 动态设置basePath,适配本地开发和GitHub Pages部署 |
42 | 42 | // 改进GitHub Pages环境检测逻辑 |
43 | | - const isGitHubPages = window.location.hostname.includes('github.io') && |
44 | | - window.location.pathname.includes('/JavaWeb-Project-Source-Share/'); |
| 43 | + const isGitHubPages = window.location.hostname.includes('github.io'); |
| 44 | + const repoPath = '/JavaWeb-Project-Source-Share/'; |
45 | 45 |
|
46 | 46 | // 记录环境信息用于调试 |
47 | 47 | console.log('[Docsify配置] 当前环境:', window.location.hostname); |
48 | 48 | console.log('[Docsify配置] 路径:', window.location.pathname); |
49 | 49 | console.log('[Docsify配置] 识别为GitHub Pages:', isGitHubPages); |
50 | 50 |
|
51 | 51 | // 确定基础路径 |
52 | | - const basePath = isGitHubPages ? '/JavaWeb-Project-Source-Share/' : './'; |
| 52 | + const basePath = isGitHubPages ? repoPath : './'; |
53 | 53 | console.log('[Docsify配置] 使用basePath:', basePath); |
54 | 54 |
|
| 55 | + // 创建全面的alias映射,确保GitHub Pages环境下所有文件都能正确加载 |
| 56 | + const aliasMap = { |
| 57 | + // 基础文件映射 |
| 58 | + '/_sidebar.md': '/_sidebar.md', |
| 59 | + '/_coverpage.md': '/_coverpage.md', |
| 60 | + '/README.md': '/README.md', |
| 61 | + '/_404.md': '/_404.md', |
| 62 | + // 为GitHub Pages环境添加特殊映射 |
| 63 | + [repoPath + '_sidebar.md']: '/_sidebar.md', |
| 64 | + [repoPath + '_coverpage.md']: '/_coverpage.md', |
| 65 | + [repoPath + 'README.md']: '/README.md', |
| 66 | + [repoPath + '_404.md']: '/_404.md', |
| 67 | + // 处理子目录中的特殊文件引用 |
| 68 | + '/.*/_sidebar.md': '/_sidebar.md', |
| 69 | + '/.*/_coverpage.md': '/_coverpage.md' |
| 70 | + }; |
| 71 | + |
55 | 72 | window.$docsify = { |
56 | 73 | name: 'JavaWeb项目源码库', |
57 | 74 | repo: 'coderzcr/JavaWeb-Project-Source-Share', |
|
62 | 79 | basePath: basePath, |
63 | 80 | homepage: 'README.md', |
64 | 81 | relativePath: true, // 启用相对路径处理 |
65 | | - alias: { |
66 | | - // 增强别名映射,确保所有可能的文件路径都能正确解析 |
67 | | - '/JavaWeb-Project-Source-Share/': '/', |
68 | | - '/JavaWeb-Project-Source-Share/_sidebar.md': '/_sidebar.md', |
69 | | - '/JavaWeb-Project-Source-Share/_coverpage.md': '/_coverpage.md', |
70 | | - '/JavaWeb-Project-Source-Share/README.md': '/README.md', |
71 | | - '/JavaWeb-Project-Source-Share/_404.md': '/_404.md', |
72 | | - '/.*/_sidebar.md': '/_sidebar.md', |
73 | | - '/.*/_coverpage.md': '/_coverpage.md', |
74 | | - // 为各级别目录添加别名 |
75 | | - '/newcomer/.*': '/newcomer/$1', |
76 | | - '/easy/.*': '/easy/$1', |
77 | | - '/medium/.*': '/medium/$1', |
78 | | - '/difficult/.*': '/difficult/$1', |
79 | | - '/expert/.*': '/expert/$1' |
| 82 | + alias: aliasMap, |
| 83 | + // 添加钩子函数来处理路径问题 |
| 84 | + hooks: { |
| 85 | + // 路由钩子,处理路径规范化 |
| 86 | + beforeEach: function (router) { |
| 87 | + console.log('[路由钩子] 当前路由:', router); |
| 88 | + // 确保路径正确,处理GitHub Pages环境下的路径问题 |
| 89 | + if (isGitHubPages && !router.path.includes(repoPath)) { |
| 90 | + console.log('[路由钩子] 修正GitHub Pages路径'); |
| 91 | + // 这里可以进行路径修正 |
| 92 | + } |
| 93 | + }, |
| 94 | + // 页面渲染完成后的钩子 |
| 95 | + afterEach: function (html, next) { |
| 96 | + console.log('[渲染钩子] 页面渲染完成'); |
| 97 | + // 确保目录生成 |
| 98 | + setTimeout(createTableOfContents, 100); |
| 99 | + next(html); |
| 100 | + } |
80 | 101 | }, |
81 | 102 | search: { |
82 | 103 | maxAge: 86400000, |
|
98 | 119 | <script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/zoom-image.min.js"></script> |
99 | 120 | <script src="//cdn.jsdelivr.net/npm/docsify-pagination/dist/docsify-pagination.min.js"></script> |
100 | 121 |
|
| 122 | + <!-- 添加路径规范化脚本,确保GitHub Pages环境下的链接正常工作 --> |
| 123 | + <script> |
| 124 | + // 页面加载完成后执行路径规范化 |
| 125 | + window.addEventListener('load', function() { |
| 126 | + console.log('[路径规范化] 页面加载完成,开始处理链接'); |
| 127 | + |
| 128 | + // 定期检查并修正页面中的链接 |
| 129 | + function normalizeLinks() { |
| 130 | + // 获取所有内部链接 |
| 131 | + const links = document.querySelectorAll('a[href^="/"], a[href^="newcomer/"], a[href^="easy/"], a[href^="medium/"], a[href^="difficult/"], a[href^="expert/"]'); |
| 132 | + console.log('[路径规范化] 找到链接数量:', links.length); |
| 133 | + |
| 134 | + links.forEach(link => { |
| 135 | + let href = link.getAttribute('href'); |
| 136 | + console.log('[路径规范化] 处理链接:', href); |
| 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 | + } |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + // 立即执行一次 |
| 151 | + normalizeLinks(); |
| 152 | + |
| 153 | + // 定期执行,确保动态内容加载后的链接也被修正 |
| 154 | + setInterval(normalizeLinks, 2000); |
| 155 | + |
| 156 | + // 监听hash变化,确保路径正确 |
| 157 | + window.addEventListener('hashchange', function() { |
| 158 | + console.log('[路径规范化] Hash变化:', window.location.hash); |
| 159 | + setTimeout(normalizeLinks, 100); |
| 160 | + }); |
| 161 | + }); |
| 162 | + </script> |
| 163 | + |
101 | 164 | <!-- 文章内目录生成插件 --> |
102 | 165 | <script> |
103 | 166 | // 简化的目录生成函数 |
|
0 commit comments