feat: add PHP preprocessing for Mermaid code blocks
- Add argon_format_mermaid_code filter in functions.php - Decode WP-Markdown escape characters in PHP - Auto-add newlines for single-line mermaid code - Simplify JavaScript rendering logic - Use data-mermaid-code attribute for preprocessed code - Remove debug console.log statements
This commit is contained in:
@@ -3607,6 +3607,52 @@ function the_content_filter($content){
|
||||
return $content;
|
||||
}
|
||||
add_filter('the_content' , 'the_content_filter',20);
|
||||
|
||||
// Mermaid 代码块预处理
|
||||
function argon_format_mermaid_code($content) {
|
||||
if (!get_option('argon_mermaid_enable', 'false') == 'true') {
|
||||
return $content;
|
||||
}
|
||||
|
||||
// 匹配 WP-Markdown 生成的 mermaid 代码块格式
|
||||
$pattern = '/<div class="mermaid">(.*?)<\/div>/s';
|
||||
|
||||
$content = preg_replace_callback($pattern, function($matches) {
|
||||
$div_content = $matches[1];
|
||||
|
||||
// 检查是否包含 script 标签(WP-Markdown 格式)
|
||||
if (preg_match('/<script[^>]*>document\.write\("(.*)"\)<\/script>/s', $div_content, $script_match)) {
|
||||
$mermaid_code = $script_match[1];
|
||||
|
||||
// 解码转义字符
|
||||
$mermaid_code = str_replace('\\n', "\n", $mermaid_code);
|
||||
$mermaid_code = str_replace('\\"', '"', $mermaid_code);
|
||||
$mermaid_code = str_replace("\\'", "'", $mermaid_code);
|
||||
$mermaid_code = html_entity_decode($mermaid_code);
|
||||
|
||||
// 如果代码看起来像一行(前100个字符中换行符很少),添加换行
|
||||
$first_100 = substr($mermaid_code, 0, 100);
|
||||
$newline_count = substr_count($first_100, "\n");
|
||||
|
||||
if ($newline_count < 2) {
|
||||
// 在箭头前添加换行
|
||||
$mermaid_code = preg_replace('/\s*-->\s*/', "\n --> ", $mermaid_code);
|
||||
// 在 style 语句前添加换行
|
||||
$mermaid_code = preg_replace('/\s*style\s+/', "\nstyle ", $mermaid_code);
|
||||
}
|
||||
|
||||
// 返回带 data 属性的 div
|
||||
return '<div class="mermaid" data-mermaid-code="' . esc_attr($mermaid_code) . '">' . $div_content . '</div>';
|
||||
}
|
||||
|
||||
// 不是 WP-Markdown 格式,保持原样
|
||||
return $matches[0];
|
||||
}, $content);
|
||||
|
||||
return $content;
|
||||
}
|
||||
add_filter('the_content', 'argon_format_mermaid_code', 25);
|
||||
|
||||
//使用 CDN 加速 gravatar
|
||||
function gravatar_cdn($url){
|
||||
$cdn = get_option('argon_gravatar_cdn', 'gravatar.pho.ink/avatar/');
|
||||
|
||||
Reference in New Issue
Block a user