Browse Source

增加 渲染模式选项

visuddhinanda 3 years ago
parent
commit
53b7b17ef4
2 changed files with 12 additions and 7 deletions
  1. 6 6
      app/Http/Api/MdRender.php
  2. 6 1
      app/Http/Resources/ArticleResource.php

+ 6 - 6
app/Http/Api/MdRender.php

@@ -79,7 +79,7 @@ class MdRender{
         }
         return $output;
     }
-    public static function xml2tpl(string $xml, $channelId=""):string{
+    public static function xml2tpl(string $xml, $channelId="",$mode='read'):string{
         /**
          * 解析xml
          * 获取模版参数
@@ -114,7 +114,7 @@ class MdRender{
             /**
              * 生成模版参数
              */
-            $tplRender = new TemplateRender($props,$channelId,'edit');
+            $tplRender = new TemplateRender($props,$channelId,$mode);
             $tplProps = $tplRender->render($tpl_name);
             if($tplProps){
                 $tpl->addAttribute("props",$tplProps['props']);
@@ -127,13 +127,13 @@ class MdRender{
         return $html;
     }
 
-    public static function render2($markdown,$channelId='',$queryId=null){
+    public static function render2($markdown,$channelId='',$queryId=null,$mode='read'){
         $wiki = MdRender::markdown2wiki($markdown);
         $html = MdRender::wiki2xml($wiki);
         if(!is_null($queryId)){
             $html = MdRender::xmlQueryId($html, $queryId);
         }
-        $tpl = MdRender::xml2tpl($html,$channelId);
+        $tpl = MdRender::xml2tpl($html,$channelId,$mode);
         return $tpl;
     }
     public static function markdown2wiki(string $markdown): string{
@@ -170,8 +170,8 @@ class MdRender{
     /**
      *
      */
-    public static function render($markdown,$channelId,$queryId=null){
-        return MdRender::render2($markdown,$channelId,$queryId);
+    public static function render($markdown,$channelId,$queryId=null,$mode='read'){
+        return MdRender::render2($markdown,$channelId,$queryId,$mode);
 
         $html = MdRender::markdown2wiki($markdown);
 

+ 6 - 1
app/Http/Resources/ArticleResource.php

@@ -73,7 +73,12 @@ class ArticleResource extends JsonResource
             }
             Log::info("channel:{$channel}");
             Log::info("query_id:{$query_id}");
-            $data["html"] = MdRender::render($this->content,$channel,$query_id);
+            if($request->has('mode')){
+                $mode = $request->get('mode');
+            }else{
+                $mode = 'read';
+            }
+            $data["html"] = MdRender::render($this->content,$channel,$query_id,$mode);
         }
         return $data;
     }