Skip to content

Commit 2632e63

Browse files
committed
feat: custom setup for mermaid and katex, close #79, close #82
1 parent 53c47ed commit 2632e63

File tree

13 files changed

+159
-90
lines changed

13 files changed

+159
-90
lines changed

docs/.vitepress/config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ const Customizations = [
8383
text: 'Configure Monaco',
8484
link: '/custom/config-monaco',
8585
},
86+
{
87+
text: 'Configure KaTeX',
88+
link: '/custom/config-katex',
89+
},
90+
{
91+
text: 'Configure Mermaid',
92+
link: '/custom/config-mermaid',
93+
},
8694
]
8795

8896
const Resources = [

docs/custom/config-katex.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Configure KaTeX
2+
3+
Create `./setup/katex.ts` with the following content:
4+
5+
```ts
6+
import { defineKatexSetup } from '@slidev/types'
7+
8+
export default defineKatexSetup(() => {
9+
return {
10+
/* ... */
11+
}
12+
})
13+
```
14+
15+
With the setup, you can provide the custom setting for [KaTex Options](https://katex.org/docs/options.html). Refer to the type definitions and their documentation for more details.

docs/custom/config-mermaid.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Configure Mermaid
2+
3+
Create `./setup/mermaid.ts` with the following content:
4+
5+
```ts
6+
import { defineMermaidSetup } from '@slidev/types'
7+
8+
export default defineMermaidSetup(() => {
9+
return {
10+
theme: 'forest',
11+
}
12+
})
13+
```
14+
15+
With the setup, you can provide a custom default setting for [Mermaid](https://mermaid-js.github.io/). Refer to the type definitions and its documentation for more details.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"devDependencies": {
3232
"@antfu/eslint-config": "^0.6.5",
3333
"@antfu/ni": "^0.7.0",
34-
"@antfu/utils": "^0.1.5",
34+
"@antfu/utils": "^0.1.6",
3535
"@slidev/cli": "workspace:*",
3636
"@slidev/parser": "workspace:*",
3737
"@slidev/types": "workspace:*",
@@ -42,9 +42,10 @@
4242
"@types/fs-extra": "^9.0.11",
4343
"@types/jest": "^26.0.23",
4444
"@types/js-yaml": "^4.0.1",
45+
"@types/katex": "^0.11.0",
4546
"@types/markdown-it": "^12.0.1",
4647
"@types/mermaid": "^8.2.5",
47-
"@types/node": "^15.0.3",
48+
"@types/node": "^15.3.0",
4849
"@types/prettier": "^2.2.3",
4950
"@types/prismjs": "^1.16.5",
5051
"@types/prompts": "^2.4.0",
@@ -58,7 +59,9 @@
5859
"esno": "^0.5.0",
5960
"husky": "4.3.7",
6061
"jest": "^26.6.3",
62+
"katex": "^0.13.10",
6163
"lint-staged": "^11.0.0",
64+
"mermaid": "^8.10.1",
6265
"playwright-chromium": "^1.11.0",
6366
"pnpm": "^6.3.0",
6467
"rimraf": "^3.0.2",

packages/client/builtin/Mermaid.vue

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,18 @@ pie
1616
import { defineProps, computed, getCurrentInstance } from 'vue'
1717
import { renderMermaid } from '../modules/mermaid'
1818
19-
const props = defineProps({
20-
code: {
21-
default: '',
22-
},
23-
scale: {
24-
default: 1,
25-
},
26-
theme: {
27-
defualt: 'base',
28-
},
29-
})
19+
const props = defineProps<{
20+
code: string
21+
scale?: number
22+
theme?: string
23+
}>()
3024
3125
const vm = getCurrentInstance()
32-
const html = computed(() => renderMermaid(props.code, Object.assign({ theme: props.theme }, vm.attrs)))
26+
const html = computed(() => renderMermaid(props.code || '', Object.assign({ theme: props.theme }, vm!.attrs)))
3327
</script>
3428

3529
<template>
36-
<Transform :scale="scale">
30+
<Transform :scale="scale || 1">
3731
<div v-html="html"></div>
3832
</Transform>
3933
</template>

packages/client/modules/mermaid.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import mermaid from 'mermaid/dist/mermaid.min'
33
import { customAlphabet } from 'nanoid'
44
import { decode } from 'js-base64'
5+
import { clearUndefined } from '@antfu/utils'
6+
import setupMermaid from '../setup/mermaid'
57

68
mermaid.startOnLoad = false
79
mermaid.initialize({ startOnLoad: false })
@@ -17,8 +19,8 @@ export function renderMermaid(encoded: string, options: any) {
1719

1820
mermaid.initialize({
1921
startOnLoad: false,
20-
theme: 'forest',
21-
...options,
22+
...clearUndefined(setupMermaid() || {}),
23+
...clearUndefined(options),
2224
})
2325
const code = decode(encoded)
2426
const id = nanoid()

packages/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"homepage": "https://sli.dev",
1616
"bugs": "https://github.com/slidevjs/slidev/issues",
1717
"dependencies": {
18-
"@antfu/utils": "^0.1.5",
18+
"@antfu/utils": "^0.1.6",
1919
"@slidev/parser": "workspace:*",
2020
"@slidev/types": "workspace:*",
2121
"@vueuse/core": "^4.11.0",

packages/client/setup/mermaid.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* __imports__ */
2+
3+
import { defineMermaidSetup, MermaidOptions } from '@slidev/types'
4+
5+
export default defineMermaidSetup(() => {
6+
// eslint-disable-next-line prefer-const
7+
let injection_return: MermaidOptions = {
8+
theme: 'default',
9+
}
10+
11+
/* __injections__ */
12+
13+
return injection_return
14+
})

packages/slidev/node/plugins/markdown.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { slash } from '@antfu/utils'
88
import mila from 'markdown-it-link-attributes'
99
// @ts-expect-error
1010
import Katex from 'markdown-it-katex'
11+
import type { KatexOptions } from 'katex'
1112
import { ResolvedSlidevOptions, SlidevPluginOptions } from '../options'
1213
import { loadSetups } from './setupNode'
1314
import Prism from './markdown-it-prism'
@@ -38,6 +39,8 @@ export async function createMarkdownPlugin(
3839
setups.push(md => md.use(Prism))
3940
}
4041

42+
const KatexOptions: KatexOptions = await loadSetups(roots, 'katex.ts', {}, {}, false)
43+
4144
return Markdown({
4245
wrapperClasses: '',
4346
headEnabled: false,
@@ -56,7 +59,7 @@ export async function createMarkdownPlugin(
5659
},
5760
})
5861

59-
md.use(Katex)
62+
md.use(Katex, KatexOptions)
6063

6164
setups.forEach(i => i(md))
6265
},

packages/slidev/node/plugins/setupClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function createClientSetupPlugin({ clientRoot, themeRoots, userRoot }: Re
3030
imports.push(`import __n${idx} from '${toAtFS(path)}'`)
3131
injections.push(
3232
`// ${path}`,
33-
`__n${idx}()`,
33+
`injection_return = __n${idx}()`,
3434
)
3535
asyncInjections.push(
3636
`// ${path}`,

0 commit comments

Comments
 (0)