@@ -333,8 +333,55 @@ $(function () {
333333 } catch ( e ) {
334334 console . log ( e ) ;
335335 }
336- } ) . on ( "click" , ".markdown-toc-list a" , function ( ) {
336+ } ) . on ( "click" , ".markdown-toc-list a" , function ( e ) {
337+ e . preventDefault ( ) ;
337338 var $this = $ ( this ) ;
339+ var href = $this . attr ( "href" ) ;
340+ if ( href ) {
341+ var name = href . replace ( / ^ # / , "" ) ;
342+ var target = null ;
343+ // 1. 按 name 属性查找
344+ var found = $ ( "a[name='" + name + "']" ) ;
345+ if ( found . length > 0 ) {
346+ target = found ;
347+ }
348+ // 2. 按 id 查找
349+ if ( ! target ) {
350+ var el = document . getElementById ( name ) ;
351+ if ( el ) target = $ ( el ) ;
352+ }
353+ // 3. 尝试带 h{n}- 前缀的id格式
354+ if ( ! target ) {
355+ $ ( "h1,h2,h3,h4,h5,h6" ) . each ( function ( ) {
356+ if ( this . id && this . id . replace ( / ^ h \d - / , "" ) === name ) {
357+ target = $ ( this ) ;
358+ return false ;
359+ }
360+ } ) ;
361+ }
362+ // 4. 按标题文本匹配(兼容旧文档无锚点的情况)
363+ if ( ! target ) {
364+ var linkText = $this . text ( ) . trim ( ) ;
365+ $ ( ".markdown-heading, h1, h2, h3, h4, h5, h6" , "#page-content" ) . each ( function ( ) {
366+ var headingText = $ ( this ) . clone ( ) . children ( "a, span" ) . remove ( ) . end ( ) . text ( ) . trim ( ) ;
367+ if ( ! headingText ) headingText = $ ( this ) . text ( ) . trim ( ) ;
368+ if ( headingText === linkText ) {
369+ target = $ ( this ) ;
370+ return false ;
371+ }
372+ } ) ;
373+ }
374+ if ( target && target . length > 0 ) {
375+ var container = $ ( ".manual-right" ) ;
376+ var scrollTo = target . offset ( ) . top - container . offset ( ) . top + container . scrollTop ( ) ;
377+ container . animate ( { scrollTop : scrollTo } , 200 ) ;
378+ if ( history . replaceState ) {
379+ history . replaceState ( null , null , href ) ;
380+ } else {
381+ location . hash = href ;
382+ }
383+ }
384+ }
338385 setTimeout ( function ( ) {
339386 $ ( ".markdown-toc-list li" ) . removeClass ( "directory-item-active" ) ;
340387 $this . parents ( "li" ) . addClass ( "directory-item-active" ) ;
0 commit comments