Skip to content

Commit 350125a

Browse files
authored
Add math directive (#2090)
* Add math directive * Prettify * Fix linter issue * Formatting * Title of doc
1 parent b12fdf1 commit 350125a

File tree

56 files changed

+1660
-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

+1660
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,67 @@ import { initTabs } from './tabs'
1010
import { initTocNav } from './toc-nav'
1111
import 'htmx-ext-head-support'
1212
import 'htmx-ext-preload'
13+
import katex from 'katex'
14+
import 'katex/dist/katex.min.css'
1315
import { $, $$ } from 'select-dom'
1416
import { UAParser } from 'ua-parser-js'
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 =
34+
element.tagName === 'DIV' ||
35+
content.includes('\\[') ||
36+
content.includes('$$') ||
37+
content.includes('\\begin{') ||
38+
content.includes('\n')
39+
40+
// Clean up common LaTeX delimiters
41+
const cleanContent = content
42+
.replace(/^\$\$|\$\$$/g, '') // Remove $$ delimiters
43+
.replace(/^\\\[|\\\]$/g, '') // Remove \[ \] delimiters
44+
.trim()
45+
46+
// Clear the element content before rendering
47+
element.innerHTML = ''
48+
49+
katex.render(cleanContent, element, {
50+
throwOnError: false,
51+
displayMode: isDisplayMath,
52+
strict: false, // Allow some LaTeX extensions
53+
trust: false, // Security: don't trust arbitrary commands
54+
output: 'mathml', // Only render MathML, not HTML
55+
macros: {
56+
// Add common macros if needed
57+
},
58+
})
59+
} catch (error) {
60+
console.warn('KaTeX rendering error:', error)
61+
// Fallback: keep the original content
62+
element.innerHTML = element.textContent || ''
63+
}
64+
})
65+
}
66+
2067
document.addEventListener('htmx:load', function (event) {
2168
initTocNav()
2269
initHighlight()
2370
initCopyButton()
2471
initTabs()
2572
initAppliesSwitch()
73+
initMath()
2674

2775
// We do this so that the navigation is not initialized twice
2876
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)