Skip to content
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

Add math rendering to posts using KaTeX #184

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Astro Cactus is a simple opinionated starter built with the Astro framework. Use
- Auto-generated [sitemap](https://docs.astro.build/en/guides/integrations-guide/sitemap/)
- [Pagefind](https://pagefind.app/) static search library integration
- [Astro Icon](https://github.com/natemoo-re/astro-icon) svg icon component
- [KaTeX](https://katex.org/) integration for math rendering

## Demo 💻

Expand Down
9 changes: 8 additions & 1 deletion astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import fs from "fs";
import mdx from "@astrojs/mdx";
import tailwind from "@astrojs/tailwind";
import sitemap from "@astrojs/sitemap";
import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";
import remarkUnwrapImages from "remark-unwrap-images";
import rehypeExternalLinks from "rehype-external-links";
import { remarkReadingTime } from "./src/utils/remark-reading-time";
Expand All @@ -12,9 +14,14 @@ export default defineConfig({
// ! Please remember to replace the following site property with your own domain
site: "https://astro-cactus.chriswilliams.dev/",
markdown: {
remarkPlugins: [remarkUnwrapImages, remarkReadingTime],
remarkPlugins: [
remarkUnwrapImages,
remarkReadingTime,
[remarkMath, { singleDollarTextMath: false }],
],
rehypePlugins: [
[rehypeExternalLinks, { target: "_blank", rel: ["nofollow, noopener, noreferrer"] }],
rehypeKatex,
],
remarkRehype: { footnoteLabelProperties: { className: [""] } },
shikiConfig: {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"astro": "4.0.3",
"astro-icon": "^0.8.2",
"rehype-external-links": "^3.0.0",
"rehype-katex": "^7.0.0",
"remark-math": "^6.0.0",
"satori": "0.10.11",
"satori-html": "^0.3.2",
"sharp": "^0.33.0"
Expand Down
104 changes: 104 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/content/post/markdown-elements/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ Start numbering with offset:
57. foo
1. bar

## Math

An example in display mode:

$$
\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}
$$

An example of $$\LaTeX$$ inline with text $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$

## Code

Inline `code`
Expand Down
2 changes: 2 additions & 0 deletions src/layouts/BlogPost.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import BlogHero from "@/components/blog/Hero";
import TOC from "@/components/blog/TOC";
import WebMentions from "@/components/blog/webmentions/index";

import "@/katex"; // KaTeX css file

interface Props {
post: CollectionEntry<"post">;
}
Expand Down
9 changes: 7 additions & 2 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const cactusTech: Array<{ title: string; desc: string; href: string }> = [
desc: "Markdown for the component era.",
href: "https://mdxjs.com/",
},
{
title: "KaTeX",
desc: "The fastest math typesetting library for the web.",
href: "https://katex.org/",
},
{
title: "Satori",
desc: "Generating png Open Graph images for blog posts.",
Expand Down Expand Up @@ -87,8 +92,8 @@ const cactusTech: Array<{ title: string; desc: string; href: string }> = [
class="cactus-link inline-block"
>
{title}
</a>:
<p class="inline-block sm:mt-2">{desc}</p>
</a>
:<p class="inline-block sm:mt-2">{desc}</p>
</li>
))
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"@/utils": ["src/utils/index.ts"],
"@/stores/*": ["src/stores/*"],
"@/types": ["src/types.ts"],
"@/site-config": ["src/site.config.ts"]
"@/site-config": ["src/site.config.ts"],
"@/katex": ["node_modules/.pnpm/rehype-katex@7.0.0/node_modules/katex/dist/katex.min.css"]
}
},
"include": ["src", "*.ts"],
Expand Down