laravel 中引入markdown,html處理外掛程式

來源:互聯網
上載者:User

標籤:top   parse   services   return   body   request   cti   composer   get   

1.Purifier:過濾不良的代碼外掛程式

composer 中引入 ,git地址

"mews/purifier": "~2.0"
php artisan vendor:publish --provider="Mews\Purifier\PurifierServiceProvider"

2.引入html轉markdown外掛程式,git地址

"league/html-to-markdown": "^4.2",

3.引入markdown轉html外掛程式,git地址  官網地址

"erusev/parsedown": "^1.6",

4.測試代碼

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Services\Mention;use Parsedown;use League\HTMLToMarkdown\HtmlConverter;use Purifier;class Test extends Controller{protected $htmlParser;    protected $markdownParser;    public function __construct()    {        $this->htmlParser = new HtmlConverter([‘header_style‘ => ‘atx‘]);        $this->markdownParser = new Parsedown();    }    public function t2(Request $request)    {        $mention = new Mention();        $content = $request->input(‘content‘);        $markdownContent = $mention->parse($content);        $htmlContent = $this->convertMarkdownToHtml($markdownContent);        var_dump($htmlContent);exit;    }    public function convertHtmlToMarkdown($html)    {        return $this->htmlParser->convert($html);    }    public function convertMarkdownToHtml($markdown)    {        $convertedHmtl = $this->markdownParser->setBreaksEnabled(true)->text($markdown);        $convertedHmtl = Purifier::clean($convertedHmtl, ‘user_topic_body‘);        $convertedHmtl = str_replace("<pre><code>", ‘<pre><code class=" language-php">‘, $convertedHmtl);        return $convertedHmtl;    }}

mention 類檔案代碼

<?phpnamespace App\Services;use App\Model\User;class Mention{    public $body_parsed;    public $users = [];    public $usernames;    public $body_original;    public function getMentionedUsername()    {        preg_match_all("/(\S*)\@([^\r\n\s]*)/i", $this->body_original, $atlist_tmp);        $usernames = [];        foreach ($atlist_tmp[2] as $k=>$v) {            if ($atlist_tmp[1][$k] || strlen($v) >25) {                continue;            }            $usernames[] = $v;        }        return array_unique($usernames);    }    public function replace()    {        $this->body_parsed = $this->body_original;        foreach ($this->users as $user) {            $search = ‘@‘ . $user->name;            //$place = ‘[‘.$search.‘](‘.route(‘users.show‘, $user->id).‘)‘;            $place = ‘[‘.$search.‘](http://user?id=‘.$user->id.‘)‘;            $this->body_parsed = str_replace($search, $place, $this->body_parsed);        }    }    public function parse($body)    {        $this->body_original = $body;        $this->usernames = $this->getMentionedUsername();        count($this->usernames) > 0 && $this->users = User::whereIn(‘name‘, $this->usernames)->get();        $this->replace();        return $this->body_parsed;    }}

 

laravel 中引入markdown,html處理外掛程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.