How to convert markdown to html in php

coldplay.xixi
Release: 2023-03-05 18:22:02
Original
4251 people have browsed it

php method to convert markdown to html: use markdown editing api, the code is [$fileContent = file_get_contents(storage_path('doc/admin_api.md'))].

How to convert markdown to html in php

[Related learning recommendations: php programming (video)]

How to convert markdown to html in php:

Use a plug-in to convert markdown to html

The function is very simple, just Just upload the code.

<?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(&#39;doc/admin_api.md&#39;));
        $htmlContent = $this->convertMarkdownToHtml($fileContent);
        $content = $this->convertMarkdownToHtml($htmlContent);
        return view(&#39;apidoc_admin&#39;)->with(&#39;content&#39;,$content);
    }
    public function convertMarkdownToHtml($markdown)
    {
        $convertedHmtl = $this->markdownParser->setBreaksEnabled(true)->text($markdown);
        return $convertedHmtl;
    }
}
Copy after login

What this article recommends is to use markdown to edit the api. md is the suffix of the markdown file. I now put this file at storage/doc/admin_api.md.

For testing, I temporarily pasted an api in markdown format into the file:

**简要描述:** 
- 用户登录接口
**请求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:普通用户  |
 **备注** 
- 更多返回错误代码请看首页的错误代码描述
Copy after login

Finally, a view file needs to be prepared.

I created it in the resources/views folder, and the file name is: apidoc_admin.blade.php.

It is convenient to express my strong recommendation. I have adjusted the css style for everyone. You can use it directly.

<!doctype html>
<html lang="{{ str_replace(&#39;_&#39;, &#39;-&#39;, 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: &#39;Nunito&#39;, 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>
Copy after login

If you want to learn more about programming, please pay attention to the php training column!

The above is the detailed content of How to convert markdown to html in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!