584   Composer HTML Laravel PHP

laravel5 , markdown to html , 使用 michelf/php-markdown 类库,
html to markdown 使用thephpleague/html-to-markdown类库,
 

1,加载类库:

composer require "michelf/php-markdown:1.4.*"
composer require "league/html-to-markdown:4.1.*"

2,在根目录下,新建app/HtmlMarkdown/HtmlMarkdownConvertor.php,增加了HtmlMarkdown目录和HtmlMarkdownConvertor文件,内容为

<?php 
namespace App\HtmlMarkdown; 
use Michelf\MarkdownExtra; 
use Purifier; 
use League\HTMLToMarkdown\HtmlConverter; 

class HtmlMarkdownConvertor { 
    protected $htmlConverter; 
    protected $markdownParser; 
    public function __construct() { 
        $this->htmlConverter = new HtmlConverter();

        $this->markdownParser = new MarkdownExtra();
        $this->markdownParser->no_markup = true;
    }

    public function convertHtmlToMarkdown($html)
    {
        return $this->htmlConverter->convert($html);
    }

    public function convertMarkdownToHtml($markdown)
    {
        $html = $this->markdownParser->transform($markdown);
        return Purifier::clean($html, 'markdown');
    }
}

3,在控制器PostsContollers使用方法:

<?php 
namespace App\Http\Controllers; 
use App\HtmlMarkdown\HtmlMarkdownConvertor; 
class PostsController extends Controller { 
    protected $htmlMarkdownConvertor; 
    public function __construct(HtmlMarkdownConvertor $htmlMarkdownConvertor) { 
        $this->htmlMarkdownConvertor = $htmlMarkdownConvertor;
    }

    public function postCreate(Request $request){

        $all = $request->all();
        $markdown_content = $all['content'];
        $html_content = $this->htmlMarkdownConvertor->convertMarkdownToHtml($markdown_content);
        
        $post = Posts::findOrFail($id);
        $html_content = $post->content;
        $markdown_content = $this->htmlMarkdownConvertor->convertHtmlToMarkdown($html_content);
        

    }




Leave a Reply

Your email address will not be published. Required fields are marked *