|
11 | 11 | /> |
12 | 12 | <!--主题--> |
13 | 13 | <link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css" /> |
14 | | - <!-- 你也可以在这里添加自定义 CSS 来控制侧边栏折叠样式,或者使用 JS 动态添加 (如下面的插件所示) --> |
| 14 | + <!-- CSS for Collapsible Sidebar --> |
15 | 15 | <style> |
16 | | - /* CSS for Collapsible Sidebar */ |
17 | | - /* Hide nested lists by default */ |
18 | 16 | .sidebar .sidebar-nav ul { |
19 | 17 | display: none; |
20 | 18 | } |
21 | | - /* Show nested lists when the parent LI has the 'expanded' class */ |
22 | 19 | .sidebar .sidebar-nav li.collapsible.expanded > ul { |
23 | 20 | display: block; |
24 | 21 | } |
25 | | - /* Optional: Style for collapsible items/links */ |
26 | 22 | .sidebar .sidebar-nav li.collapsible > a { |
27 | | - /* Needed if you add content like arrows */ |
28 | | - position: relative; /* Allow positioning of pseudo-elements */ |
29 | | - padding-right: 20px; /* Make space for the indicator */ |
30 | | - cursor: pointer; /* Indicate it's clickable */ |
| 23 | + position: relative; |
| 24 | + padding-right: 20px; |
| 25 | + cursor: pointer; |
31 | 26 | } |
32 | | - /* Optional: Add expand/collapse indicator (right arrow for collapsed, down arrow for expanded) */ |
33 | 27 | .sidebar .sidebar-nav li.collapsible > a::before { |
34 | | - content: '►'; /* Right arrow when collapsed */ |
| 28 | + content: '►'; |
35 | 29 | position: absolute; |
36 | | - right: 5px; /* Position relative to the link */ |
| 30 | + right: 5px; |
37 | 31 | top: 50%; |
38 | 32 | transform: translateY(-50%); |
39 | 33 | font-size: 0.8em; |
40 | | - transition: transform 0.2s ease-in-out; /* Smooth transition for indicator */ |
| 34 | + transition: transform 0.2s ease-in-out; |
41 | 35 | } |
42 | 36 | .sidebar .sidebar-nav li.collapsible.expanded > a::before { |
43 | | - content: '▼'; /* Down arrow when expanded */ |
44 | | - transform: translateY(-50%) rotate(0deg); /* No rotation needed for down arrow */ |
| 37 | + content: '▼'; |
| 38 | + transform: translateY(-50%) rotate(0deg); |
45 | 39 | } |
46 | | - /* Ensure padding is consistent if some links don't have indicators */ |
47 | 40 | .sidebar .sidebar-nav a { |
48 | | - padding-right: 10px; /* Base padding for all links */ |
| 41 | + padding-right: 10px; |
49 | 42 | } |
50 | 43 | .sidebar .sidebar-nav li.collapsible > a { |
51 | | - padding-right: 20px; /* Override for collapsible links */ |
| 44 | + padding-right: 20px; |
52 | 45 | } |
53 | 46 | </style> |
54 | 47 | </head> |
|
63 | 56 | </nav> |
64 | 57 | <div id="app"></div> |
65 | 58 |
|
66 | | - <!-- docsify-edit-on-github (需要放在 docsify 脚本之前,因为它修改的是 $docsify 配置) --> |
| 59 | + <!-- docsify-edit-on-github --> |
67 | 60 | <script src="//unpkg.com/docsify-edit-on-github/index.js"></script> |
68 | 61 |
|
69 | 62 | <script> |
|
75 | 68 | window.$docsify = { |
76 | 69 | name: "JavaGuide", |
77 | 70 | repo: "https://github.com/Snailclimb/JavaGuide", |
78 | | - maxLevel: 4, //最大支持渲染的标题层级 |
79 | | - //封面,_coverpage.md |
| 71 | + maxLevel: 4, |
80 | 72 | //coverpage: true, |
81 | | - auto2top: true, //切换页面后是否自动跳转到页面顶部 |
| 73 | + auto2top: true, |
82 | 74 | //ga: 'UA-138586553-1', |
83 | 75 | //logo: 'https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-3logo-透明.png' , |
84 | 76 | search: { |
85 | | - //maxAge: 86400000, // 过期时间,单位毫秒,默认一天 |
| 77 | + //maxAge: 86400000, |
86 | 78 | paths: "auto", |
87 | 79 | placeholder: "搜索", |
88 | 80 | noData: "找不到结果", |
89 | | - // 搜索标题的最大程级, 1 - 6 |
90 | 81 | depth: 3, |
91 | 82 | }, |
92 | | - // 备案 |
93 | 83 | beian: { |
94 | 84 | ICP: "鄂ICP备2020015769号", |
95 | 85 | NISMSP: { |
|
98 | 88 | id: "", |
99 | 89 | }, |
100 | 90 | }, |
101 | | - // 字数统计 |
102 | 91 | count: { |
103 | 92 | countable: true, |
104 | 93 | fontsize: "0.9em", |
105 | 94 | color: "rgb(90,90,90)", |
106 | 95 | language: "chinese", |
107 | 96 | }, |
108 | 97 |
|
109 | | - // =========== Plugins Configuration =========== |
110 | 98 | plugins: [ |
111 | | - // 1. Existing EditOnGithubPlugin |
112 | 99 | EditOnGithubPlugin.create( |
113 | 100 | "https://github.com/Snailclimb/JavaGuide/blob/master/" |
114 | 101 | ), |
115 | 102 |
|
116 | | - // 3. New Plugin to Add referrerpolicy="no-referrer" and Implement Collapsible Sidebar |
117 | 103 | function (hook) { |
118 | | - // This part adds referrerpolicy="no-referrer" to all img tags |
| 104 | + const targetOrigin = 'https://oss.javaguide.cn/'; |
| 105 | + const antiHotlinkParamKeyVal = 'x-oss-process=style/javaguide'; |
| 106 | + |
| 107 | + hook.afterEach(function (html, next) { |
| 108 | + const imgRegex = new RegExp(`<img[^>]*src="(https?:\\/\\/oss\\.javaguide\\.cn\\/[^"]*)"[^>]*>`, 'gi'); |
| 109 | + let modifiedHtml = html.replace(imgRegex, function(match, srcUrl) { |
| 110 | + if (srcUrl.includes(antiHotlinkParamKeyVal)) { |
| 111 | + return match; |
| 112 | + } |
| 113 | + const separator = srcUrl.includes('?') ? '&' : '?'; |
| 114 | + const newSrcUrl = srcUrl + separator + antiHotlinkParamKeyVal; |
| 115 | + return match.replace(srcUrl, newSrcUrl); |
| 116 | + }); |
| 117 | + next(modifiedHtml); |
| 118 | + }); |
| 119 | + }, |
| 120 | + |
| 121 | + // --- Corrected Plugin to Add referrerpolicy and Implement Collapsible Sidebar --- |
| 122 | + function (hook) { |
| 123 | + // Add referrerpolicy="no-referrer" |
119 | 124 | hook.afterEach(function (html, next) { |
120 | 125 | const imgTagRegex = /<img[^>]*>/gi; |
121 | 126 | let modifiedHtml = html.replace(imgTagRegex, function(match) { |
|
127 | 132 | next(modifiedHtml); |
128 | 133 | }); |
129 | 134 |
|
130 | | - // This part implements the collapsible sidebar logic |
131 | | - // Use doneEach or ready hook to ensure sidebar DOM is available |
132 | | - hook.doneEach(function() { // Or hook.ready(function() { ... }); |
| 135 | + // Implement collapsible sidebar logic |
| 136 | + hook.doneEach(function() { |
133 | 137 | const sidebar = document.querySelector('.sidebar'); |
134 | | - if (!sidebar) return; // Exit if sidebar element is not found |
| 138 | + if (!sidebar) return; |
135 | 139 |
|
136 | | - // Find all LIs that contain a direct child UL (these are the parent items with nested menus) |
137 | | - // Use querySelector(':scope > ul') for checking direct children. |
138 | | - // Fallback for older browsers that don't support :scope (though uncommon now) |
139 | 140 | const allLIs = sidebar.querySelectorAll('.sidebar-nav li'); |
140 | 141 | const collapsibleItems = []; |
141 | | - allLIs.forEach(li => { |
142 | | - // Check if this LI contains a UL as a direct child |
| 142 | + |
| 143 | + allLIs.forEach(li => { |
143 | 144 | if (li.querySelector(':scope > ul')) { |
144 | 145 | collapsibleItems.push(li); |
145 | 146 | } |
146 | | - }); |
| 147 | + }); |
147 | 148 |
|
148 | | - // Process each potential collapsible item |
149 | 149 | collapsibleItems.forEach(li => { |
150 | | - li.classList.add('collapsible'); // Mark it with a class |
| 150 | + li.classList.add('collapsible'); |
| 151 | + const link = li.querySelector(':scope > a'); // Find the direct child link |
151 | 152 |
|
152 | | - // Find the direct link within this collapsible LI |
153 | | - const link = li.querySelector(':scope > a'); // Direct child link |
| 153 | + // *** FIX: Check if the link element exists before adding event listener *** |
154 | 154 | if (link) { |
155 | | - // Add click listener to the link |
156 | 155 | link.addEventListener('click', function(event) { |
157 | | - // Toggle the 'expanded' class on the parent LI (li is available via closure) |
| 156 | + // Toggle the 'expanded' class on the parent LI |
158 | 157 | li.classList.toggle('expanded'); |
159 | 158 |
|
160 | | - // --- IMPORTANT --- |
161 | | - // Decide if clicking this link should *also* navigate to the page it links to. |
162 | | - // - If this link represents a *category* title that doesn't have its own page, |
163 | | - // and clicking it should *only* toggle the submenu: |
164 | | - // event.preventDefault(); |
165 | | - // - If this link represents a *page* (e.g., an index page for the category), |
166 | | - // and clicking it should *both* toggle the submenu *and* navigate: |
167 | | - // Remove event.preventDefault(); // (Current state in the code) |
168 | | - // --- End IMPORTANT --- |
169 | | - |
170 | | - // event.preventDefault(); // Uncomment this line if clicking should only toggle collapse |
| 159 | + // Uncomment the line below if clicking should ONLY toggle and NOT navigate |
| 160 | + // event.preventDefault(); |
171 | 161 |
|
172 | | - // Stop event from bubbling up, preventing potential interference |
173 | | - event.stopPropagation(); |
| 162 | + event.stopPropagation(); // Prevent parent listeners (if any) from firing |
174 | 163 | }); |
| 164 | + } else { |
| 165 | + // Optional: Log a warning if a collapsible LI has no direct link |
| 166 | + // This might indicate an issue with your _sidebar.md structure |
| 167 | + console.warn("Collapsible LI found without a direct child link:", li); |
| 168 | + // You might want to add a click listener to the LI itself here as a fallback |
| 169 | + li.addEventListener('click', function(event) { |
| 170 | + li.classList.toggle('expanded'); |
| 171 | + // Check if click target is not a link within this LI |
| 172 | + if (!event.target.closest('a')) { |
| 173 | + event.preventDefault(); // Prevent default if not clicking a link further down |
| 174 | + } |
| 175 | + event.stopPropagation(); |
| 176 | + }); |
175 | 177 | } |
176 | | - // Note: Adding click listener to the LI itself might be another approach, |
177 | | - // but clicking the link within the LI is usually the intended interaction point. |
178 | | - // Adding it to the link also allows using the link's default behavior if needed. |
| 178 | + // --- End FIX --- |
179 | 179 | }); |
180 | 180 |
|
181 | | - // --- Handle initial expansion of the active path --- |
182 | | - // Find the currently active link in the sidebar |
| 181 | + // Handle initial expansion of the active path |
183 | 182 | const activeLink = sidebar.querySelector('.sidebar-nav .active'); |
184 | 183 | if (activeLink) { |
185 | | - // Start from the LI containing the active link |
186 | 184 | let currentElement = activeLink.parentElement; |
187 | | - // Traverse up the DOM tree towards the sidebar root |
188 | 185 | while (currentElement && currentElement !== sidebar) { |
189 | | - // If the current element is an LI AND it has the 'collapsible' class |
190 | 186 | if (currentElement.tagName === 'LI' && currentElement.classList.contains('collapsible')) { |
191 | | - currentElement.classList.add('expanded'); // Expand this parent collapsible LI |
| 187 | + currentElement.classList.add('expanded'); |
192 | 188 | } |
193 | | - // Move up to the next parent element |
194 | 189 | currentElement = currentElement.parentElement; |
195 | 190 | } |
196 | 191 | } |
197 | | - // --- End initial expansion logic --- |
198 | 192 | }); |
199 | 193 | } |
200 | | - // --- End Collapsible Sidebar Plugin --- |
| 194 | + // --- End Corrected Plugin --- |
201 | 195 | ], |
202 | | - // =========================================== |
203 | 196 | }; |
204 | 197 | </script> |
205 | 198 |
|
206 | | - <!-- Main Docsify script (必须在配置脚本之后) --> |
| 199 | + <!-- Main Docsify script --> |
207 | 200 | <script src="//unpkg.com/docsify/lib/docsify.min.js"></script> |
208 | 201 |
|
209 | | - <!-- 其他 Docsify 插件 (通常在主 docsify 脚本之后) --> |
210 | | - <!--Java代码高亮--> |
| 202 | + <!-- Other Docsify Plugins --> |
211 | 203 | <script src="//unpkg.com/prismjs/components/prism-java.js"></script> |
212 | | - <!--全文搜索,直接用官方提供的无法生效--> |
213 | 204 | <script src="https://cdn.bootcss.com/docsify/4.5.9/plugins/search.min.js"></script> |
214 | | - <!--谷歌统计 |
215 | | - <script src="//unpkg.com/docsify" data-ga="UA-138586553-1"></script> |
216 | | - <script src="//unpkg.com/docsify/lib/plugins/ga.js"></script> |
217 | | - --> |
218 | | - <!-- 复制到剪贴板 --> |
219 | 205 | <script src="//unpkg.com/docsify-copy-code"></script> |
220 | | - <!-- 图片缩放 --> |
221 | 206 | <script src="//unpkg.com/docsify/lib/plugins/zoom-image.js"></script> |
222 | | - <!-- 表情 --> |
223 | 207 | <script src="//unpkg.com/docsify/lib/plugins/emoji.js"></script> |
224 | | - <!-- 字数统计 --> |
225 | 208 | <script src="//unpkg.com/docsify-count/dist/countable.js"></script> |
226 | | - <!-- 备案 --> |
227 | 209 | <script src="https://cdn.jsdelivr.net/npm/docsify-beian@latest/dist/beian.min.js"></script> |
228 | 210 |
|
229 | | - <!-- 如果你需要 MathJax,请在此处或 <head> 中引入 --> |
230 | | - <!-- <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" async></script> --> |
231 | | - |
232 | 211 | </body> |
233 | 212 | </html> |
0 commit comments