Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@dennexequiel
Copy link
Contributor

@dennexequiel dennexequiel commented Sep 3, 2025

This PR addresses #49.

The issue

The project previously used rehypeDocument in the Astro config to inject the KaTeX CSS. However, rehypeDocument outputs a full HTML document (with <html> and <head> tags) around Markdown/MDX content. This caused duplicate <html> and <head> tags when rendering pages.

Why it matters

  • Without KaTeX CSS, math expressions from rehype-katex render unstyled or broken.
  • Using rehypeDocument fixed styling but introduced invalid markup (extra <head>).

The fix

  1. Removed rehypeDocument from the Astro config and package.json.
  2. Conditionally inject the KaTeX CSS at runtime in Layout.astro only on pages containing math content (.katex).

Preview

With KaTeX CSS
Image

Without KaTeX CSS
Image

@vercel
Copy link

vercel bot commented Sep 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
astro-erudite Ready Ready Preview Comment Sep 4, 2025 3:53am

@Ankitz007
Copy link
Contributor

Hi @dennexequiel - Will you able to attach a screengrab of math with and without katex css after removing rehypeDocument? Will be helpful in case someone can't run it locally.

@dennexequiel
Copy link
Contributor Author

Hi @dennexequiel - Will you able to attach a screengrab of math with and without katex css after removing rehypeDocument? Will be helpful in case someone can't run it locally.

Hi @Ankitz007,

Sure! I've added the with/without KaTeX CSS screenshots to the PR description.

@Ankitz007
Copy link
Contributor

Ankitz007 commented Sep 3, 2025

Awesome! How about just using math or katex instead of usingKatex? Rest of it looks good to me. 👍🏻


{usingKatex && (
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should put it into the SITE config maybe instead of using magic links? Just following the pattern in other declarations. Maybe something like SITE.katex_css_href?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should go in SITE const because that would make it look like something template users are supposed to configure, when in reality the CSS version is tied to the rehype-katex dependency. If someone changed it there, it could easily break things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean not exactly into SITE but something like that. Just to stick with the style with the code around it.

@dennexequiel
Copy link
Contributor Author

Awesome! How about just using math or katex instead of usingKatex? Rest of it looks good to me. 👍🏻

I originally had katex, but before opening the PR I changed it to usingKatex for clarity. Since the flag's purpose right now is just to conditionally include KaTeX's CSS, either works. math would make more sense once we make it library-agnostic. I'll defer to @jktrn .

@jktrn
Copy link
Owner

jktrn commented Sep 3, 2025

Is there any way that we can automatically detect katex usage rather than hard coding it into frontmatter? I think it'd be more elegant

@jktrn
Copy link
Owner

jktrn commented Sep 3, 2025

Or any other way to just not necessitate a flag, it feels contrived

@dennexequiel
Copy link
Contributor Author

Or any other way to just not necessitate a flag, it feels contrived

I'll look into it and see if we can avoid using a flag.

@Ankitz007
Copy link
Contributor

How about using a regex based inference?

/**
 * Detects if content contains mathematical expressions that require KaTeX
 * @param content Raw markdown/MDX content string
 * @returns true if math expressions are found
 */
export function containsMathExpressions(content: string): boolean {
  // Check for inline math: $...$ (but not $$...$$)
  const inlineMathRegex = /(?<!\$)\$(?!\$)[^$\n]+?\$(?!\$)/g

  // Check for display math: $$...$$ (can span multiple lines)
  const displayMathRegex = /\$\$[\s\S]*?\$\$/g

  // Check for LaTeX-style delimiters: \(...\) for inline, \[...\] for display
  const latexInlineRegex = /\\\([^)]*\\\)/g
  const latexDisplayRegex = /\\\[[\s\S]*?\\\]/g

  return (
    inlineMathRegex.test(content) ||
    displayMathRegex.test(content) ||
    latexInlineRegex.test(content) ||
    latexDisplayRegex.test(content)
  )
}

@dennexequiel
Copy link
Contributor Author

Is there any way that we can automatically detect katex usage rather than hard coding it into frontmatter? I think it'd be more elegant

Hi @jktrn,

I've updated my commits and the PR description. Now, the KaTeX CSS is injected only when the .katex class name is present. It's a simple, lightweight approach that works the same. Let me know if this looks good.

@dennexequiel
Copy link
Contributor Author

How about using a regex based inference?

/**
 * Detects if content contains mathematical expressions that require KaTeX
 * @param content Raw markdown/MDX content string
 * @returns true if math expressions are found
 */
export function containsMathExpressions(content: string): boolean {
  // Check for inline math: $...$ (but not $$...$$)
  const inlineMathRegex = /(?<!\$)\$(?!\$)[^$\n]+?\$(?!\$)/g

  // Check for display math: $$...$$ (can span multiple lines)
  const displayMathRegex = /\$\$[\s\S]*?\$\$/g

  // Check for LaTeX-style delimiters: \(...\) for inline, \[...\] for display
  const latexInlineRegex = /\\\([^)]*\\\)/g
  const latexDisplayRegex = /\\\[[\s\S]*?\\\]/g

  return (
    inlineMathRegex.test(content) ||
    displayMathRegex.test(content) ||
    latexInlineRegex.test(content) ||
    latexDisplayRegex.test(content)
  )
}

Hey @Ankitz007, thanks for the suggestion! A regex-based check on the raw Markdown/MDX content could work, but it has a few drawbacks:

  • It adds extra processing at build or runtime since the entire content must be scanned.
  • It's easy to miss edge cases, like escaped $ signs or multiline LaTeX expressions, which can lead to false positives or negatives.
  • Our current approach checks for the .katex class in the rendered HTML. It's lightweight and works seamlessly with SSG or Islands architecture.

For this reason, I'm sticking with runtime .katex detection. It's simple, safe, and avoids unnecessary parsing while achieving the same result.

@jktrn jktrn merged commit ef90987 into jktrn:main Sep 7, 2025
2 checks passed
@jktrn jktrn changed the title Fix Duplicate <html>/<head> Tags in Blogs fix: duplicate <html>/<head> tags in blog Sep 30, 2025
@jktrn jktrn self-assigned this Oct 1, 2025
@jktrn jktrn added the bug Something isn't working label Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants