Skip to content

Commit 7271a95

Browse files
authored
feat: mathjax support (vuejs#2977)
1 parent fd46dc9 commit 7271a95

File tree

11 files changed

+472
-1
lines changed

11 files changed

+472
-1
lines changed

docs/.vitepress/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export default defineConfig({
1212
lastUpdated: true,
1313
cleanUrls: true,
1414

15+
markdown: {
16+
math: true
17+
},
18+
1519
sitemap: {
1620
hostname: 'https://vitepress.dev',
1721
transformItems(items) {

docs/guide/markdown.md

+45
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,51 @@ The format of the selected line range can be: `{3,}`, `{,10}`, `{1,10}`
795795
Note that this does not throw errors if your file is not present. Hence, when using this feature make sure that the contents are being rendered as expected.
796796
:::
797797

798+
## Math Equations
799+
800+
This is currently opt-in. To enable it, you need to install `markdown-it-mathjax3` and set `markdown.math` to `true` in your config file:
801+
802+
```sh
803+
npm add -D markdown-it-mathjax3
804+
```
805+
806+
```ts
807+
// .vitepress/config.ts
808+
export default {
809+
markdown: {
810+
math: true
811+
}
812+
}
813+
```
814+
815+
**Input**
816+
817+
```md
818+
When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are
819+
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
820+
821+
**Maxwell's equations:**
822+
823+
| equation | description |
824+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
825+
| $\nabla \cdot \vec{\mathbf{B}} = 0$ | divergence of $\vec{\mathbf{B}}$ is zero |
826+
| $\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} = \vec{\mathbf{0}}$ | curl of $\vec{\mathbf{E}}$ is proportional to the rate of change of $\vec{\mathbf{B}}$ |
827+
| $\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} = \frac{4\pi}{c}\vec{\mathbf{j}} \nabla \cdot \vec{\mathbf{E}} = 4 \pi \rho$ | _wha?_ |
828+
```
829+
830+
**Output**
831+
832+
When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are
833+
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
834+
835+
**Maxwell's equations:**
836+
837+
| equation | description |
838+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
839+
| $\nabla \cdot \vec{\mathbf{B}} = 0$ | divergence of $\vec{\mathbf{B}}$ is zero |
840+
| $\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} = \vec{\mathbf{0}}$ | curl of $\vec{\mathbf{E}}$ is proportional to the rate of change of $\vec{\mathbf{B}}$ |
841+
| $\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} = \frac{4\pi}{c}\vec{\mathbf{j}} \nabla \cdot \vec{\mathbf{E}} = 4 \pi \rho$ | _wha?_ |
842+
798843
## Advanced Configuration
799844

800845
VitePress uses [markdown-it](https://github.com/markdown-it/markdown-it) as the Markdown renderer. A lot of the extensions above are implemented via custom plugins. You can further customize the `markdown-it` instance using the `markdown` option in `.vitepress/config.js`:

docs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"preview": "vitepress preview"
99
},
1010
"devDependencies": {
11+
"markdown-it-mathjax3": "^4.3.2",
1112
"vitepress": "workspace:*"
1213
}
1314
}

docs/reference/site-config.md

+16
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,24 @@ interface MarkdownOptions extends MarkdownIt.Options {
525525
// See: https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-toc#options
526526
toc?: TocPluginOptions
527527

528+
// @mdit-vue/plugin-component plugin options.
529+
// See: https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-component#options
530+
component?: ComponentPluginOptions
531+
528532
// Configure the Markdown-it instance.
529533
config?: (md: MarkdownIt) => void
534+
535+
// Same as `config` but will be applied before all other plugins.
536+
preConfig?: (md: MarkdownIt) => void
537+
538+
// Disable cache (experimental)
539+
cache?: boolean
540+
541+
// Math support (experimental)
542+
// You need to install `markdown-it-mathjax3` and set `math` to `true` to enable it.
543+
// You can also pass options to `markdown-it-mathjax3` here.
544+
// See: https://github.com/tani/markdown-it-mathjax3#customization
545+
math?: any
530546
}
531547
```
532548

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@
102102
"vite": "^4.4.9",
103103
"vue": "^3.3.4"
104104
},
105+
"peerDependencies": {
106+
"markdown-it-mathjax3": "^4.3.2"
107+
},
108+
"peerDependenciesMeta": {
109+
"markdown-it-mathjax3": {
110+
"optional": true
111+
}
112+
},
105113
"devDependencies": {
106114
"@clack/prompts": "^0.7.0",
107115
"@mdit-vue/plugin-component": "^0.12.1",
@@ -154,6 +162,7 @@
154162
"markdown-it-attrs": "^4.1.6",
155163
"markdown-it-container": "^3.0.0",
156164
"markdown-it-emoji": "^2.0.2",
165+
"markdown-it-mathjax3": "^4.3.2",
157166
"micromatch": "^4.0.5",
158167
"minimist": "^1.2.8",
159168
"nanoid": "^4.0.2",

0 commit comments

Comments
 (0)