首頁  >  文章  >  後端開發  >  php如何實作markdown轉html

php如何實作markdown轉html

coldplay.xixi
coldplay.xixi原創
2020-09-04 09:55:514116瀏覽

php實作markdown轉html的方法:用markdown編輯api的方式,程式碼為【$fileContent = file_get_contents(storage_path('doc/admin_api.md'))】。

php如何實作markdown轉html

【相關學習推薦:#php程式設計(影片)】

php實作markdown轉html的方法:

# 使用外掛實作markdown轉為html

功能很簡單,就直接上代碼啦。

markdownParser = new Parsedown();
    }
    public function showDoc(Request $request){
        $fileContent = file_get_contents(storage_path('doc/admin_api.md'));
        $htmlContent = $this->convertMarkdownToHtml($fileContent);
        $content = $this->convertMarkdownToHtml($htmlContent);
        return view('apidoc_admin')->with('content',$content);
    }
    public function convertMarkdownToHtml($markdown)
    {
        $convertedHmtl = $this->markdownParser->setBreaksEnabled(true)->text($markdown);
        return $convertedHmtl;
    }
}

本文推薦的就是用markdown編輯api的方式,md就是markdown檔案的後綴,我現在把這個檔案放在storage/doc/admin_api.md處。

為了測試,我暫時在文件裡貼了一個markdown格式的api:

**简要描述:** 
- 用户登录接口
**请求URL:** 
- ` http://xx.com/api/user/login `
  
**请求方式:**
- POST 
**参数:** 
|参数名|必选|类型|说明|
|:----    |:---|:----- |-----   |
|username |是  |string |用户名   |
|password |是  |string | 密码    |
 **返回示例**
``` 
  {
    "error_code": 0,
    "data": {
      "uid": "1",
      "username": "zhai coder",
      "name": "璇哈",
      "groupid": 2 ,
      "reg_time": "2019-08-01",
      "last_login_time": "0",
    }
  }
```
 **返回参数说明** 
|参数名|类型|说明|
|:-----  |:-----|-----                           |
|groupid |int   |用户组id,1:超级管理员;2:普通用户  |
 **备注** 
- 更多返回错误代码请看首页的错误代码描述

最後還需要準備好一個view檔。

我是創建在resources/views資料夾下的,檔案名稱為:apidoc_admin.blade.php。

方便表達我強烈的推薦意願,css樣式我都給大家調好了,大家直接拿去用吧。






Laravel



{!! $content !!}

想了解更多程式設計學習,請關注php培訓欄位!

#

以上是php如何實作markdown轉html的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn