fix: 在 PHP 端处理 Mermaid 代码块换行符 - 在 the_content_filter 中添加 Mermaid 预处理 - 使用正则表达式匹配代码块 - 解码 HTML 实体并重新编码 - 确保换行符在输出时保留

This commit is contained in:
2026-01-23 19:17:57 +08:00
parent c96e0bdfa3
commit b7aac3633e

View File

@@ -3566,6 +3566,24 @@ function argon_fancybox($content){
return $content; return $content;
} }
function the_content_filter($content){ function the_content_filter($content){
// 处理 Mermaid 代码块,确保换行符正确
if (get_option('argon_enable_mermaid') == 'true') {
// 匹配 <pre><code class="language-mermaid">...</code></pre> 或 <pre><code class="mermaid">...</code></pre>
$content = preg_replace_callback(
'/<pre><code\s+class=["\'](?:language-)?mermaid["\']>(.*?)<\/code><\/pre>/is',
function($matches) {
$code = $matches[1];
// 解码 HTML 实体
$code = html_entity_decode($code, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// 去除首尾空白
$code = trim($code);
// 返回带换行符的代码块
return '<pre><code class="language-mermaid">' . htmlspecialchars($code, ENT_QUOTES, 'UTF-8') . '</code></pre>';
},
$content
);
}
// 根据设置决定是否启用懒加载 // 根据设置决定是否启用懒加载
if (get_option('argon_enable_lazyload') !== 'false') { if (get_option('argon_enable_lazyload') !== 'false') {
$content = argon_lazyload($content); $content = argon_lazyload($content);