@@ -393,6 +393,62 @@ class Theme {
393393 }
394394 }
395395
396+ initRelated ( ) {
397+ const $relatedStatic = document . getElementById ( 'related-static' ) ;
398+ if ( ! $relatedStatic ) return ;
399+
400+ if ( this . util . isTocStatic ( ) || $relatedStatic . getAttribute ( 'data-kept' ) === 'true' ) {
401+ if ( this . _relatedOnScroll ) this . scrollEventSet . delete ( this . _relatedOnScroll ) ;
402+ return ;
403+ }
404+
405+ const $relatedAuto = document . getElementById ( 'related-auto' ) ;
406+ const $relatedContent = document . getElementById ( 'related-content-static' ) ;
407+
408+ if ( $relatedContent && $relatedAuto ) {
409+ $relatedAuto . querySelector ( '.related-content' ) . appendChild ( $relatedContent ) ;
410+ } else {
411+ return ;
412+ }
413+
414+ const $toc = document . getElementById ( 'toc-auto' ) ;
415+
416+ if ( $toc && getComputedStyle ( $toc ) . visibility !== 'hidden' && getComputedStyle ( $toc ) . display !== 'none' ) {
417+ // Sychronize position with the TOC
418+ const tocRect = $toc . getBoundingClientRect ( ) ;
419+ $relatedAuto . style . top = `${ window . scrollY + tocRect . top } px` ;
420+ $relatedAuto . style . left = `20px` ;
421+ $relatedAuto . style . visibility = 'visible' ;
422+ } else {
423+ $relatedAuto . style . visibility = 'hidden' ;
424+ return ;
425+ }
426+
427+ const headerIsFixed = document . body . getAttribute ( 'data-header-desktop' ) !== 'normal' ;
428+ const headerHeight = document . getElementById ( 'header-desktop' ) . offsetHeight ;
429+ const TOP_SPACING = 20 + ( headerIsFixed ? headerHeight : 0 ) ;
430+ const minRelatedTop = $relatedAuto . offsetTop ;
431+ const minScrollTop = minRelatedTop - TOP_SPACING + ( headerIsFixed ? 0 : headerHeight ) ;
432+
433+ this . _relatedOnScroll = this . _relatedOnScroll || ( ( ) => {
434+ const footerTop = document . getElementById ( 'post-footer' ) . offsetTop ;
435+ const maxRelatedTop = footerTop - $relatedAuto . getBoundingClientRect ( ) . height ;
436+ const maxScrollTop = maxRelatedTop - TOP_SPACING + ( headerIsFixed ? 0 : headerHeight ) ;
437+ if ( this . newScrollTop < minScrollTop ) {
438+ $relatedAuto . style . position = 'absolute' ;
439+ $relatedAuto . style . top = `${ minRelatedTop } px` ;
440+ } else if ( this . newScrollTop > maxScrollTop ) {
441+ $relatedAuto . style . position = 'absolute' ;
442+ $relatedAuto . style . top = `${ maxRelatedTop } px` ;
443+ } else {
444+ $relatedAuto . style . position = 'fixed' ;
445+ $relatedAuto . style . top = `${ TOP_SPACING } px` ;
446+ }
447+ } ) ;
448+ this . _relatedOnScroll ( ) ;
449+ this . scrollEventSet . add ( this . _relatedOnScroll ) ;
450+ }
451+
396452 initToc ( ) {
397453 const $tocCore = document . getElementById ( 'TableOfContents' ) ;
398454 if ( $tocCore === null ) return ;
@@ -693,8 +749,12 @@ class Theme {
693749 this . _resizeTimeout = null ;
694750 for ( let event of this . resizeEventSet ) event ( ) ;
695751 this . initToc ( ) ;
696- this . initMermaid ( ) ;
697- this . initSearch ( ) ;
752+ this . initRelated ( ) ; // Ensure this call remains
753+ this . initComment ( ) ;
754+
755+ this . onScroll ( ) ;
756+ this . onResize ( ) ;
757+ this . onClickMask ( ) ;
698758 } , 100 ) ;
699759 }
700760 } , false ) ;
@@ -731,6 +791,7 @@ class Theme {
731791
732792 window . setTimeout ( ( ) => {
733793 this . initToc ( ) ;
794+ this . initRelated ( ) ; // Ensure this call remains
734795 this . initComment ( ) ;
735796
736797 this . onScroll ( ) ;
0 commit comments