@@ -10,14 +10,17 @@ import { initTabs } from './tabs'
1010import { initTocNav } from './toc-nav'
1111import 'htmx-ext-head-support'
1212import 'htmx-ext-preload'
13- import katex from 'katex'
13+ import * as katex from 'katex'
1414import { $ , $$ } from 'select-dom'
1515import { UAParser } from 'ua-parser-js'
1616
1717const { getOS } = new UAParser ( )
1818const isLazyLoadNavigationEnabled =
1919 $ ( 'meta[property="docs:feature:lazy-load-navigation"]' ) ?. content === 'true'
2020
21+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22+ type HtmxEvent = any
23+
2124/**
2225 * Initialize KaTeX math rendering for elements with class 'math'
2326 */
@@ -71,7 +74,7 @@ document.addEventListener('DOMContentLoaded', function () {
7174 initMath ( )
7275} )
7376
74- document . addEventListener ( 'htmx:load' , function ( event ) {
77+ document . addEventListener ( 'htmx:load' , function ( event : HtmxEvent ) {
7578 initTocNav ( )
7679 initHighlight ( )
7780 initCopyButton ( )
@@ -99,14 +102,17 @@ document.addEventListener('htmx:load', function (event) {
99102} )
100103
101104// Don't remove style tags because they are used by the elastic global nav.
102- document . addEventListener ( 'htmx:removingHeadElement' , function ( event ) {
103- const tagName = event . detail . headElement . tagName
104- if ( tagName === 'STYLE' ) {
105- event . preventDefault ( )
105+ document . addEventListener (
106+ 'htmx:removingHeadElement' ,
107+ function ( event : HtmxEvent ) {
108+ const tagName = event . detail . headElement . tagName
109+ if ( tagName === 'STYLE' ) {
110+ event . preventDefault ( )
111+ }
106112 }
107- } )
113+ )
108114
109- document . addEventListener ( 'htmx:beforeRequest' , function ( event ) {
115+ document . addEventListener ( 'htmx:beforeRequest' , function ( event : HtmxEvent ) {
110116 if (
111117 event . detail . requestConfig . verb === 'get' &&
112118 event . detail . requestConfig . triggeringEvent
@@ -126,60 +132,75 @@ document.addEventListener('htmx:beforeRequest', function (event) {
126132 }
127133} )
128134
129- document . body . addEventListener ( 'htmx:oobBeforeSwap' , function ( event ) {
130- // This is needed to scroll to the top of the page when the content is swapped
131- if (
132- event . target . id === 'main-container' ||
133- event . target . id === 'markdown-content' ||
134- event . target . id === 'content-container'
135- ) {
136- window . scrollTo ( 0 , 0 )
135+ document . body . addEventListener (
136+ 'htmx:oobBeforeSwap' ,
137+ function ( event : HtmxEvent ) {
138+ // This is needed to scroll to the top of the page when the content is swapped
139+ if (
140+ event . target ?. id === 'main-container' ||
141+ event . target ?. id === 'markdown-content' ||
142+ event . target ?. id === 'content-container'
143+ ) {
144+ window . scrollTo ( 0 , 0 )
145+ }
137146 }
138- } )
139-
140- document . body . addEventListener ( 'htmx:pushedIntoHistory' , function ( event ) {
141- const pagesNav = $ ( '#pages-nav' )
142- const currentNavItem = $$ ( '.current' , pagesNav )
143- currentNavItem . forEach ( ( el ) => {
144- el . classList . remove ( 'current' )
145- } )
146- const navItems = $$ ( 'a[href="' + event . detail . path + '"]' , pagesNav )
147- navItems . forEach ( ( navItem ) => {
148- navItem . classList . add ( 'current' )
149- } )
150- } )
151-
152- document . body . addEventListener ( 'htmx:responseError' , function ( event ) {
153- // If you get a 404 error while clicking on a hx-get link, actually open the link
154- // This is needed because the browser doesn't update the URL when the response is a 404
155- // In production, cloudfront handles serving the 404 page.
156- // Locally, the DocumentationWebHost handles it.
157- // On previews, a generic 404 page is shown.
158- if ( event . detail . xhr . status === 404 ) {
159- window . location . assign ( event . detail . pathInfo . requestPath )
147+ )
148+
149+ document . body . addEventListener (
150+ 'htmx:pushedIntoHistory' ,
151+ function ( event : HtmxEvent ) {
152+ const pagesNav = $ ( '#pages-nav' )
153+ const currentNavItem = $$ ( '.current' , pagesNav )
154+ currentNavItem . forEach ( ( el ) => {
155+ el . classList . remove ( 'current' )
156+ } )
157+ const navItems = $$ ( 'a[href="' + event . detail . path + '"]' , pagesNav )
158+ navItems . forEach ( ( navItem ) => {
159+ navItem . classList . add ( 'current' )
160+ } )
160161 }
161- } )
162+ )
163+
164+ document . body . addEventListener (
165+ 'htmx:responseError' ,
166+ function ( event : HtmxEvent ) {
167+ // If you get a 404 error while clicking on a hx-get link, actually open the link
168+ // This is needed because the browser doesn't update the URL when the response is a 404
169+ // In production, cloudfront handles serving the 404 page.
170+ // Locally, the DocumentationWebHost handles it.
171+ // On previews, a generic 404 page is shown.
172+ if ( event . detail . xhr . status === 404 ) {
173+ window . location . assign ( event . detail . pathInfo . requestPath )
174+ }
175+ }
176+ )
162177
163178// We add a query string to the get request to make sure the requested page is up to date
164- const docsBuilderVersion = $ ( 'body' ) . dataset . docsBuilderVersion
165- document . body . addEventListener ( 'htmx:configRequest' , function ( event ) {
166- if ( event . detail . verb === 'get' ) {
167- event . detail . parameters [ 'v' ] = docsBuilderVersion
179+ const docsBuilderVersion = $ ( 'body' ) ?. dataset . docsBuilderVersion
180+ document . body . addEventListener (
181+ 'htmx:configRequest' ,
182+ function ( event : HtmxEvent ) {
183+ if ( event . detail . verb === 'get' && docsBuilderVersion ) {
184+ event . detail . parameters [ 'v' ] = docsBuilderVersion
185+ }
168186 }
169- } )
187+ )
170188
171189// Here we need to strip the v parameter from the URL so
172190// that the browser doesn't show the v parameter in the address bar
173- document . body . addEventListener ( 'htmx:beforeHistoryUpdate' , function ( event ) {
174- const params = new URLSearchParams (
175- event . detail . history . path . split ( '?' ) [ 1 ] ?? ''
176- )
177- params . delete ( 'v' )
178- const pathWithoutQueryString = event . detail . history . path . split ( '?' ) [ 0 ]
179- if ( params . size === 0 ) {
180- event . detail . history . path = pathWithoutQueryString
181- } else {
182- event . detail . history . path =
183- pathWithoutQueryString + '?' + params . toString ( )
191+ document . body . addEventListener (
192+ 'htmx:beforeHistoryUpdate' ,
193+ function ( event : HtmxEvent ) {
194+ const params = new URLSearchParams (
195+ event . detail . history . path . split ( '?' ) [ 1 ] ?? ''
196+ )
197+ params . delete ( 'v' )
198+ const pathWithoutQueryString = event . detail . history . path . split ( '?' ) [ 0 ]
199+ if ( params . size === 0 ) {
200+ event . detail . history . path = pathWithoutQueryString
201+ } else {
202+ event . detail . history . path =
203+ pathWithoutQueryString + '?' + params . toString ( )
204+ }
184205 }
185- } )
206+ )
0 commit comments