95
95
} ) ;
96
96
}
97
97
} ) ;
98
+
99
+ /* Expand Anchors Update */
100
+
101
+ document . addEventListener ( "DOMContentLoaded" , initDrawer ) ;
102
+
103
+ function initDrawer ( ) {
104
+ // Ensure all drawers start collapsed
105
+
106
+ // Listen for clicks on anchor links within the page
107
+ document . addEventListener ( "click" , function ( event ) {
108
+ const anchor = event . target . closest ( "a[href]" ) ;
109
+ if ( anchor && ! isInsideH2 ( anchor ) ) {
110
+ // Extract target ID from href (e.g., #emdash -> emdash)
111
+ const targetId = anchor . getAttribute ( "href" ) . replace ( "#" , "" ) ;
112
+ if ( targetId ) {
113
+ // Find the element with the matching ID
114
+ const targetElement = document . getElementById ( targetId ) ;
115
+ if ( targetElement ) {
116
+ // Expand only the specific drawer related to the target
117
+ expandSingleDrawer ( targetElement ) ;
118
+ }
119
+ }
120
+ }
121
+ } ) ;
122
+ }
123
+
124
+ function resetDrawerStyles ( ) {
125
+ // Ensure all <h2> elements start without the "expand" class
126
+ document . querySelectorAll ( ".drawer h2.expand" ) . forEach ( h2 => {
127
+ h2 . classList . remove ( "expand" ) ;
128
+ } ) ;
129
+
130
+ // Ensure all <div> elements containing matched IDs start with display: none;
131
+ document . querySelectorAll ( ".drawer h2 + div" ) . forEach ( div => {
132
+ div . style . display = "none" ;
133
+ } ) ;
134
+ }
135
+
136
+ function expandSingleDrawer ( targetElement ) {
137
+ // Step 3: Find the parent <div> that contains the target ID
138
+ const parentDiv = targetElement . closest ( "div" ) ;
139
+
140
+ if ( parentDiv ) {
141
+ // Step 4: Find the <h2> immediately before this <div> within the same .drawer
142
+ let h2 = null ;
143
+ let sibling = parentDiv . previousElementSibling ;
144
+
145
+ while ( sibling ) {
146
+ if ( sibling . tagName . toLowerCase ( ) === "h2" ) {
147
+ h2 = sibling ;
148
+ break ;
149
+ }
150
+ sibling = sibling . previousElementSibling ;
151
+ }
152
+
153
+ if ( h2 ) {
154
+ // Step 5: Apply the changes
155
+ h2 . classList . add ( "expand" ) ; // Add the "expand" class
156
+ parentDiv . style . display = "block" ; // Show the <div>
157
+ }
158
+ }
159
+ }
160
+
161
+ function isInsideH2 ( element ) {
162
+ // Check if the element is inside an <h2> (to prevent accidental expansion)
163
+ return element . closest ( "h2" ) !== null ;
164
+ }
0 commit comments