@@ -242,6 +242,77 @@ const Index = () => {
242
242
}
243
243
}
244
244
245
+ useEffect ( ( ) => {
246
+ // handle footnote links
247
+ const fixFootnoteLinks = ( ) => {
248
+ const footnoteLinks = document . querySelectorAll (
249
+ 'a[href^="#"]:not([data-footnote-fixed])' ,
250
+ ) ;
251
+
252
+ footnoteLinks . forEach ( ( link ) => {
253
+ link . setAttribute ( 'data-footnote-fixed' , 'true' ) ;
254
+ const href = link . getAttribute ( 'href' ) ;
255
+ link . addEventListener ( 'click' , ( e ) => {
256
+ e . preventDefault ( ) ;
257
+ const targetId = href ?. substring ( 1 ) || '' ;
258
+ const targetElement = document . getElementById ( targetId ) ;
259
+
260
+ if ( targetElement ) {
261
+ window . history . pushState ( null , '' , `${ location . pathname } ${ href } ` ) ;
262
+
263
+ scrollToElementTop ( targetElement ) ;
264
+ }
265
+ } ) ;
266
+ } ) ;
267
+
268
+ // 检查当前URL是否包含锚点,如果有,自动滚动到正确位置
269
+ if ( window . location . hash ) {
270
+ const { hash } = window . location ;
271
+ const targetElement = document . getElementById ( hash . substring ( 1 ) ) ;
272
+
273
+ if ( targetElement ) {
274
+ // 给浏览器一点时间来完成渲染
275
+ setTimeout ( ( ) => {
276
+ scrollToElementTop ( targetElement ) ;
277
+ } , 100 ) ;
278
+ }
279
+ }
280
+ } ;
281
+ fixFootnoteLinks ( ) ;
282
+
283
+ const observer = new MutationObserver ( ( ) => {
284
+ fixFootnoteLinks ( ) ;
285
+ } ) ;
286
+
287
+ observer . observe ( document . body , {
288
+ childList : true ,
289
+ subtree : true ,
290
+ attributes : true ,
291
+ attributeFilter : [ 'id' , 'href' ] ,
292
+ } ) ;
293
+
294
+ // 监听 URL hash 变化
295
+ const handleHashChange = ( ) => {
296
+ if ( window . location . hash ) {
297
+ const { hash } = window . location ;
298
+ const targetElement = document . getElementById ( hash . substring ( 1 ) ) ;
299
+
300
+ if ( targetElement ) {
301
+ setTimeout ( ( ) => {
302
+ scrollToElementTop ( targetElement ) ;
303
+ } , 100 ) ;
304
+ }
305
+ }
306
+ } ;
307
+
308
+ window . addEventListener ( 'hashchange' , handleHashChange ) ;
309
+
310
+ return ( ) => {
311
+ observer . disconnect ( ) ;
312
+ window . removeEventListener ( 'hashchange' , handleHashChange ) ;
313
+ } ;
314
+ } , [ location . pathname ] ) ;
315
+
245
316
return (
246
317
< Row className = "questionDetailPage pt-4 mb-5" >
247
318
< Col className = "page-main flex-auto" >
0 commit comments