Browse Source

Merge pull request #1970 from visuddhinanda/laravel

支持note渲染markdown
visuddhinanda 2 years ago
parent
commit
632a6ec910

+ 4 - 2
app/Http/Api/MdRender.php

@@ -27,7 +27,7 @@ class MdRender{
         'format'=>'react',
         'debug'=>[],
         'studioId'=>null,
-        'lang'=>'en',
+        'lang'=>'zh-Hans',
         ];
 
     public function __construct($options=[])
@@ -273,7 +273,9 @@ class MdRender{
                                         $this->options['mode'],
                                         $this->options['format'],
                                         $this->options['studioId'],
-                                        $this->options['debug']);
+                                        $this->options['debug'],
+                                        $this->options['lang'],
+                                    );
 
             $tplProps = $tplRender->render($tpl_name);
             if($this->options['format']==='react' && $tplProps){

+ 22 - 12
app/Http/Api/TemplateRender.php

@@ -35,7 +35,7 @@ class TemplateRender{
      * string $format  'react' | 'text' | 'tex' | 'unity'
      * @return void
      */
-    public function __construct($param, $channelInfo, $mode,$format='react',$studioId,$debug=[])
+    public function __construct($param, $channelInfo, $mode,$format='react',$studioId,$debug=[],$lang='zh-Hans')
     {
         $this->param = $param;
         foreach ($channelInfo as $value) {
@@ -51,12 +51,12 @@ class TemplateRender{
             $channelId = $this->channel_id[0];
             if(Str::isUuid($channelId)){
                 $lang = Channel::where('uid',$channelId)->value('lang');
-                if(!empty($lang)){
-                    $this->lang = $lang;
-                    $this->langFamily = explode('-',$lang)[0];
-                }
             }
         }
+        if(!empty($lang)){
+            $this->lang = $lang;
+            $this->langFamily = explode('-',$lang)[0];
+        }
     }
     /**
      * TODO 设置默认语言。在渲染某些内容的时候需要语言信息
@@ -139,14 +139,24 @@ class TemplateRender{
         }
 
         if(count($this->channelInfo)===0){
-            Log::error('channel is null');
-            $output = [
-                "word" => $word,
-                'innerHtml' => '',
-            ];
-            return $output;
+            if(!empty($channel)){
+                $channelInfo = Channel::where('uid',$channel)->first();
+                if(!$channelInfo){
+                    unset($channelInfo);
+                }
+            }
+            if(!isset($channelInfo)){
+                Log::error('channel is null');
+                $output = [
+                    "word" => $word,
+                    'innerHtml' => '',
+                ];
+                return $output;
+            }
+        }else{
+            $channelInfo = $this->channelInfo[0];
         }
-        $channelInfo = $this->channelInfo[0];
+
         if(Str::isUuid($channelId)){
             $lang = Channel::where('uid',$channelId)->value('lang');
             if(!empty($lang)){

+ 4 - 1
app/Http/Controllers/UserDictController.php

@@ -24,7 +24,10 @@ class UserDictController extends Controller
     {
         //
 		$result=false;
-		$indexCol = ['id','word','type','grammar','mean','parent','note','factors','confidence','dict_id','updated_at','creator_id'];
+		$indexCol = ['id','word','type','grammar',
+                     'mean','parent','note',
+                     'factors','confidence','dict_id',
+                     'updated_at','creator_id'];
 		switch ($request->get('view')) {
             case 'all':
             # 获取studio内所有channel

+ 5 - 0
app/Http/Resources/UserDictResource.php

@@ -6,6 +6,7 @@ use Illuminate\Http\Resources\Json\JsonResource;
 use App\Http\Api\UserApi;
 use App\Models\UserOperationDaily;
 use App\Models\DictInfo;
+use App\Http\Api\MdRender;
 
 class UserDictResource extends JsonResource
 {
@@ -30,6 +31,10 @@ class UserDictResource extends JsonResource
          'updated_at'=>$this->updated_at,
          'creator_id'=>$this->creator_id,
         ];
+        if(!empty($this->note)){
+            $mdRender = new MdRender(['format'=>'react','lang'=>'zh-Hans']);
+            $data['note'] = $mdRender->convert($this->note);
+        }
         if($request->get('view')==='community'){
             $data['editor'] = UserApi::getById($this->creator_id);
             //毫秒计算的经验值