Skip to content

Commit eae10b9

Browse files
authored
feat: mermaid diagrams support (#71)
* wip: basic mermaid support * fix: regex for memraid * feat: support for mermaid * docs: update docs about diagrams
1 parent e5716f2 commit eae10b9

File tree

14 files changed

+584
-31
lines changed

14 files changed

+584
-31
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ Documentations and guides on **[Sli.dev](https://sli.dev)**
5656
- 🌈 [**Stylish**](https://sli.dev/guide/syntax.html#embedded-styles) - [Windi CSS](https://windicss.org/) on-demand utilities, easy-to-use embedded stylesheets
5757
- 🤹 [**Interactive**](https://sli.dev/custom/directory-structure.html#components) - embedding Vue components seamlessly
5858
- 🎙 [**Presenter Mode**](https://sli.dev/guide/presenter-mode.html) - use another window, or even your phone to control your slides
59-
- 🧮 [**LaTeX**](https://sli.dev/demo/starter/6) - Built-in LaTeX math equations support
59+
- 🧮 [**LaTeX**](https://sli.dev/guide/syntax.html#latex) - Built-in LaTeX math equations support
60+
- 📰 [**Diagrams**](https://sli.dev/guide/syntax.html#diagrams) - Creates diagrams with textual descriptions
6061
- 🌟 [**Icons**](https://sli.dev/guide/syntax.html#icons) - Access to icons from any iconset directly
6162
- 💻 [**Editors**](https://sli.dev/guide/editors.html) - Integrated editor, or [extension for VS Code](https://github.com/slidevjs/slidev-vscode)
6263
- 🎥 [**Recording**](https://sli.dev/guide/recording.html) - built-in recording and camera view
@@ -84,6 +85,7 @@ For a full example, you can check the [demo](https://github.com/slidevjs/slidev/
8485
- [VueUse](https://vueuse.org) family - [`@vueuse/core`](https://github.com/vueuse/vueuse), [`@vueuse/head`](https://github.com/vueuse/head), [`@vueuse/motion`](https://github.com/vueuse/motion), etc.
8586
- [Iconify](https://iconify.design/) - Iconsets collection.
8687
- [KaTeX](https://katex.org/) - LaTeX math rendering.
88+
- [Mermaid](https://mermaid-js.github.io/mermaid) - Textual Diagrams.
8789

8890
## Sponsors
8991

docs/guide/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ You can learn more about the rationale behind the project in the [Why Slidev](/g
1616
- 🌈 [**Stylish**](/guide/syntax.html#embedded-styles) - [Windi CSS](https://windicss.org/) on-demand utilities, easy-to-use embedded stylesheets
1717
- 🤹 [**Interactive**](/custom/directory-structure.html#components) - embedding Vue components seamlessly
1818
- 🎙 [**Presenter Mode**](/guide/presenter-mode.html) - use another window, or even your phone to control your slides
19-
- 🧮 [**LaTeX**](https://sli.dev/demo/starter/6) - Built-in LaTeX math equations support
19+
- 🧮 [**LaTeX**](/guide/syntax.html#latex) - Built-in LaTeX math equations support
20+
- 📰 [**Diagrams**](/guide/syntax.html#diagrams) - Creates diagrams with textual descriptions
2021
- 🌟 [**Icons**](/guide/syntax.html#icons) - Access to icons from any iconset directly
2122
- 💻 [**Editors**](/guide/editors.html) - Integrated editor, or [extension for VS Code](https://github.com/slidevjs/slidev-vscode)
2223
- 🎥 [**Recording**](/guide/recording.html) - built-in recording and camera view
@@ -36,6 +37,7 @@ Slidev is made possible by combining these tools and technologies.
3637
- [VueUse](https://vueuse.org) family - [`@vueuse/core`](https://github.com/vueuse/vueuse), [`@vueuse/head`](https://github.com/vueuse/head), [`@vueuse/motion`](https://github.com/vueuse/motion), etc.
3738
- [Iconify](https://iconify.design/) - Iconsets collection.
3839
- [KaTeX](https://katex.org/) - LaTeX math rendering.
40+
- [Mermaid](https://mermaid-js.github.io/mermaid) - Textual Diagrams.
3941

4042
### Scaffolding Your First Presentation
4143

docs/guide/syntax.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,31 @@ $$
259259
$$
260260
```
261261

262-
Learn more: [Demo](https://sli.dev/demo/starter/6) | [KaTeX](https://katex.org/) | [`markdown-it-katex`](https://github.com/waylonflinn/markdown-it-katex).
262+
Learn more: [Demo](https://sli.dev/demo/starter/7) | [KaTeX](https://katex.org/) | [`markdown-it-katex`](https://github.com/waylonflinn/markdown-it-katex)
263+
264+
## Diagrams
265+
266+
You can also create diagrams / graphs from textual descriptions in your Markdown, powered by [Mermaid](https://mermaid-js.github.io/mermaid).
267+
268+
Code blocks marked as `mermaid` will be converted to digrams, for example:
269+
270+
~~~md
271+
//```mermaid
272+
sequenceDiagram
273+
Alice->John: Hello John, how are you?
274+
Note over Alice,John: A typical interaction
275+
//```
276+
~~~
277+
278+
You can further pass an options object to it to specify the scaling and theming. The syntax of the object is a JavaScript object literal, you will need to add quotes (`'`) for strings and use comma (`,`) between keys.
279+
280+
~~~md
281+
//```mermaid {theme: 'neutral', scale: 0.8}
282+
graph TD
283+
B[Text] --> C{Decision}
284+
C -->|One| D[Result 1]
285+
C -->|Two| E[Result 2]
286+
//```
287+
~~~
288+
289+
Learn more: [Demo](https://sli.dev/demo/starter/8) | [Mermaid](https://mermaid-js.github.io/mermaid)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@types/jest": "^26.0.23",
4343
"@types/js-yaml": "^4.0.1",
4444
"@types/markdown-it": "^12.0.1",
45+
"@types/mermaid": "^8.2.5",
4546
"@types/node": "^15.0.2",
4647
"@types/prettier": "^2.2.3",
4748
"@types/prismjs": "^1.16.5",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!--
2+
Mermaid
3+
(auto transformed, you don't need to use this component directly)
4+
5+
Usage:
6+
7+
```mermaid
8+
pie
9+
"Dogs" : 386
10+
"Cats" : 85
11+
"Rats" : 15
12+
```
13+
-->
14+
15+
<script setup lang="ts">
16+
import { defineProps, computed, getCurrentInstance } from 'vue'
17+
import { renderMermaid } from '../modules/mermaid'
18+
19+
const props = defineProps({
20+
code: {
21+
default: '',
22+
},
23+
scale: {
24+
default: 1,
25+
},
26+
})
27+
28+
const vm = getCurrentInstance()
29+
const html = computed(() => renderMermaid(props.code, vm?.attrs || {}))
30+
</script>
31+
32+
<template>
33+
<Transform :scale="scale">
34+
<div v-html="html"></div>
35+
</Transform>
36+
</template>

packages/client/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import './styles/code.css'
1414
import 'virtual:windi-utilities.css'
1515
import 'virtual:windi-devtools'
1616

17+
/* __body__ */
18+
1719
const app = createApp(App)
1820
app.use(router)
1921
app.use(createHead())

packages/client/modules/mermaid.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @ts-ignore
2+
import mermaid from 'mermaid/dist/mermaid.min'
3+
import { customAlphabet } from 'nanoid'
4+
import { decode } from 'js-base64'
5+
6+
mermaid.startOnLoad = false
7+
mermaid.initialize({ startOnLoad: false })
8+
9+
const nanoid = customAlphabet('abcedfghicklmn', 10)
10+
const cache = new Map<string, string>()
11+
12+
export function renderMermaid(encoded: string, options: any) {
13+
const _cache = cache.get(encoded)
14+
if (_cache)
15+
return _cache
16+
17+
mermaid.initialize({
18+
startOnLoad: false,
19+
theme: 'forest',
20+
...options,
21+
})
22+
const code = decode(encoded)
23+
const id = nanoid()
24+
const svg = mermaid.render(id, code)
25+
cache.set(encoded, svg)
26+
return svg
27+
}

packages/create-app/template/slides.md

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,39 @@ Check out [the guides](https://sli.dev/custom/#components) for more.
151151
</div>
152152
</div>
153153

154+
155+
---
156+
class: px-20
157+
---
158+
159+
# Themes
160+
161+
Slidev comes with powerful theming support. Themes are able to provide styles, layouts, components, or even configurations for tools. Switching between themes by just **one edit** in your frontmatter:
162+
163+
<div grid="~ cols-2 gap-2" m="-t-2">
164+
165+
```yaml
166+
---
167+
theme: default
168+
---
169+
```
170+
171+
```yaml
172+
---
173+
theme: seriph
174+
---
175+
```
176+
177+
<img border="rounded" src="https://github.com/slidevjs/themes/blob/main/screenshots/theme-default/01.png?raw=true">
178+
179+
<img border="rounded" src="https://github.com/slidevjs/themes/blob/main/screenshots/theme-seriph/01.png?raw=true">
180+
181+
</div>
182+
183+
Read more about [How to use a theme](https://sli.dev/themes/use.html) and
184+
check out the [Awesome Themes Gallery](https://sli.dev/themes/gallery.html).
185+
186+
154187
---
155188

156189
# LaTeX
@@ -179,37 +212,30 @@ $$
179212

180213
[Learn more](https://sli.dev/guide/syntax#latex)
181214

182-
---
183-
class: px-20
184215
---
185216

186-
# Themes
217+
# Diagrams
187218

188-
Slidev comes with powerful theming support. Themes are able to provide styles, layouts, components, or even configurations for tools. Switching between themes by just **one edit** in your frontmatter:
219+
You can create diagrams / graphs from textual descriptions, directly in your Markdown.
189220

190-
<div grid="~ cols-2 gap-2" m="-t-2">
221+
<div class="grid grid-cols-2 gap-4 pt-4 -mb-6">
191222

192-
```yaml
193-
---
194-
theme: default
195-
---
223+
```mermaid {scale: 0.9}
224+
sequenceDiagram
225+
Alice->John: Hello John, how are you?
226+
Note over Alice,John: A typical interaction
196227
```
197228

198-
```yaml
199-
---
200-
theme: seriph
201-
---
229+
```mermaid {theme: 'neutral', scale: 0.8}
230+
graph TD
231+
B[Text] --> C{Decision}
232+
C -->|One| D[Result 1]
233+
C -->|Two| E[Result 2]
202234
```
203235

204-
<img border="rounded" src="https://github.com/slidevjs/themes/blob/main/screenshots/theme-default/01.png?raw=true">
205-
206-
<img border="rounded" src="https://github.com/slidevjs/themes/blob/main/screenshots/theme-seriph/01.png?raw=true">
207-
208236
</div>
209237

210-
Read more about [How to use a theme](https://sli.dev/themes/use.html) and
211-
check out the [Awesome Themes Gallery](https://sli.dev/themes/gallery.html).
212-
238+
Learn more about [the syntax](https://sli.dev/guide/syntax.html#diagrams)
213239

214240
---
215241
layout: center

packages/parser/src/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function detectFeatures(code: string) {
5959
katex: !!code.match(/\$.*?\$/) || !!code.match(/$\$\$/),
6060
monaco: !!code.match(/{monaco.*}/),
6161
tweet: !!code.match(/<Tweet\b/),
62+
mermaid: !!code.match(/^```mermaid/m),
6263
}
6364
}
6465

packages/slidev/node/plugins/entry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function createEntryPlugin({ clientRoot, themeRoots, userRoot }: Resolved
1414
async transform(code, id) {
1515
if (id === mainEntry) {
1616
const imports: string[] = []
17+
const body: string[] = []
1718

1819
async function scanStyle(root: string) {
1920
const styles = [
@@ -40,6 +41,7 @@ export function createEntryPlugin({ clientRoot, themeRoots, userRoot }: Resolved
4041
await scanStyle(root)
4142

4243
code = code.replace('/* __imports__ */', imports.join('\n'))
44+
code = code.replace('/* __body__ */', body.join('\n'))
4345
return code
4446
}
4547

0 commit comments

Comments
 (0)