Skip to content

Add KaTeX math rendering support#2

Merged
Tnixc merged 3 commits intomainfrom
claude/add-katex-rendering-011CUp2ZjEPdEbpR648nzB2d
Nov 5, 2025
Merged

Add KaTeX math rendering support#2
Tnixc merged 3 commits intomainfrom
claude/add-katex-rendering-011CUp2ZjEPdEbpR648nzB2d

Conversation

@Tnixc
Copy link
Owner

@Tnixc Tnixc commented Nov 5, 2025

This pull request adds automatic KaTeX math rendering and CSS injection for pages that contain math expressions. It introduces a new katex_assets module to manage KaTeX usage tracking and conditional CSS injection, ensuring the KaTeX stylesheet is only included when needed. The PR also updates the binary name and adds the katex crate as a dependency.

KaTeX math rendering and CSS injection:

  • Added the katex crate as a dependency in Cargo.toml and created a new katex_assets module to track and manage KaTeX usage, CSS injection, and related messaging. [1] [2] [3]
  • Updated the markdown rendering pipeline in src/handlers/markdown.rs to detect and render math expressions using KaTeX, and to mark when KaTeX is used. Errors in math rendering are now handled with clear error messages. [1] [2] [3] [4]
  • Modified src/handlers/pages.rs and src/handlers/entries.rs to reset the KaTeX usage flag at the start of each page, and to conditionally inject the KaTeX CSS link into the HTML output only if math has been rendered and injection is not disabled by environment variable. [1] [2] [3] [4] [5] [6]

Other changes:

  • Renamed the binary output in Cargo.toml from replacer to simple.

claude and others added 3 commits November 5, 2025 03:39
Implements server-side LaTeX rendering using katex-rs when processing
markdown content. Supports both inline math ($...$) and display math
($$...$$) notation.

Features:
- Server-side rendering converts LaTeX to HTML at build time
- Auto-injects KaTeX CSS from CDN when math is detected
- Prints informative message during build
- Fails build on invalid LaTeX syntax with clear error messages
- Environment variable SIMPLE_DISABLE_KATEX_CSS to disable auto-injection

Implementation:
- Added katex dependency for LaTeX rendering
- Post-processes comrak HTML output to find and render math spans
- Injects KaTeX CSS link into page <head> when math is used
- Clean separation of concerns with katex_assets module
- Add KaTeX CSS injection to entries.rs for .md file processing
- Reset KaTeX flag at start of process_entry
- Inject KaTeX CSS when math is detected in entries
- Rename binary from "replacer" to "simple" in Cargo.toml

This ensures KaTeX works for both:
- HTML files with <markdown> blocks (pages.rs)
- .md files processed through templates (entries.rs)
@Tnixc Tnixc merged commit f3500b5 into main Nov 5, 2025
1 check passed
@Tnixc Tnixc deleted the claude/add-katex-rendering-011CUp2ZjEPdEbpR648nzB2d branch November 5, 2025 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants