fix: 修复 plantuml 语法解析误区

This commit is contained in:
Frankie Huang 2025-05-06 23:53:16 +08:00
parent 861ff4667c
commit b8fb046dbb

View File

@ -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] == '<br>') {
lines.shift();
if (lines.length == 0) {
return sourceCode;
}
if (/<p\s+[^>]*>/i.test(lines[lines.length - 1])) {
lines.pop();
// 如果 @startuml 后接的不是 <br> 或者 </p>,可能是代码块中,不作解析
if (lines[0] !== '<br>' && lines[0] !== '</p>') {
return sourceCode;
}
const lastLine = lines[lines.length - 1];
// @enduml 前面要么是换行符(由于已切割 \n 所以会是空字符串)
// 要么是类似 <p data-line="7"> 的标签(前面有空行的情况)
// 如果都不是,则说明尾行不以 @enduml 开头,不作解析
if (lastLine.length > 0) {
// 既不是换行符也不是 <p data-line="7"> 标签,不作解析
if (!/<p\s+[^>]*>$/i.test(lastLine)) {
return sourceCode;
}
// 存在一种情况,@enduml 前面带 > 符号,即 >@enduml。这种情况:
// 尾行是 <p data-line="7"> 标签,上一行是 <blockquote data-line="7">,同样不作解析
if (lines.length >= 2 && /<blockquote\s+[^>]*>$/i.test(lines[lines.length - 2])) {
return sourceCode;
}
}
// 去除换行作用的首行(首行是在 @startuml 后的换行符)
lines.shift();
// 去除换行作用的尾行(尾行是在 @enduml 前面的换行符)
lines.pop();
const removeLineBreak = /(.*?)(?=<br>|<\/p>)/i;
const extractedLines = lines.map(line => {