From 2659bb9131a7bfa015abcc64b083743f8e6a1913 Mon Sep 17 00:00:00 2001
From: nanhaoluo <3075912108@qq.com>
Date: Fri, 23 Jan 2026 21:15:40 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=20WP-Markdown=20?=
=?UTF-8?q?=E7=9A=84=20Mermaid=20=E6=B8=B2=E6=9F=93=E6=A0=BC=E5=BC=8F=20-?=
=?UTF-8?q?=20=E6=B7=BB=E5=8A=A0=E5=AF=B9=20script=20=E6=A0=87=E7=AD=BE?=
=?UTF-8?q?=E5=8C=85=E8=A3=B9=E6=A0=BC=E5=BC=8F=E7=9A=84=E6=94=AF=E6=8C=81?=
=?UTF-8?q?=20-=20=E8=A7=A3=E7=A0=81=20document.write=20=E4=B8=AD=E7=9A=84?=
=?UTF-8?q?=E8=BD=AC=E4=B9=89=E5=AD=97=E7=AC=A6=20-=20=E5=A4=84=E7=90=86?=
=?UTF-8?q?=20\\n=20=E4=B8=BA=E7=9C=9F=E5=AE=9E=E6=8D=A2=E8=A1=8C=E7=AC=A6?=
=?UTF-8?q?=20-=20=E5=90=8C=E6=97=B6=E4=BF=9D=E6=8C=81=E5=AF=B9=E6=A0=87?=
=?UTF-8?q?=E5=87=86=E6=A0=BC=E5=BC=8F=E7=9A=84=E5=85=BC=E5=AE=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
footer.php | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 74 insertions(+), 4 deletions(-)
diff --git a/footer.php b/footer.php
index afdf9f3..95712d3 100644
--- a/footer.php
+++ b/footer.php
@@ -187,6 +187,74 @@
});
+ // 处理 WP-Markdown 生成的特殊格式:
...
+
+ document.querySelectorAll('div.mermaid script[type="text/javascript"]').forEach(function(scriptElement) {
+
+ let mermaidDiv = scriptElement.parentElement;
+
+
+
+ // 检查是否已经处理过
+
+ if (mermaidDiv.classList.contains('mermaid-processed')) {
+
+ return;
+
+ }
+
+ mermaidDiv.classList.add('mermaid-processed');
+
+
+
+ // 从 script 标签中提取代码
+
+ let scriptContent = scriptElement.textContent;
+
+ let match = scriptContent.match(/document\.write\("(.*)"\)/s);
+
+
+
+ if (match && match[1]) {
+
+ // 解码转义字符
+
+ let code = match[1]
+
+ .replace(/\\n/g, '\n')
+
+ .replace(/\\"/g, '"')
+
+ .replace(/\\'/g, "'")
+
+ .replace(/</g, '<')
+
+ .replace(/>/g, '>')
+
+ .replace(/&/g, '&');
+
+
+
+ console.log('WP-Markdown Mermaid code found (length: ' + code.length + ')');
+
+ console.log('Contains newlines:', code.indexOf('\n') !== -1);
+
+ console.log('First 200 chars:', code.substring(0, 200));
+
+
+
+ // 清空 div 内容
+
+ mermaidDiv.innerHTML = '';
+
+ mermaidDiv.textContent = code;
+
+ mermaidDiv.classList.add('mermaid-to-render');
+
+ }
+
+ });
+
// 递归获取所有文本节点,保留换行符
function getTextWithLineBreaks(element) {
@@ -219,7 +287,7 @@
}
- // 自动渲染所有 mermaid 代码块
+ // 处理标准格式的 mermaid 代码块
document.querySelectorAll('pre code.language-mermaid, pre code.mermaid').forEach(function(element) {
@@ -283,7 +351,7 @@
// 调试:输出代码内容和换行符
- console.log('Mermaid code found (length: ' + code.length + '):', code);
+ console.log('Standard Mermaid code found (length: ' + code.length + ')');
console.log('Contains newlines:', code.indexOf('\n') !== -1);
@@ -293,7 +361,7 @@
let mermaidDiv = document.createElement('div');
- mermaidDiv.className = 'mermaid';
+ mermaidDiv.className = 'mermaid mermaid-to-render';
mermaidDiv.textContent = code;
@@ -307,7 +375,7 @@
setTimeout(function() {
- let mermaidElements = document.querySelectorAll('.mermaid:not(.mermaid-rendered)');
+ let mermaidElements = document.querySelectorAll('.mermaid-to-render:not(.mermaid-rendered)');
if (mermaidElements.length > 0) {
@@ -317,6 +385,8 @@
el.classList.add('mermaid-rendered');
+ el.classList.remove('mermaid-to-render');
+
});
try {