-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmain.ts
More file actions
213 lines (192 loc) · 7.17 KB
/
main.ts
File metadata and controls
213 lines (192 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { initApiDocs } from './api-docs'
import { initAppliesSwitch } from './applies-switch'
import { config } from './config'
import { initCopyButton } from './copybutton'
import { initHighlight } from './hljs'
import { initImageCarousel } from './image-carousel'
import { initMermaid } from './mermaid'
import { openDetailsWithAnchor } from './open-details-with-anchor'
import { initNav } from './pages-nav'
import { initSmoothScroll } from './smooth-scroll'
import { initTabs } from './tabs'
import { initializeOtel } from './telemetry/instrumentation'
import { initTocNav } from './toc-nav'
import 'htmx-ext-head-support'
import 'htmx-ext-preload'
import * as katex from 'katex'
import { $, $$ } from 'select-dom'
import { UAParser } from 'ua-parser-js'
// Injected at build time from MinVer
const DOCS_BUILDER_VERSION =
process.env.DOCS_BUILDER_VERSION?.trim() ?? '0.0.0-dev'
// Initialize OpenTelemetry FIRST, before any other code runs (when enabled)
// This must happen early so all subsequent code is instrumented
if (config.telemetryEnabled) {
initializeOtel({
serviceName: config.serviceName,
serviceVersion: DOCS_BUILDER_VERSION,
baseUrl: config.rootPath,
debug: false,
})
}
// Dynamically import web components after telemetry is initialized.
// Parcel code-splits these into separate chunks loaded on demand.
import('./web-components/NavigationSearch/NavigationSearchComponent')
import('./web-components/AskAi/AskAi')
import('./web-components/VersionDropdown')
import('./web-components/AppliesToPopover')
import('./web-components/FullPageSearch/FullPageSearchComponent')
import('./web-components/Diagnostics/DiagnosticsComponent')
import('./web-components/VectorSizingCalculator/VectorSizingCalculatorComponent')
const { getOS } = new UAParser()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type HtmxEvent = any
/**
* Initialize KaTeX math rendering for elements with class 'math'
*/
function initMath() {
const mathElements = $$('.math:not([data-katex-processed])')
mathElements.forEach((element) => {
try {
const content = element.textContent?.trim()
if (!content) return
// Determine if this is display math based on content and element type
const isDisplayMath =
element.tagName === 'DIV' ||
content.includes('\\[') ||
content.includes('$$') ||
content.includes('\\begin{') ||
content.includes('\n')
// Clean up common LaTeX delimiters
const cleanContent = content
.replace(/^\$\$|\$\$$/g, '') // Remove $$ delimiters
.replace(/^\\\[|\\\]$/g, '') // Remove \[ \] delimiters
.trim()
// Clear the element content before rendering
element.innerHTML = ''
katex.render(cleanContent, element, {
throwOnError: false,
displayMode: isDisplayMath,
strict: false, // Allow some LaTeX extensions
trust: false, // Security: don't trust arbitrary commands
output: 'mathml', // Only render MathML, not HTML
macros: {
// Add common macros if needed
},
})
// Mark as processed to prevent double processing
element.setAttribute('data-katex-processed', 'true')
} catch (error) {
console.warn('KaTeX rendering error:', error)
// Fallback: keep the original content
element.innerHTML = element.textContent || ''
}
})
}
// Initialize on initial page load
document.addEventListener('DOMContentLoaded', function () {
initMath()
initMermaid()
})
document.addEventListener('htmx:load', function () {
initTocNav()
initHighlight()
initCopyButton()
initTabs()
initAppliesSwitch()
initMath()
initMermaid()
initNav()
initSmoothScroll()
openDetailsWithAnchor()
initImageCarousel()
initApiDocs()
const urlParams = new URLSearchParams(window.location.search)
const editParam = urlParams.has('edit')
if (editParam) {
$('.edit-this-page.hidden')?.classList.remove('hidden')
}
})
// Don't remove style tags because they are used by the elastic global nav.
document.addEventListener(
'htmx:removingHeadElement',
function (event: HtmxEvent) {
const tagName = event.detail.headElement.tagName
if (tagName === 'STYLE') {
event.preventDefault()
}
}
)
document.addEventListener('htmx:beforeRequest', function (event: HtmxEvent) {
if (
event.detail.requestConfig.verb === 'get' &&
event.detail.requestConfig.triggeringEvent
) {
const { ctrlKey, metaKey, shiftKey }: PointerEvent =
event.detail.requestConfig.triggeringEvent
const { name: os } = getOS()
const modifierKey: boolean = os === 'macOS' ? metaKey : ctrlKey
if (shiftKey || modifierKey) {
event.preventDefault()
window.open(
event.detail.requestConfig.path,
'_blank',
'noopener,noreferrer'
)
}
}
})
document.body.addEventListener(
'htmx:oobBeforeSwap',
function (event: HtmxEvent) {
// Scroll to the top of the page when the content is swapped
if (
event.target?.id === 'main-container' ||
event.target?.id === 'markdown-content' ||
event.target?.id === 'content-container'
) {
window.scrollTo(0, 0)
}
}
)
document.body.addEventListener(
'htmx:responseError',
function (event: HtmxEvent) {
// If you get a 404 error while clicking on a hx-get link, actually open the link
// This is needed because the browser doesn't update the URL when the response is a 404
// In production, cloudfront handles serving the 404 page.
// Locally, the DocumentationWebHost handles it.
// On previews, a generic 404 page is shown.
if (event.detail.xhr.status === 404) {
window.location.assign(event.detail.pathInfo.requestPath)
}
}
)
// We add a query string to the get request to make sure the requested page is up to date
const docsBuilderVersion = $('body')?.dataset.docsBuilderVersion
document.body.addEventListener(
'htmx:configRequest',
function (event: HtmxEvent) {
if (event.detail.verb === 'get' && docsBuilderVersion) {
event.detail.parameters['v'] = docsBuilderVersion
}
}
)
// Here we need to strip the v parameter from the URL so
// that the browser doesn't show the v parameter in the address bar
document.body.addEventListener(
'htmx:beforeHistoryUpdate',
function (event: HtmxEvent) {
const params = new URLSearchParams(
event.detail.history.path.split('?')[1] ?? ''
)
params.delete('v')
const pathWithoutQueryString = event.detail.history.path.split('?')[0]
if (params.size === 0) {
event.detail.history.path = pathWithoutQueryString
} else {
event.detail.history.path =
pathWithoutQueryString + '?' + params.toString()
}
}
)