File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -677,6 +677,16 @@ async function showDetail(num) {
677677 safeHTML ( $ ( '#hip-content' ) , rendered ) ;
678678 applyRainbowIndent ( $ ( '#hip-content' ) ) ;
679679
680+ // Intercept in-body anchor links (e.g. [see Motivation](#motivation)) so they
681+ // scroll within the detail view instead of triggering handleRoute
682+ $ ( '#hip-content' ) . addEventListener ( 'click' , e => {
683+ const a = e . target . closest ( 'a[href^="#"]' ) ;
684+ if ( ! a ) return ;
685+ const id = a . getAttribute ( 'href' ) . slice ( 1 ) ;
686+ const target = document . getElementById ( id ) ;
687+ if ( target ) { e . preventDefault ( ) ; target . scrollIntoView ( { behavior : 'smooth' } ) ; }
688+ } ) ;
689+
680690 // Render any Mermaid diagrams
681691 const mermaidEls = $ ( '#hip-content' ) . querySelectorAll ( '.mermaid' ) ;
682692 if ( mermaidEls . length ) {
@@ -779,6 +789,16 @@ function buildTOC() {
779789 html += `<li class="${ lvl } "><a href="#${ id } " data-toc-target="${ id } ">${ h . textContent } </a></li>` ;
780790 } ) ;
781791 safeHTML ( tocList , html ) ;
792+
793+ // Intercept TOC clicks to scroll without changing the hash,
794+ // which would trigger handleRoute and navigate away from the detail view
795+ tocList . addEventListener ( 'click' , e => {
796+ const link = e . target . closest ( 'a[data-toc-target]' ) ;
797+ if ( ! link ) return ;
798+ e . preventDefault ( ) ;
799+ const target = document . getElementById ( link . dataset . tocTarget ) ;
800+ if ( target ) target . scrollIntoView ( { behavior : 'smooth' } ) ;
801+ } ) ;
782802}
783803
784804function setupScrollSpy ( ) {
You can’t perform that action at this time.
0 commit comments