visuddhinanda 4 дней назад
Родитель
Сommit
f5cbce6906
2 измененных файлов с 75 добавлено и 22 удалено
  1. 23 1
      api-v12/app/Http/Api/MdRender.php
  2. 52 21
      api-v12/app/Tools/Markdown.php

+ 23 - 1
api-v12/app/Http/Api/MdRender.php

@@ -645,8 +645,10 @@ class MdRender
                     $output .= '</div>';
                     $output .= '</div>';
                     unset($GLOBALS['note']);
                     unset($GLOBALS['note']);
                 }
                 }
+
                 //处理图片链接
                 //处理图片链接
                 $output = str_replace('<img src="', '<img src="' . config('app.url'), $output);
                 $output = str_replace('<img src="', '<img src="' . config('app.url'), $output);
+                $output = $this->replaceSinglePWithSpan($output);
                 break;
                 break;
             case 'markdown':
             case 'markdown':
                 //处理脚注
                 //处理脚注
@@ -682,7 +684,27 @@ class MdRender
         );
         );
 
 
         $output  = $mdRender->convert($markdown, $channelId, $queryId);
         $output  = $mdRender->convert($markdown, $channelId, $queryId);
-
         return $output;
         return $output;
     }
     }
+
+    /**
+     * 如果字符串中只有一对 p 标签,则替换为 span
+     *
+     * @param string $html
+     * @return string
+     */
+    public static function replaceSinglePWithSpan(string $html): string
+    {
+        preg_match_all('/<p\b[^>]*>.*?<\/p>/is', $html, $matches);
+
+        if (count($matches[0]) === 1) {
+            return preg_replace(
+                ['/^\s*<p\b([^>]*)>/i', '/<\/p>\s*$/i'],
+                ['<span$1>', '</span>'],
+                $html
+            );
+        }
+
+        return $html;
+    }
 }
 }

+ 52 - 21
api-v12/app/Tools/Markdown.php

@@ -1,47 +1,55 @@
 <?php
 <?php
+
 namespace App\Tools;
 namespace App\Tools;
+
 use Illuminate\Support\Str;
 use Illuminate\Support\Str;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Http;
 use Illuminate\Support\Facades\Http;
 
 
 class Markdown
 class Markdown
 {
 {
-    public static function driver($driver){
+    public static function driver($driver)
+    {
         $GLOBALS['markdown.driver'] = $driver;
         $GLOBALS['markdown.driver'] = $driver;
     }
     }
-    public static function render($text){
-        if(isset($GLOBALS['markdown.driver']) &&
-            $GLOBALS['markdown.driver'] === 'str'){
+    public static function render($text)
+    {
+        if (
+            isset($GLOBALS['markdown.driver']) &&
+            $GLOBALS['markdown.driver'] === 'str'
+        ) {
             return Markdown::strdown($text);
             return Markdown::strdown($text);
-        }else{
+        } else {
             return Markdown::strdown($text);
             return Markdown::strdown($text);
         }
         }
     }
     }
-    public static function morus_restful($text){
+    public static function morus_restful($text)
+    {
         $host = config('mint.server.rpc.morus.host');
         $host = config('mint.server.rpc.morus.host');
-        Log::debug('morus host='.$host);
+        Log::debug('morus host=' . $host);
 
 
         $response = Http::post($host, [
         $response = Http::post($host, [
-            'text'=>$text
+            'text' => $text
         ]);
         ]);
-        if($response->successful()){
+        if ($response->successful()) {
             return $response->json('data');
             return $response->json('data');
-        }else{
-            Log::error('morus_restful fail markdown='.$text);
+        } else {
+            Log::error('morus_restful fail markdown=' . $text);
             return Str::markdown($text);
             return Str::markdown($text);
         }
         }
     }
     }
 
 
-    public static function morus($text){
+    public static function morus($text)
+    {
 
 
-        if(isset($GLOBALS['morus_client'])){
+        if (isset($GLOBALS['morus_client'])) {
             $client = $GLOBALS['morus_client'];
             $client = $GLOBALS['morus_client'];
-        }else{
-            $host = config('mint.server.rpc.morus.host') . ':'. config('mint.server.rpc.morus.port');
-            Log::debug('morus host='.$host);
+        } else {
+            $host = config('mint.server.rpc.morus.host') . ':' . config('mint.server.rpc.morus.port');
+            Log::debug('morus host=' . $host);
             $client = new \Mint\Morus\V1\MarkdownClient($host, [
             $client = new \Mint\Morus\V1\MarkdownClient($host, [
-                    'credentials' => \Grpc\ChannelCredentials::createInsecure(),
-                ]);
+                'credentials' => \Grpc\ChannelCredentials::createInsecure(),
+            ]);
             $GLOBALS['morus_client'] = $client;
             $GLOBALS['morus_client'] = $client;
         }
         }
 
 
@@ -56,8 +64,31 @@ class Markdown
         return $response->getPayload();
         return $response->getPayload();
     }
     }
 
 
-    public static function strdown($text){
-        $text = str_replace("** ","**\r\n ",$text);
-        return Str::markdown($text);
+    public static function strdown($text)
+    {
+        $text = str_replace("** ", "**\r\n ", $text);
+        $html = Str::markdown($text);
+        $html = self::replaceSinglePWithSpan($html);
+        return $html;
+    }
+    /**
+     * 如果字符串中只有一对 p 标签,则替换为 span
+     *
+     * @param string $html
+     * @return string
+     */
+    public static function replaceSinglePWithSpan(string $html): string
+    {
+        preg_match_all('/<p\b[^>]*>.*?<\/p>/is', $html, $matches);
+
+        if (count($matches[0]) === 1) {
+            return preg_replace(
+                ['/^\s*<p\b([^>]*)>/i', '/<\/p>\s*$/i'],
+                ['<span$1>', '</span>'],
+                $html
+            );
+        }
+
+        return $html;
     }
     }
 }
 }