跳到主要内容

Mardown增加PlantUML的渲染

· 阅读需 1 分钟
故事的程序猿👨🏻‍💻
一个后端打酱油程序猿

PlantUML 画图

依靠docusaurus.config.jsmarkdown的配置中的preprocessor方法。

以及Javascript API Client Code——PlantUML官网

需要做三件事:

To use PlantUML image generation, a text diagram description have to be :

  1. Encoded in UTF-8
  2. Compressed using Deflate algorithm
  3. Reencoded in ASCII using a transformation close to base64

最主要的就是利用算法Deflate得到转换后的字符串,这个已经有开源项目给整理好了 plantuml-encoder 拿过来直接使用即可。

首先编写md


```plantuml
@startuml
Bob -> Alice : hello
@enduml
```

配置文件增加

var plantumlEncoder = require('plantuml-encoder')

markdown: {
mermaid: true,
preprocessor: ({ filePath, fileContent }) => {
const plantumlNodeRegex = /```plantuml[\s\S]*?```/gm;
const plantumlContentRegex = /(?<=```plantuml)[\s\S]*?(?=```)/g;
if (filePath.search('docs') != -1) {
let m;
const str = fileContent;
while ((m = plantumlNodeRegex.exec(str)) !== null) {
if (m.index === plantumlNodeRegex.lastIndex) {
plantumlNodeRegex.lastIndex++;
}
m.forEach((match, groupIndex) => {
let n;
while ((n = plantumlContentRegex.exec(match)) !== null) {
if (n.index === plantumlContentRegex.lastIndex) {
plantumlContentRegex.lastIndex++;
}
n.forEach((match2, groupIndex2) => {
fileContent = fileContent.replaceAll(
match,
'![](https://www.plantuml.com/plantuml/svg/' +
plantumlEncoder.encode(match2) +
')'
);
});
}
});
}
}
return fileContent;
}
}

如果你想自己搞的话,参考rawdeflate.js 来着 https://github.com/johan/js-deflate 。但是需要注意这中文编码的问题,可能中文乱码。

文章标题:Mardown增加PlantUML的渲染
版权声明:内容遵守
许可协议。转载请注明出处!
侵权提示:部分信息可能来源于网络。如发现有侵权,请随时联系删除!

相关推荐

Nacos2.3.2开启服务端用户认证的坑

Nacos2.3.2开启服务端用户认证的坑

最近升级Nacos服务端到版本2.3.2。但是在开启服务端用户认证的时候出现了问题。

Docusaurus 静态博客系列(1)- 系统搭建

Docusaurus 静态博客系列(1)- 系统搭建

这里介绍使用 Docusaurus ,它是一个用于构建开源项目网站的静态网站生成器。它由 Facebook 开发,旨在简化创建、维护和发布技术文档和项目文档的过程。

Docker常用命令整理

Docker常用命令整理

Docker常用镜像和容器的命令,包括:镜像增删改查导入导出、容器增删改查、网络配置。

使用rss-parser解析rss订阅「Docusaurus」

使用rss-parser解析rss订阅「Docusaurus」

多库龙🐲使用rss-parser解析订阅feed,对于一个后端打酱油来说,写前端代码还是有点费劲的。


神评论