Skip to content

Commit

Permalink
feat(mdx-math): add katex math support (#675)
Browse files Browse the repository at this point in the history
* feat(mdx-math): add katex math support
* chore(mdx-math): add more testing contents

issue #614
  • Loading branch information
sabertazimi authored Apr 26, 2022
1 parent 040c400 commit e92e4ea
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ A great [blog](https://blog.tazimi.dev) system based on React.js and Next.js.
- Great tags cloud page: separate page for posts under each tag.
- Great GitHub information card.
- Social share buttons.
- Syntax highlighting in code blocks using PrismJS.
- Full-featured MDX support:
- GFM support.
- Katex math support.
- Annotation container support.
- Emoji short code support.
- Syntax highlighting in code blocks using PrismJS.
- Automatically generated sidebar navigation,
table of contents, previous and next links.
- Nice animation for page transitions and dynamic routing.
- Disqus comments system.
- Customized 404 not found page.
Expand All @@ -36,7 +43,6 @@ A great [blog](https://blog.tazimi.dev) system based on React.js and Next.js.
- Optimized build with `Rust` compiler.
- Dynamic route generation for `Markdown` posts.
- Performant `MDX` support, enhance `Markdown` with `React`.
- Automatically generated sidebar navigation, table of contents, previous/next links.
- Out of box support for `tailwind.css`.
- Fully customizable through `tailwind.css`.
- Mobile-first approach in development.
Expand Down
47 changes: 47 additions & 0 deletions contents/implementFancyMDX.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,50 @@ A note[^1]

* [ ] to do
* [x] done

## Math

Inline math formulas

$\sqrt{a^2 + b^2}$

$c = \pm\sqrt{a^2 + b^2}$

Block math formulas

$$
L = \frac{1}{2} \rho v^2 S C_L
$$

$$
\def\arraystretch{1.5}
\begin{array}{c:c:c}
a & b & c \\ \hline
d & e & f \\
\hdashline
g & h & i
\end{array}
$$

$$
\begin{equation}
\begin{split}
a &= b + c \\
&= e + f
\end{split}
\end{equation}
$$

$$
\begin{align}
a &= b + c \\
d + e &= f
\end{align}
$$

$$
\begin{alignat}{2}
10 &x + &3 &y = 2 \\
3 &x + &13 &y = 4
\end{alignat}
$$
10 changes: 9 additions & 1 deletion lib/getPostsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { execSync } from 'node:child_process';
import fs from 'node:fs/promises';
import path from 'node:path';
import readingTime from 'reading-time';
import rehypeKatex from 'rehype-katex';
import remarkMath from 'remark-math';

const contentsPath = path.join(process.cwd(), 'contents');
let postsData: Post[] = [];
Expand Down Expand Up @@ -41,7 +43,13 @@ async function generatePostData(filePath: string): Promise<Post> {
const updateTime = execSync(
`git log -1 --pretty=format:%aI ${filePath}`
).toString();
const source = await serialize(content, { parseFrontmatter: false });
const source = await serialize(content, {
parseFrontmatter: false,
mdxOptions: {
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
},
});

return {
...fields,
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"antd": "^4.20.0",
"classnames": "^2.3.1",
"framer-motion": "^6.3.1",
"katex": "^0.15.3",
"next": "12.1.5",
"next-mdx-remote": "4.0.1",
"next-progress": "^2.2.0",
Expand Down Expand Up @@ -77,6 +78,8 @@
"postcss-preset-env": "^7.4.3",
"prettier": "^2.6.2",
"reading-time": "^1.5.0",
"rehype-katex": "^6.0.2",
"remark-math": "^5.1.1",
"standard-version": "^9.3.2",
"stylelint": "^14.7.1",
"tailwindcss": "^3.0.24",
Expand Down
1 change: 1 addition & 0 deletions pages/post/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Article } from '@components';
import { PostLayout } from '@layouts';
import { getBuildTime, getPostData, getPostsMeta } from '@lib';
import type { Post, PostMeta } from '@types';
import 'katex/dist/katex.css';
import type { GetStaticPaths, GetStaticProps } from 'next/types';
import type { ParsedUrlQuery } from 'querystring';

Expand Down
Loading

1 comment on commit e92e4ea

@vercel
Copy link

@vercel vercel bot commented on e92e4ea Apr 26, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

blog – ./

blog.tazimi.dev
blog-git-main-sabertaz.vercel.app
blog-sabertaz.vercel.app

Please sign in to comment.