fix(docs-viewer): Add LaTex math formula rendering via KaTeX

Add client-side LaTeX formula rendering to the Docs Viewer and implement
a parser-safe server-side math stashing mechanism.

Previously, LaTeX math expressions ($ .. $, $$ .. $$) were underdered.
Protecting math blocks with underscore-based placeholders (e.g.,
___MATH_BLOCK_37___) caused Python Markdown to parse the underscores
into emphasis tags, displaying the literal placeholder text (e.g.,
_MATHBLOCK_37_) in the output HTML.

This commit switches the staching mechanism to pure alphanumeric tokens
(e.g., MATHBLOCKTOKEN{index}END), safely bypassing the Markdown parser
before rendering the restored formulas via KaTex

- Integrate KaTeX CSS and auto-render JS in layout.html
- Add server-side math stashing with alphanumeric tokens in main.py
- Mount ./app volume in docker-compose.yml for live code updates

:wa
This commit is contained in:
kiskaadee committed 2026-07-20 11:31:48 -05:00
1 parent 9821054fb2
commit 50d7ed3ef3
3 files changed
+58

No files matched your search

+18
View File
@@ -10,6 +10,8 @@
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Outfit:wght@400;500;600;700&family=Fira+Code:wght@400;500&display=swap" rel="stylesheet">
<!-- Prism Syntax Highlighting -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" id="prism-theme">
<!-- KaTeX Math Highlighting -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css">
<style>
:root {
/* Dark Theme Variables (Default) */
@@ -771,6 +773,9 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<!-- Mermaid.js -->
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<!-- KaTeX JS -->
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js"></script>
<script>
// Hamburger Menu Toggle
@@ -852,6 +857,19 @@
});
mermaid.run();
}
// Render KaTeX Math
if (window.renderMathInElement) {
renderMathInElement(document.body, {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '$', right: '$', display: false},
{left: '\\(', right: '\\)', display: false},
{left: '\\[', right: '\\]', display: true}
],
throwOnError: false
});
}
});
// Search Input Filter
+39
View File
@@ -164,6 +164,43 @@ def process_alerts(html):
return pattern.sub(replace_alert, html)
def preserve_math(text):
"""Protect LaTeX math blocks ($...$ and $$...$$) from being altered by Markdown parser."""
math_blocks = []
# Stash inline code and code blocks first to avoid stashing math inside code blocks
code_blocks = []
def stash_code(m):
code_blocks.append(m.group(0))
return f"CODEBLOCKTOKEN{len(code_blocks)-1}END"
# Match ``` ``` code blocks and ` ` inline code
text = re.sub(r'```.*?```', stash_code, text, flags=re.DOTALL)
text = re.sub(r'`[^`\n]+`', stash_code, text)
def stash_math(m):
math_blocks.append(m.group(0))
return f"MATHBLOCKTOKEN{len(math_blocks)-1}END"
# Match display math $$...$$
text = re.sub(r'\$\$.*?\$\$', stash_math, text, flags=re.DOTALL)
# Match inline math $...$ (ensuring single $ on same line, not empty)
text = re.sub(r'(?<!\\)\$([^\$\n]+?)(?<!\\)\$', stash_math, text)
# Restore code blocks back to markdown text
for i, code in enumerate(code_blocks):
text = text.replace(f"CODEBLOCKTOKEN{i}END", code)
return text, math_blocks
def restore_math(html, math_blocks):
"""Restore stashed math blocks after Markdown conversion."""
for i, block in enumerate(math_blocks):
html = html.replace(f"MATHBLOCKTOKEN{i}END", block)
return html
def generate_folder_index(folder_path, rel_url_path):
"""Renders a directory list when no index README is present."""
try:
@@ -276,8 +313,10 @@ class DocsHTTPHandler(BaseHTTPRequestHandler):
return
# Render Markdown
md_content, math_blocks = preserve_math(md_content)
md = markdown.Markdown(extensions=['extra', 'toc', 'nl2br'])
html_content = md.convert(md_content)
html_content = restore_math(html_content, math_blocks)
html_content = process_alerts(html_content)
toc_html = md.toc
+1
View File
@@ -5,6 +5,7 @@ services:
restart: unless-stopped
volumes:
- ${PROJECT_PATH:-./data}:/docs:ro
- ./app:/app:ro
environment:
- DOCS_ROOT=/docs
- PORT=8000