Преглед изворни кода

fix stabilize KP markdown math rendering in local PDF flow

Disable KaTeX cache for unified PDF rendering and restore matrix row-break escapes in KP markdown math blocks only to avoid affecting non-KP formula paths.

Made-with: Cursor
yemeishu пре 2 недеља
родитељ
комит
15938fe392
1 измењених фајлова са 30 додато и 1 уклоњено
  1. 30 1
      app/Services/ExamPdfExportService.php

+ 30 - 1
app/Services/ExamPdfExportService.php

@@ -3479,7 +3479,7 @@ class ExamPdfExportService
             // 2. 使用 KatexRenderer 进行服务端预渲染
             // 2. 使用 KatexRenderer 进行服务端预渲染
             if ($this->katexRenderer) {
             if ($this->katexRenderer) {
                 $beforeLength = strlen($html);
                 $beforeLength = strlen($html);
-                $html = $this->katexRenderer->renderHtml($html);
+                $html = $this->katexRenderer->disableCache()->renderHtml($html);
                 $afterLength = strlen($html);
                 $afterLength = strlen($html);
 
 
                 Log::info('ExamPdfExportService: LaTeX 公式服务端预渲染完成', [
                 Log::info('ExamPdfExportService: LaTeX 公式服务端预渲染完成', [
@@ -5174,10 +5174,39 @@ MARKDOWN;
         $parser = new \Michelf\MarkdownExtra;
         $parser = new \Michelf\MarkdownExtra;
         $markdown = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
         $markdown = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
         $rendered = $parser->transform($markdown);
         $rendered = $parser->transform($markdown);
+        $rendered = $this->restoreLatexRowBreaksInMathBlocks($rendered);
 
 
         return '<div class="kp-markdown-container kp-markdown-content">'.$rendered.'</div>';
         return '<div class="kp-markdown-container kp-markdown-content">'.$rendered.'</div>';
     }
     }
 
 
+    /**
+     * 仅针对知识点讲解中的矩阵环境,恢复 Markdown 解析后丢失的行分隔符。
+     *
+     * 典型异常:\begin{vmatrix}...&#92;x_1...&#92;x_2...\end{vmatrix}
+     * 需要还原为:...\\x_1...\\x_2...
+     *
+     * 注意:这里故意只处理 matrix/vmatrix/bmatrix/pmatrix/Bmatrix,避免影响其他公式场景。
+     */
+    private function restoreLatexRowBreaksInMathBlocks(string $html): string
+    {
+        return (string) preg_replace_callback('/\$\$([\s\S]*?)\$\$/', static function (array $m): string {
+            $math = (string) ($m[1] ?? '');
+            if ($math === '' || str_contains($math, '&#92;') === false) {
+                return (string) ($m[0] ?? '');
+            }
+
+            // 仅在矩阵相关环境中恢复,避免误改普通数学块。
+            if (! preg_match('/\\\\begin\{(?:matrix|vmatrix|bmatrix|pmatrix|Bmatrix)\}/', $math)) {
+                return (string) ($m[0] ?? '');
+            }
+
+            // 仅恢复“行分隔”语义:实体反斜杠后接字母/数字(常见为 x_1、a_1、1)
+            $math = preg_replace('/&#92;(?=\s*[A-Za-z0-9])/', '\\\\\\\\', $math) ?? $math;
+
+            return '$$'.$math.'$$';
+        }, $html);
+    }
+
     private function looksLikeHtml(string $content): bool
     private function looksLikeHtml(string $content): bool
     {
     {
         if (stripos($content, 'kp-markdown-container') !== false ||
         if (stripos($content, 'kp-markdown-container') !== false ||