-
-
Notifications
You must be signed in to change notification settings - Fork 70
fix: duplicate <html>/<head> tags in blog
#50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: duplicate <html>/<head> tags in blog
#50
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hi @dennexequiel - Will you able to attach a screengrab of math with and without katex css after removing |
Hi @Ankitz007, Sure! I've added the with/without KaTeX CSS screenshots to the PR description. |
|
Awesome! How about just using |
src/components/PostHead.astro
Outdated
|
|
||
| {usingKatex && ( | ||
| <link | ||
| href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I originally had |
|
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 |
|
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. |
|
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)
)
} |
cc95e9c to
2d4017e
Compare
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. |
Hey @Ankitz007, thanks for the suggestion! A regex-based check on the raw Markdown/MDX content could work, but it has a few drawbacks:
For this reason, I'm sticking with runtime .katex detection. It's simple, safe, and avoids unnecessary parsing while achieving the same result. |
<html>/<head> tags in blog
This PR addresses #49.
The issue
The project previously used
rehypeDocumentin the Astro config to inject the KaTeX CSS. However,rehypeDocumentoutputs 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
rehype-katexrender unstyled or broken.rehypeDocumentfixed styling but introduced invalid markup (extra<head>).The fix
rehypeDocumentfrom the Astro config andpackage.json.Layout.astroonly on pages containing math content (.katex).Preview
With KaTeX CSS

Without KaTeX CSS
