时间:2021-07-01 10:21:17 帮助过:108人阅读
php实现markdown转html的方法:用markdown编辑api的方式,代码为【$fileContent = file_get_contents(storage_path('doc/admin_api.md'))】。
【相关学习推荐:php编程(视频)】
php实现markdown转html的方法:
使用插件实现markdown转为html
功能很简单,就直接上代码啦。
- <?php
- namespace App\Http\Controllers\Admin;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Parsedown;
- class ApiDocController extends Controller
- {
- public function __construct(){
- $this->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样式我都给大家调好了,大家直接拿去用吧。
- <!doctype html>
- <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Laravel</title>
- <style>
- html, body {
- background-color: #fff;
- color: #636b6f;
- font-family: 'Nunito', sans-serif;
- font-weight: 200;
- height: 100vh;
- margin: 0;
- color:#222; }
- .container{
- width:800px;
- margin:10px auto;
- padding:20px;
- border-left:2px solid silver;
- border-right:2px solid silver; }
- table th,td{
- border:1px solid #ede;
- padding:5px 10px; }
- pre{
- background: #666;
- color: white;
- padding: 20px 10px;
- font-family: yahei;
- overflow: auto; }
- li code{
- font-size: 28px;
- color: #4eb4ee;
- font-weight: bold;
- }
- </style>
- </head>
- <body>
- <div>
- {!! $content !!}
- </div>
- </body>
- </html>
想了解更多编程学习,敬请关注php培训栏目!
以上就是php如何实现markdown转html的详细内容,更多请关注gxlcms其它相关文章!