Skip to content

Commit 65fbe0f

Browse files
committed
feat: render plantuml diagrams
1 parent 95fc07c commit 65fbe0f

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

doc/src/writing/extensions.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,16 @@ You can include other files by using `@include filename.md`. Once loaded, they a
142142

143143
Recursive includes will result in an error, but one external file can include another one. That is, if `1.md` includes `2.md`, then `2.md` can still include `3.md`.
144144

145+
## PlantUML Diagrams
146+
147+
PlantUML diagrams can be rendered.
148+
149+
```plantuml
150+
Bob -> Alice : hello
151+
```
152+
153+
results in:
154+
155+
```plantuml
156+
Bob -> Alice : hello
157+
```

package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
],
7676
"dependencies": {
7777
"archiver": "^3.0.0",
78+
"axios": "^0.18.0",
7879
"chalk": "^2.4.1",
7980
"chokidar": "^2.1.5",
8081
"commander": "^2.20.0",
@@ -90,6 +91,7 @@
9091
"koa-send": "^5.0.0",
9192
"make-dir": "^3.0.0",
9293
"mdast-util-to-hast": "^5.0.0",
94+
"plantuml-encoder": "^1.2.5",
9395
"puppeteer-core": "^1.15.0",
9496
"rehype-katex": "^1.2.0",
9597
"rehype-stringify": "^5.0.0",

src/renderer/markdown.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import katex from 'rehype-katex'
77
import math from 'remark-math'
88
import markdown from 'remark-parse'
99
import meta from 'remark-meta'
10+
import plantuml from './remark/remark-plantuml'
1011
import redirect from 'remark-redirect'
1112
import remark2rehype from 'remark-rehype'
1213
import supersub from 'remark-supersub'
@@ -23,6 +24,7 @@ const createProcessor = () =>
2324
.use(include)
2425
.use(math)
2526
.use(deflist)
27+
.use(plantuml)
2628
.use(supersub)
2729
.use(meta)
2830
.use(bibliography)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Remark plugin for rendering PlantUML syntax
3+
*/
4+
import axios from 'axios'
5+
import { encode } from 'plantuml-encoder'
6+
import visit from 'unist-util-visit'
7+
8+
export default function (options = {}) {
9+
return async (tree, file) => {
10+
const nodes = []
11+
12+
visit(tree, ['code'], (node, i, parent) => {
13+
if (node.lang !== 'plantuml') {
14+
return
15+
}
16+
nodes.push([parent, i, node.value])
17+
})
18+
19+
if (!nodes.length) {
20+
return
21+
}
22+
23+
for (const [parent, i, value] of nodes) {
24+
const base64 = encode(value)
25+
const { data } = await axios(
26+
`http://www.plantuml.com/plantuml/svg/${base64}`
27+
)
28+
29+
const node = {
30+
type: 'image',
31+
url: `data:image/svg+xml;utf8,${encodeURIComponent(data)}`,
32+
title: 'PlantUML image',
33+
alt: 'PlantUML image'
34+
}
35+
36+
parent.children.splice(i, 1, node)
37+
}
38+
39+
return tree
40+
}
41+
}

0 commit comments

Comments
 (0)