PHP code example of pjkui / yii2-markdown

1. Go to this page and download the library: Download pjkui/yii2-markdown library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pjkui / yii2-markdown example snippets


use pjkui\markdown\Editor;

echo Editor::widget([
    'model'     => $model,
    'attribute' => 'content',
    'options'   => [
        'url'   => '/upload',       // 文件上传接口
        'extra' => ['type' => 'md'],
    ],
]);

echo Editor::widget([
    'model'      => $model,
    'attribute'  => 'content',
    'isMarkdown' => false,           // 启动即 WYSIWYG(Vditor)
    'options'    => ['url' => '/upload'],
]);

use pjkui\markdown\Editor;
use pjkui\markdown\ConverterAsset;
use pjkui\markdown\VditorAsset;

// 预加载双引擎资源(包含 dual-engine-controller.js)
ConverterAsset::register($this); // $this = \yii\web\View
VditorAsset::register($this);

echo Editor::widget([
    'model'     => $model,
    'attribute' => 'content',
    'options'   => ['url' => '/upload'],
]);

public function rules(): array
{
    return [
        [['content', 'content_md', 'content_html'], 'string'],
    ];
}

public function beforeSave($insert): bool
{
    if (!parent::beforeSave($insert)) return false;
    // 以 Markdown 为权威源,HTML 用作展示缓存
    if (!empty($this->content_md)) {
        $this->content = $this->content_md;
    }
    return true;
}

use pjkui\markdown\Preview;

echo Preview::widget([
    'value' => $model->content,  // Markdown 字符串
]);