From b8fb046dbb1078177d008f2527b9647035eac481 Mon Sep 17 00:00:00 2001
From: Frankie Huang
Date: Tue, 6 May 2025 23:53:16 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20plantuml=20?=
=?UTF-8?q?=E8=AF=AD=E6=B3=95=E8=A7=A3=E6=9E=90=E8=AF=AF=E5=8C=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/components/MainEditor.vue | 31 ++++++++++++++++++++++++++-----
1 file changed, 26 insertions(+), 5 deletions(-)
diff --git a/src/components/MainEditor.vue b/src/components/MainEditor.vue
index d964d01..e8b35d1 100644
--- a/src/components/MainEditor.vue
+++ b/src/components/MainEditor.vue
@@ -221,14 +221,35 @@ const sanitize = (html) => {
// PlantUML 渲染核心逻辑
html = html.replace(/@startuml([\s\S]*?)@enduml/g, (_, umlCode) => {
+ // 参数 umlCode 不包含 @startuml 和 @enduml
+ const sourceCode = `@startuml${umlCode}@enduml`;
const lines = umlCode.split('\n');
- // 去除换行作用的首尾
- if (lines[0] == '
') {
- lines.shift();
+ if (lines.length == 0) {
+ return sourceCode;
}
- if (/]*>/i.test(lines[lines.length - 1])) {
- lines.pop();
+ // 如果 @startuml 后接的不是
或者
,可能是代码块中,不作解析
+ if (lines[0] !== '
' && lines[0] !== '
') {
+ return sourceCode;
}
+ const lastLine = lines[lines.length - 1];
+ // @enduml 前面要么是换行符(由于已切割 \n 所以会是空字符串)
+ // 要么是类似 的标签(前面有空行的情况)
+ // 如果都不是,则说明尾行不以 @enduml 开头,不作解析
+ if (lastLine.length > 0) {
+ // 既不是换行符也不是
标签,不作解析
+ if (!/
]*>$/i.test(lastLine)) {
+ return sourceCode;
+ }
+ // 存在一种情况,@enduml 前面带 > 符号,即 >@enduml。这种情况:
+ // 尾行是
标签,上一行是
,同样不作解析
+ if (lines.length >= 2 && /]*>$/i.test(lines[lines.length - 2])) {
+ return sourceCode;
+ }
+ }
+ // 去除换行作用的首行(首行是在 @startuml 后的换行符)
+ lines.shift();
+ // 去除换行作用的尾行(尾行是在 @enduml 前面的换行符)
+ lines.pop();
const removeLineBreak = /(.*?)(?=
|<\/p>)/i;
const extractedLines = lines.map(line => {