Skip to content

Commit

Permalink
feat: PlantUML online support (#389)
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph Welcz <christoph@welcz.de>
  • Loading branch information
enolive and Christoph Welcz committed Nov 1, 2021
1 parent 091107b commit bffec8a
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 12 deletions.
39 changes: 37 additions & 2 deletions demo/starter/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ $$

You can create diagrams / graphs from textual descriptions, directly in your Markdown.

<div class="grid grid-cols-2 gap-10 pt-4 -mb-6">
<div class="grid grid-cols-3 gap-10 pt-4 -mb-6">

```mermaid {scale: 0.9}
```mermaid {scale: 0.5}
sequenceDiagram
Alice->John: Hello John, how are you?
Note over Alice,John: A typical interaction
Expand All @@ -333,6 +333,41 @@ C -->|One| D[Result 1]
C -->|Two| E[Result 2]
```

```plantuml {scale: 0.7}
@startuml
package "Some Group" {
HTTP - [First Component]
[Another Component]
}
node "Other Groups" {
FTP - [Second Component]
[First Component] --> FTP
}
cloud {
[Example 1]
}
database "MySql" {
folder "This is my folder" {
[Folder 3]
}
frame "Foo" {
[Frame 4]
}
}
[Another Component] --> [Example 1]
[Example 1] --> [Folder 3]
[Folder 3] --> [Frame 4]
@enduml
```

</div>

[Learn More](https://sli.dev/guide/syntax.html#diagrams)
Expand Down
29 changes: 29 additions & 0 deletions packages/client/builtin/PlantUml.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
PlantUML
(auto transformed, you don't need to use this component directly)
Usage:
```plantuml
@startuml
Alice -> Bob : Hello!
@enduml
```
-->
<template>
<img alt="PlantUML diagram" :src="uri" :style="{scale}" />
</template>

<script setup lang="ts">
import { computed } from 'vue'
const props = defineProps<{
code: string
server: string
scale?: number
}>()
const uri = computed(() => `${props.server}/svg/${props.code}`)
</script>
1 change: 1 addition & 0 deletions packages/parser/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function resolveConfig(headmatter: any, themeMeta: SlidevThemeMeta = {})
themeConfig: {},
fonts: {} as ResolvedFontOptions,
drawings: {} as ResolvedDrawingsOptions,
plantUmlServer: 'https://www.plantuml.com/plantuml',
}
const config: SlidevConfig = {
...defaultConfig,
Expand Down
11 changes: 11 additions & 0 deletions packages/slidev/node/plugins/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { KatexOptions } from 'katex'
import type MarkdownIt from 'markdown-it'
import type { ShikiOptions } from '@slidev/types'
import * as Shiki from 'shiki'
import { encode } from 'plantuml-encoder'
import { ResolvedSlidevOptions, SlidevPluginOptions } from '../options'
import Katex from './markdown-it-katex'
import { loadSetups } from './setupNode'
Expand Down Expand Up @@ -82,6 +83,7 @@ export async function createMarkdownPlugin(

code = transformSlotSugar(code)
code = transformMermaid(code)
code = transformPlantUml(code, config.plantUmlServer)
code = monaco(code)
code = transformHighlighter(code)
code = transformPageCSS(code, id)
Expand Down Expand Up @@ -205,6 +207,15 @@ export function transformMermaid(md: string): string {
})
}

export function transformPlantUml(md: string, server: string): string {
return md
.replace(/^```plantuml\s*?({.*?})?\n([\s\S]+?)\n```/mg, (full, options = '', content = '') => {
const code = encode(content.trim())
options = options.trim() || '{}'
return `<PlantUml :code="'${code}'" :server="'${server}'" v-bind="${options}" />`
})
}

/**
* Escape `{{}}` in code block to prevent Vue interpret it, #99
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/slidev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"nanoid": "^3.1.30",
"open": "^8.3.0",
"pdf-lib": "^1.17.0",
"plantuml-encoder": "^1.4.0",
"prismjs": "^1.25.0",
"prompts": "^2.4.2",
"resolve": "^1.20.0",
Expand All @@ -90,6 +91,7 @@
"yargs": "^17.2.1"
},
"devDependencies": {
"@types/plantuml-encoder": "^1.4.0",
"fast-deep-equal": "^3.1.3",
"semver": "^7.3.5",
"sirv": "^1.0.18"
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ export interface SlidevConfig {
* Options for drawings
*/
drawings: ResolvedDrawingsOptions

/**
* URL of PlantUML server used to render diagrams
*
* @default https://www.plantuml.com/plantuml
*/
plantUmlServer: string
}

export interface FontOptions {
Expand Down
27 changes: 18 additions & 9 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Object {
"layout": "cover",
"lineNumbers": false,
"monaco": "dev",
"plantUmlServer": "https://www.plantuml.com/plantuml",
"remoteAssets": false,
"routerMode": "history",
"selectable": false,
Expand Down Expand Up @@ -282,6 +283,7 @@ Object {
"info": false,
"lineNumbers": false,
"monaco": "dev",
"plantUmlServer": "https://www.plantuml.com/plantuml",
"remoteAssets": false,
"routerMode": "history",
"selectable": false,
Expand Down Expand Up @@ -443,6 +445,7 @@ Object {
"info": false,
"lineNumbers": false,
"monaco": "dev",
"plantUmlServer": "https://www.plantuml.com/plantuml",
"remoteAssets": false,
"routerMode": "history",
"selectable": false,
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ exports[`markdown transform Mermaid 1`] = `
"
`;

exports[`markdown transform PlantUML 1`] = `
"
# Page
<PlantUml :code=\\"'SoWkIImgAStDuNBCoKnELT2rKt3AJrAmKl18pSd9XmhQw28BwYYKvEShSZcavgK0zG40'\\" :server=\\"'https://www.plantuml.com/plantuml'\\" v-bind=\\"{}\\" />
<PlantUml :code=\\"'JOzD3e8m44Rtd6BMtNW192IM5I29HEDsAbKdeLD2MvNRIsjCMCsRlFd9LpgFipV4Wy4f4o2r8kHC23Yhm3wi9A0X3XzeYNrgwx1H6wvb1KTjqtRJoYhMtexBSAqJUescwoEUq4tn3xp9Fm7XfUS5HiiFO3Gw7SjT4QUCkkKxLy2-WAvl3rkrtEclBdOCXcnMwZN7ByiN'\\" :server=\\"'https://www.plantuml.com/plantuml'\\" v-bind=\\"{scale: 0.5}\\" />
"
`;

exports[`markdown transform slot-sugar 1`] = `
"
# Page
Expand Down
37 changes: 36 additions & 1 deletion test/transform.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { transformMermaid, transformPageCSS, transformSlotSugar } from '../packages/slidev/node/plugins/markdown'
import {
transformMermaid,
transformPageCSS,
transformPlantUml,
transformSlotSugar,
} from '@slidev/cli/node/plugins/markdown'

describe('markdown transform', () => {
it('slot-sugar', () => {
Expand Down Expand Up @@ -80,4 +85,34 @@ C -->|Two| E[Result 2]
\`\`\`
`)).toMatchSnapshot()
})

it('PlantUML', () => {
expect(transformPlantUml(`
# Page
\`\`\`plantuml
@startuml
Alice -> Bob : Hello
Alice <- Bob : Hello, too!
@enduml
\`\`\`
\`\`\`plantuml {scale: 0.5}
@startmindmap
* Debian
** Ubuntu
*** Linux Mint
*** Kubuntu
*** Lubuntu
*** KDE Neon
** LMDE
** SolydXK
** SteamOS
** Raspbian with a very long name
*** <s>Raspmbc</s> => OSMC
*** <s>Raspyfi</s> => Volumio
@endmindmap
\`\`\`
`, 'https://www.plantuml.com/plantuml')).toMatchSnapshot()
})
})

0 comments on commit bffec8a

Please sign in to comment.