Skip to content

Commit 1de7b5e

Browse files
committed
Add math directive
1 parent b12fdf1 commit 1de7b5e

File tree

56 files changed

+509
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+509
-3
lines changed

docs/_docset.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ toc:
102102
- file: icons.md
103103
- file: images.md
104104
- file: kbd.md
105+
- file: math.md
105106
- file: lists.md
106107
- file: line_breaks.md
107108
- file: links.md

docs/syntax/directives.md

Lines changed: 1 addition & 0 deletions

docs/syntax/math.md

Lines changed: 141 additions & 0 deletions

src/Elastic.Documentation.Site/Assets/main.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,64 @@ import 'htmx-ext-head-support'
1212
import 'htmx-ext-preload'
1313
import { $, $$ } from 'select-dom'
1414
import { UAParser } from 'ua-parser-js'
15+
import katex from 'katex'
16+
import 'katex/dist/katex.min.css'
1517

1618
const { getOS } = new UAParser()
1719
const isLazyLoadNavigationEnabled =
1820
$('meta[property="docs:feature:lazy-load-navigation"]')?.content === 'true'
1921

22+
/**
23+
* Initialize KaTeX math rendering for elements with class 'math'
24+
*/
25+
function initMath() {
26+
const mathElements = $$('.math')
27+
mathElements.forEach(element => {
28+
try {
29+
const content = element.textContent?.trim()
30+
if (!content) return
31+
32+
// Determine if this is display math based on content and element type
33+
const isDisplayMath = element.tagName === 'DIV' ||
34+
content.includes('\\[') ||
35+
content.includes('$$') ||
36+
content.includes('\\begin{') ||
37+
content.includes('\n')
38+
39+
// Clean up common LaTeX delimiters
40+
let cleanContent = content
41+
.replace(/^\$\$|\$\$$/g, '') // Remove $$ delimiters
42+
.replace(/^\\\[|\\\]$/g, '') // Remove \[ \] delimiters
43+
.trim()
44+
45+
// Clear the element content before rendering
46+
element.innerHTML = ''
47+
48+
katex.render(cleanContent, element, {
49+
throwOnError: false,
50+
displayMode: isDisplayMath,
51+
strict: false, // Allow some LaTeX extensions
52+
trust: false, // Security: don't trust arbitrary commands
53+
output: 'mathml', // Only render MathML, not HTML
54+
macros: {
55+
// Add common macros if needed
56+
}
57+
})
58+
} catch (error) {
59+
console.warn('KaTeX rendering error:', error)
60+
// Fallback: keep the original content
61+
element.innerHTML = element.textContent || ''
62+
}
63+
})
64+
}
65+
2066
document.addEventListener('htmx:load', function (event) {
2167
initTocNav()
2268
initHighlight()
2369
initCopyButton()
2470
initTabs()
2571
initAppliesSwitch()
72+
initMath()
2673

2774
// We do this so that the navigation is not initialized twice
2875
if (isLazyLoadNavigationEnabled) {

src/Elastic.Documentation.Site/Assets/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,7 @@ body {
224224
user-select: none;
225225
pointer-events: none;
226226
}
227+
228+
math {
229+
margin-top: 1.5rem;
230+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)