@@ -19,75 +19,25 @@ export function enableScrollToTop() {
19
19
export function setupSidebar ( ) {
20
20
const h1Elements = document . querySelectorAll ( 'h1' ) ;
21
21
const h2Elements = document . querySelectorAll ( 'article h2' ) ;
22
- if ( h2Elements . length === 0 ) {
23
- // If there are no h2 headings, do not set up the sidebar
24
- return ;
25
- }
26
- const sidebar = document . querySelector ( '.sidebar' ) ;
27
- function appendSidebarItem ( textContent , tagName ) {
28
- const newItem = document . createElement ( 'a' ) ;
29
- newItem . textContent = textContent ;
30
- if ( tagName === 'h2' ) {
31
- newItem . classList . add ( 'sidebar-item-h2' , 'text-gray-500' , 'hover:text-green-400' , 'ml-4' , 'cursor-pointer' ) ;
32
- }
33
- sidebar . appendChild ( newItem ) ;
34
22
35
- newItem . addEventListener ( 'click' , function ( ) {
36
- const allSidebarItems = document . querySelectorAll ( '.sidebar a' ) ;
37
- allSidebarItems . forEach ( function ( item ) {
38
- item . style . color = '' ; // Reset color
39
- item . style . fontWeight = '' ;
40
- } ) ;
41
-
42
- // Change color of clicked item
43
- newItem . style . color = '#34D399' ;
44
- newItem . style . fontWeight = 'bold' ;
45
-
46
- const currentVersion = newItem . textContent ;
47
- const headings = document . querySelectorAll ( 'h2' ) ;
48
- let targetElement = null ;
49
- headings . forEach ( function ( heading ) {
50
- if ( heading . textContent . trim ( ) === currentVersion . trim ( ) ) {
51
- targetElement = heading ;
52
- }
53
- } ) ;
54
- if ( targetElement ) {
55
- window . scrollTo ( 0 , targetElement . offsetTop - 100 ) ;
56
- }
57
- } ) ;
23
+ if ( h2Elements . length == 0 ) {
24
+ return ;
58
25
}
59
26
60
- const sidebarHeading = document . createElement ( 'div' ) ;
61
- sidebarHeading . textContent = 'ON THIS PAGE' ;
62
- sidebarHeading . classList . add ( 'sidebar-heading' ) ;
63
- sidebarHeading . style . color = '#34D399' ;
64
- sidebarHeading . style . fontSize = '15px' ;
65
- sidebarHeading . style . fontWeight = 'bold' ;
66
- sidebarHeading . style . paddingBottom = '15px' ;
67
- sidebar . appendChild ( sidebarHeading ) ;
27
+ h1Elements . forEach ( function ( element ) {
28
+ appendSidebarItem ( element . textContent , 'h1' ) ;
29
+ } ) ;
68
30
69
31
h2Elements . forEach ( function ( element ) {
70
32
appendSidebarItem ( element . textContent , 'h2' ) ;
71
33
} ) ;
72
-
34
+
73
35
const h2Items = document . querySelectorAll ( '.sidebar-item-h2' ) ;
74
36
h2Items . forEach ( item => {
75
- item . style . paddingTop = '5px ' ;
37
+ item . style . paddingTop = '10px ' ;
76
38
} ) ;
77
39
}
78
40
79
- export function saveAndRestoreNavigationPosition ( ) {
80
- var scrollPosition = sessionStorage . getItem ( 'scrollPosition' ) ;
81
- if ( scrollPosition !== undefined ) {
82
- document . querySelector ( '#navigation-sidebar' ) . scrollTop = parseInt ( scrollPosition ) ;
83
- }
84
- document . querySelector ( '#navigation-sidebar' ) . addEventListener ( 'click' , function ( ) {
85
- var scrollPosition = document . querySelector ( '#navigation-sidebar' ) . scrollTop ;
86
- sessionStorage . setItem ( 'scrollPosition' , scrollPosition ) ;
87
- console . log ( 'clicked' )
88
- } ) ;
89
- } ;
90
-
91
41
function appendSidebarItem ( textContent , tagName ) {
92
42
const sidebar = document . querySelector ( '.sidebar' ) ;
93
43
const newItem = document . createElement ( 'a' ) ;
@@ -101,6 +51,19 @@ function appendSidebarItem(textContent, tagName) {
101
51
sidebar . appendChild ( newItem ) ;
102
52
103
53
newItem . addEventListener ( 'click' , function ( ) {
54
+ const allSidebarItems = document . querySelectorAll ( '.sidebar a' ) ;
55
+ allSidebarItems . forEach ( function ( item ) {
56
+ item . style . color = '' ; // Reset color
57
+ item . style . fontWeight = '' ;
58
+ } ) ;
59
+
60
+ // Change color of clicked item
61
+ if ( tagName == 'h2' ) {
62
+ newItem . style . color = '#34D399' ;
63
+ newItem . style . fontWeight = 'bold' ;
64
+ }
65
+
66
+
104
67
const currentVersion = newItem . textContent ;
105
68
const headings = document . querySelectorAll ( 'h2' ) ;
106
69
let targetElement = null ;
@@ -112,5 +75,17 @@ function appendSidebarItem(textContent, tagName) {
112
75
if ( targetElement ) {
113
76
window . scrollTo ( 0 , targetElement . offsetTop - 100 ) ;
114
77
}
115
- } ) ;
78
+ } ) ;
116
79
}
80
+
81
+ export function saveAndRestoreNavigationPosition ( ) {
82
+ var scrollPosition = sessionStorage . getItem ( 'scrollPosition' ) ;
83
+ if ( scrollPosition !== undefined ) {
84
+ document . querySelector ( '#navigation-sidebar' ) . scrollTop = parseInt ( scrollPosition ) ;
85
+ }
86
+ document . querySelector ( '#navigation-sidebar' ) . addEventListener ( 'click' , function ( ) {
87
+ var scrollPosition = document . querySelector ( '#navigation-sidebar' ) . scrollTop ;
88
+ sessionStorage . setItem ( 'scrollPosition' , scrollPosition ) ;
89
+ console . log ( 'clicked' )
90
+ } ) ;
91
+ } ;
0 commit comments